简体   繁体   English

当我尝试使用cropperJS获得50%的图像时出现错误

[英]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.

相关问题 如何通过CropperJS获取裁剪图像的坐标? - How to get Coordinate of Cropped Image by CropperJS? 尝试执行 XQuery 语句时出现错误 - I get an error when I try to execute an XQuery statement 当我尝试在JavaScript中解析JSON时为什么会出错 - Why I get error when I try to parse JSON in the JavaScript 尝试使用 Firebase Auth 时出现错误 - I get an Error when I try to use Firebase Auth 当我尝试将 HTML 文件导入 React 时,出现错误 - When I try to import an HTML file into React, I get an error 当尝试在IE中将Rule添加到样式表时出现无效参数错误 - I get Invalid argument error when try to addRule to stylesheet in ie 为什么在尝试发送带有标题的请求时出现错误? - Why i get error when try post request with headers? 为什么尝试 append 元素记录时出现错误? - Why I get error when try append element to document? 新手尝试 react.js 项目。 当我尝试导入图像时,我收到一条无法编译的错误消息 - Newbie trying a react.js project. When I try to import an image I get an error message that it failed to compile 当我尝试从属性值获取HTML数据时,为什么会出现错误? - Why do I get an error when I try to get HTML data from an attribute value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM