duannao3819 2016-07-26 08:21
浏览 1099
已采纳

如何通过CropperJS获得裁剪图像的坐标?

I am using cropper JS to crop my image. I am able to get width and height of my canvas, however, need to know co-ordinates (X and Y) of cropped image.

Here is my code-

(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));

    };

  });

})();

Any idea, how to get coordinate of cropped image, so that I can crop this image by PHP.

  • 写回答

1条回答 默认 最新

  • dongyuan1870 2016-08-17 05:44
    关注

    So, if i understand properly, you are looking for the method getData.

    It will return these properties as the documentation says:

    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
    

    Although, i suggest taking a look at this section of the doc: https://github.com/fengyuanchen/cropper#getcroppedcanvasoptions

    Specifically at this part:

    After then, you can display the canvas as an image directly, or use HTMLCanvasElement.toDataURL to get a Data URL, or use HTMLCanvasElement.toBlob to get a blob and upload it to server with FormData if the browser supports these APIs.

    If you cannot use the toBlob method because some browser limitation (you can always add a polyfill), you can get the image using toDataUrl over the canvas (IE9+). And send it to the backend service already cropped. For this you are going to need to do something like this:

    var formData = new FormData();
    formData.append('image', canvas.toDataURL());
    

    You can check the formData browser support in caniuse

    Hope you find it helpful!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到