用自定义图片替换fabric.js的调整大小和旋转控件?

3

我正在寻找有关如何用自定义图片替换fabric.js中的调整大小和旋转矩形的示例,是否有人已经完成了这个功能?

谢谢。


目前这并不是很容易实现,除非通过更改源代码或在每个控件的顶部显示类似于此演示的图像 - http://fabricjs.com/interaction-with-objects-outside-canvas/ - kangax
1
这个问题来自2012年,如果您通过谷歌到达此处,您可能还想查看这个更近期的答案:https://dev59.com/VH7aa4cB1Zd3GeqPwvHr#24533063 - Ziggy
1个回答

9
你可以通过覆盖原型来实现,像这样:

您可以通过覆盖原型来实现:

var topLeftImage = new Image();
topLeftImage.src = 'images/tl.png';

var topRightImage = new Image();
topRightImage.src = 'images/tr.png';

var bottomRightImage = new Image();
bottomRightImage.src = 'images/br.png';

//Warning I modified some other things here as well, please copy this from the sources and modify it then
fabric.Object.prototype.drawCorners = function(ctx) {
  if (!this.hasControls) return;

  var size = this.cornersize,
      size2 = size / 2,
      strokeWidth2 = this.strokeWidth / 2,
      left = -(this.width / 2),
      top = -(this.height / 2),
      _left,
      _top,
      sizeX = size / this.scaleX,
      sizeY = size / this.scaleY,
      paddingX = this.padding / this.scaleX,
      paddingY = this.padding / this.scaleY,
      scaleOffsetY = size2 / this.scaleY,
      scaleOffsetX = size2 / this.scaleX,
      scaleOffsetSizeX = (size2 - size) / this.scaleX,
      scaleOffsetSizeY = (size2 - size) / this.scaleY,
      height = this.height,
      width = this.width;

  ctx.save();

  ctx.lineWidth = 1 / Math.max(this.scaleX, this.scaleY);

  ctx.globalAlpha = 1; //this.isMoving ? this.borderOpacityWhenMoving : 1;
  ctx.strokeStyle = ctx.fillStyle = '#333333';

  // top-left
  _left = left - scaleOffsetX - strokeWidth2 - paddingX;
  _top = top - scaleOffsetY - strokeWidth2 - paddingY;

  ctx.clearRect(_left, _top, sizeX, sizeY);  
  ctx.drawImage(topLeftImage, _left, _top, sizeX, sizeY);

  // top-right
  _left = left + width - scaleOffsetX + strokeWidth2 + paddingX;
  _top = top - scaleOffsetY - strokeWidth2 - paddingY;

  ctx.clearRect(_left, _top, sizeX, sizeY);  
  ctx.drawImage(topRightImage, _left, _top, sizeX, sizeY);

  // bottom-left
  _left = left - scaleOffsetX - strokeWidth2 - paddingX;
  _top = top + height + scaleOffsetSizeY + strokeWidth2 + paddingY;

  ctx.clearRect(_left, _top, sizeX, sizeY);
  ctx.strokeRect(_left, _top, sizeX, sizeY);

  // bottom-right
  _left = left + width + scaleOffsetSizeX + strokeWidth2 + paddingX;
  _top = top + height + scaleOffsetSizeY + strokeWidth2 + paddingY;

  ctx.clearRect(_left, _top, sizeX, sizeY);
  ctx.drawImage(bottomRightImage, _left, _top, sizeX, sizeY);

  ctx.restore();

  return this;
};

3
你好 @Alexander Kludt,请问你能否更详细地解释一下你的代码,应该在哪里添加这段代码或覆盖它在 Fabrics 库中的位置?请给予建议。 - Imran Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接