gpt4 book ai didi

javascript - 如何在 FabricJS 中调整对象大小而不缩放模式?

转载 作者:行者123 更新时间:2023-12-03 04:31:12 32 4
gpt4 key购买 nike

我想知道 FabricJS 中是否有一个选项可以调整对象大小而不缩放其图案?

Link to Fiddle

var canvas = window._canvas = new fabric.Canvas('c');

fabric.Image.fromURL('https://assets-cdn.github.com/images/modules/site/integrators/slackhq.png', function(img) {
var patternSourceCanvas = new fabric.StaticCanvas()
patternSourceCanvas.add(img)
patternSourceCanvas.setBackgroundColor('red', patternSourceCanvas.renderAll.bind(patternSourceCanvas))
patternSourceCanvas.setHeight(256);
patternSourceCanvas.setWidth(256);

var pattern = new fabric.Pattern({
source: function() {
return patternSourceCanvas.getElement();
},
repeat: 'no-repeat'
})

// create a rectangle object
var rect = new fabric.Rect({
fill: pattern,
width: 256,
height: 256
})

// "add" rectangle onto canvas
canvas.add(rect)

})

有什么想法吗?

最佳答案

在下面添加这个小代码。这将在缩放时重新缩放背景图案。

canvas.observe("object:scaling", function (e) {
var shape = e.target;
shape.width = shape.scaleX * shape.width;
shape.height = shape.scaleY * shape.height;
shape.scaleX = 1;
shape.scaleY = 1;
});

关于javascript - 如何在 FabricJS 中调整对象大小而不缩放模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43479595/

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