简体   繁体   English

Fabricjs - 一侧的圆角

[英]Fabricjs - rounded corners just in one side

I'm working with fabricjs and I need to create few Rects that attached each other..我正在使用fabricjs ,我需要创建几个相互连接的矩形..
The first and the last rect need to have a rounded corner only on the side facing outwards..第一个和最后一个矩形只需要在朝外的一侧有一个圆角。

I know that I can create custom class on fabricjs but I really new in all the canvas stuff..我知道我可以在fabricjs上创建自定义类,但我在所有画布上fabricjs新。

Is someone can give me a guidance?有人可以给我一个指导吗?

Yes creating a custom class is the best and less hackish thing you can do.是的,创建自定义类是您可以做的最好且不那么黑的事情。 You have to extend rect and override the render function:您必须扩展 rect 并覆盖渲染功能:

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);
},
}

Please consider a draft of the class, but should get you the idea.请考虑课程的草稿,但应该让你有想法。

Then just do var rect = fabric.rectAsymmetric({width:200, height:100, side:'left'});然后就做 var rect = fabric.rectAsymmetric({width:200, height:100, side:'left'}); and try.并尝试。 Change the name of the class to something less ugly.将类的名称更改为不那么难看的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM