首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >风元辰作物椭圆形状?

风元辰作物椭圆形状?
EN

Stack Overflow用户
提问于 2017-04-20 00:14:01
回答 1查看 389关注 0票数 0

当裁剪圆时,我尝试设置cropper.setAspectRatio(0);,它允许我自由拖动以获得椭圆形状,但当它裁剪时,我最终得到一个圆形裁剪,但具有填充额外缺少的信息的透明边缘。这张图片应该是width=722px height=182px

以下是获取圆形画布的函数,它适用于完美的圆形,但不适用于椭圆:

代码语言:javascript
复制
function getRoundedCanvas(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.imageSmoothingEnabled = true;
  context.drawImage(sourceCanvas, 0, 0, width, height);
  context.globalCompositeOperation = 'destination-in';
  context.beginPath();
  context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true);
  context.fill();
  return canvas;
}
EN

回答 1

Stack Overflow用户

发布于 2017-04-20 01:48:01

我可以对其进行一些修改以使其正常工作:

代码语言:javascript
复制
function getRoundedCanvas(sourceCanvas) {
  var canvas = document.createElement('canvas');
  var context = canvas.getContext('2d');
  offsetTop = Math.round(cropper.getCropBoxData().top);
  offsetLeft = Math.round(cropper.getCropBoxData().left);  
  var width = sourceCanvas.width;
  var height = sourceCanvas.height;
  canvas.width = width;
  canvas.height = height;
  context.imageSmoothingEnabled = true;
  context.drawImage(sourceCanvas, 0, 0, width, height);
  context.globalCompositeOperation = 'destination-in';
  context.beginPath();
  context.ellipse(width/2, height/2, width/2, height/2, 0 * Math.PI, 0, 45 * Math.PI);
  context.fill();
  return canvas;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43500982

复制
相关文章

相似问题

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