[英]I get the error when i try get 50% of the image with cropperJS
I want to get 50% of the image with 'cropperJS'. 我想用'cropperJS'获得50%的图像。 I create a new Image() and try to create new Cropper for further obtaining canvas.
我创建一个新的Image()并尝试创建新的Cropper以进一步获取画布。 And I get the error 'Cannot read property 'insertBefore' of null', please help me to solve this problem.
并且出现错误“无法读取null的属性'insertBefore'”,请帮助我解决此问题。 Or show me how can i get 50% of the image with out 'cropperJS'.
或者告诉我如何在不使用“ cropperJS”的情况下获得50%的图像。 Thanks in advance.
提前致谢。
<template>
<v-layout
column
justify-center
align-center
>
<v-flex
xs12
sm8
md6
>
<vue-dropzone
ref="myVueDropzone"
id="dropzone"
:options="dropzoneOptions"
@vdropzone-success="vdropzoneSuccess"
>
</vue-dropzone>
<v-img
:src="imgUrl"
>
</v-img>
</v-flex>
</v-layout>
</template>
<script>
import Logo from '~/components/Logo.vue'
import VuetifyLogo from '~/components/VuetifyLogo.vue'
import vue2Dropzone from 'vue2-dropzone'
import 'vue2-dropzone/dist/vue2Dropzone.min.css'
import Cropper from 'cropperjs'
export default {
data: function () {
return {
imgUrl:'',
dropzoneOptions: {
url: 'https://httpbin.org/post',
thumbnailWidth: 150,
maxFilesize: 0.5,
headers: { "My-Awesome-Header": "header value" }
}
}
},
components: {
vueDropzone: vue2Dropzone
},
methods:{
vdropzoneSuccess(file, response){
this.imgUrl = file.dataURL
var image = new Image()
image.src = URL.createObjectURL(file)
console.log(image)
var cropper = new Cropper(image, {
aspectRatio: 16 / 9,
crop(event) {
console.log(event.detail.x)
console.log(event.detail.y)
console.log(event.detail.width)
console.log(event.detail.height)
},
})
console.log(cropper)
}
}
}
</script>
First off Cropper.js accepts DOM node as the first param, so you sould pass the DOM node. 首先, Cropper.js接受DOM节点作为第一个参数,因此您可以通过DOM节点。 Following the documentation, this DOM node should be wrapped in
<div></div>
. 根据文档,此DOM节点应包装在
<div></div>
。 Only when image is loaded , should you init the Cropper.js . 仅当加载图像时,才应初始化Cropper.js 。
<template> <v-layout column justify-center align-center > <v-flex xs12 sm8 md6 > <vue-dropzone ref="myVueDropzone" id="dropzone" :options="dropzoneOptions" @vdropzone-success="vdropzoneSuccess" > </vue-dropzone> <div> <img ref="img" :src="imgUrl" /> </div> </v-flex> </v-layout> </template> <script> import Logo from '~/components/Logo.vue' import VuetifyLogo from '~/components/VuetifyLogo.vue' import vue2Dropzone from 'vue2-dropzone' import 'vue2-dropzone/dist/vue2Dropzone.min.css' import Cropper from 'cropperjs' export default { data: function() { return { imgUrl: '', dropzoneOptions: { url: 'https://httpbin.org/post', thumbnailWidth: 150, maxFilesize: 0.5, headers: { "My-Awesome-Header": "header value" } } } }, components: { vueDropzone: vue2Dropzone }, methods: { vdropzoneSuccess(file, response) { this.imgUrl = file.dataURL var image = new Image() image.src = URL.createObjectURL(file) image.onload = () => { var cropper = new Cropper(this.$refs.img, { aspectRatio: 16 / 9, crop(event) { console.log(event.detail.x) console.log(event.detail.y) console.log(event.detail.width) console.log(event.detail.height) }, }) } } } } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.