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

如何通过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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分