首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用CropperJS获取裁剪图像的坐标?

如何利用CropperJS获取裁剪图像的坐标?
EN

Stack Overflow用户
提问于 2016-07-26 08:21:36
回答 1查看 2.7K关注 0票数 0

我正在使用克罗珀JS来裁剪我的图像。我能够获得我的画布的宽度和高度,但是,我需要知道裁剪图像的协调(X和Y)。

这是我的密码-

代码语言:javascript
复制
(function () {


  //getting ratio
  function getImageRatio(sourceCanvas) {
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    var width = sourceCanvas.width;
    var height = sourceCanvas.height;



    canvas.width = width;
    canvas.height = height;


    context.beginPath();
    context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI);
    context.strokeStyle = 'rgba(0,0,0,0)';
    context.stroke();
    context.clip();
    context.drawImage(sourceCanvas, 0, 0, width, height);

    return canvas;
  }




  window.addEventListener('DOMContentLoaded', function () {
    var image = document.getElementById('image');
    var button = document.getElementById('button');
    var result = document.getElementById('result');
    var croppable = false;
    var cropper = new Cropper(image, {
      aspectRatio: 1,
      viewMode: 1,
      built: function () {
        croppable = true;
      }
    });

    button.onclick = function () {
      var croppedCanvas;
      var roundedCanvas;
      var roundedImage;

      if (!croppable) {
        return;
      }

      // Crop
      croppedCanvas = cropper.getCroppedCanvas();


      console.log(getImageRatio(croppedCanvas));

    };

  });

})();

任何想法,如何获得裁剪图像的坐标,以便我可以裁剪这个图像的PHP。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-17 05:44:31

因此,如果我正确理解,您正在寻找方法 getData

它将返回这些属性,如文档所述:

代码语言:javascript
复制
x: the offset left of the cropped area
y: the offset top of the cropped area
width: the width of the cropped area
height: the height of the cropped area
rotate: the rotated degrees of the image
scaleX: the scaling factor to apply on the abscissa of the image
scaleY: the scaling factor to apply on the ordinate of the image

不过,我建议您看一下doc:https://github.com/fengyuanchen/cropper#getcroppedcanvasoptions这一节

特别是在这一部分:

然后,您可以直接将画布显示为图像,或者使用HTMLCanvasElement.toDataURL获取数据URL,或者使用HTMLCanvasElement.toBlob获取blob,如果浏览器支持这些API,则使用FormData将其上传到服务器。

如果您不能使用toBlob方法,因为某些浏览器限制(您可以始终添加一个多边形填充),您可以使用画布上的toDataUrl (IE9+)获取图像。并将其发送到已经裁剪的后端服务。为此,您需要这样做:

代码语言:javascript
复制
var formData = new FormData();
formData.append('image', canvas.toDataURL());

您可以在caniuse中检查formData浏览器支持。

希望你觉得这有帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38584590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档