首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fabricjs -单边圆角

Fabricjs -单边圆角
EN

Stack Overflow用户
提问于 2015-10-22 02:23:36
回答 1查看 1.5K关注 0票数 2

我正在使用fabricjs,我需要创建一些相互连接的Rect。

第一个和最后一个矩形只需要在朝外的一侧有一个圆角。

我知道我可以在fabricjs上创建自定义类,但我对所有的画布东西都是新手。

有人能给我指点一下吗?

EN

回答 1

Stack Overflow用户

发布于 2015-11-04 20:08:06

是的,创建自定义类是您可以做的最好的事情,也是最不那么麻烦的事情。您必须扩展rect并覆盖render函数:

代码语言:javascript
复制
fabric.RectAsymmetric = fabric.util.createClass(fabric.Rect, /** @lends fabric.Rect.prototype */ {

/**
 * Side of rounding corners
 * @type String
 * @default
 */
side: 'left',

_render: function(ctx, noTransform) {

  if (this.width === 1 && this.height === 1) {
    ctx.fillRect(0, 0, 1, 1);
    return;
  }

  var rx = this.rx ? Math.min(this.rx, this.width / 2) : 0,
      ry = this.ry ? Math.min(this.ry, this.height / 2) : 0,
      w = this.width,
      h = this.height,
      x = noTransform ? this.left : -this.width / 2,
      y = noTransform ? this.top : -this.height / 2,
      isRoundedLeft = (rx !== 0 || ry !== 0) && side === 'left',
      isRoundedRight = (rx !== 0 || ry !== 0) && side === 'right',
      k = 1 - 0.5522847498 

  ctx.beginPath();

  ctx.moveTo(x + (isRoundedLeft ? rx : 0), y);
  ctx.lineTo(x + w - (isRoundedRight ? rx : 0), y);
  isRoundedRight && ctx.bezierCurveTo(x + w - k * rx, y, x + w, y + k * ry, x + w, y + ry);
  ctx.lineTo(x + w, y + h - (isRoundedRight ? ry : 0));
  isRoundedRight && ctx.bezierCurveTo(x + w, y + h - k * ry, x + w - k * rx, y + h, x + w - rx, y + h);
  ctx.lineTo(x + (isRoundedLeft ? rx : 0), y + h);
  isRoundedLeft && ctx.bezierCurveTo(x + k * rx, y + h, x, y + h - k * ry, x, y + h - ry);
  ctx.lineTo(x, y + (isRoundedLeft ? ry : 0));
  isRoundedLeft && ctx.bezierCurveTo(x, y + k * ry, x + k * rx, y, x + rx, y);

  ctx.closePath();

  this._renderFill(ctx);
  this._renderStroke(ctx);
},
}

请考虑这门课的草稿,但应该能让你明白。

然后执行var rect = fabric.rectAsymmetric({width:200,height:100,side:'left'});并尝试。将类的名称更改为不那么难看的名称。

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

https://stackoverflow.com/questions/33266546

复制
相关文章

相似问题

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