gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:13 24 4
gpt4 key购买 nike

我正在寻找有关如何用自定义图像替换 fabric.js 调整大小和旋转矩形的示例……有人这样做过吗?

谢谢。

最佳答案

您可以像这样覆盖原型(prototype)来做到这一点:

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

关于javascript - 用自定义图像替换 fabric.js 调整大小和旋转控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com