gpt4 book ai didi

javascript - 缩放 fabric.js Canvas 对象

转载 作者:可可西里 更新时间:2023-11-01 01:28:05 24 4
gpt4 key购买 nike

我的页面上有一个 fabric.js Canvas ,我想对其进行响应。我的代码适用于缩放 Canvas 本身,但不适用于我在其上绘制的对象。任何的想法?我搜索过 SO 但找不到适合我的解决方案。

var resizeCanvas;

resizeCanvas = function() {
var height, ratio, width;
ratio = 800 / 1177;
width = tmpl.$('.canvas-wrapper').width();
height = width / ratio;

canvas.setDimensions({
width: width,
height: height
});
};

Meteor.setTimeout(function() {
return resizeCanvas();
}, 100);

$(window).resize(resizeCanvas);

最佳答案

这是我的缩放功能 - 我们缩放 Canvas ,然后遍历所有对象并缩放它们。

调用zoomIt(2.2)

function zoomIt(factor) {
canvas.setHeight(canvas.getHeight() * factor);
canvas.setWidth(canvas.getWidth() * factor);
if (canvas.backgroundImage) {
// Need to scale background images as well
var bi = canvas.backgroundImage;
bi.width = bi.width * factor; bi.height = bi.height * factor;
}
var objects = canvas.getObjects();
for (var i in objects) {
var scaleX = objects[i].scaleX;
var scaleY = objects[i].scaleY;
var left = objects[i].left;
var top = objects[i].top;

var tempScaleX = scaleX * factor;
var tempScaleY = scaleY * factor;
var tempLeft = left * factor;
var tempTop = top * factor;

objects[i].scaleX = tempScaleX;
objects[i].scaleY = tempScaleY;
objects[i].left = tempLeft;
objects[i].top = tempTop;

objects[i].setCoords();
}
canvas.renderAll();
canvas.calcOffset();
}

关于javascript - 缩放 fabric.js Canvas 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301286/

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