【发布时间】:2015-09-22 10:55:23
【问题描述】:
我正在使用 FabricJS 构建我的在线 T 恤设计器的新版本,以替换我几年前创建的现有 Flash 设计器。
我遇到了一个需要帮助的问题。我使用的所有图像都是光栅图像。当我缩放图像或对其进行更改(例如重新着色)时,我会调用服务器以创建新图像,然后我需要能够替换画布上的现有图像。
我能够弄清楚如何做到这一点的唯一方法是使用画布方法 getActiveObject()。这很好用,但如果您选择了多个对象,您不知道要更新哪一个。
当图像最初添加到画布时,我将一个 onModified 事件处理程序附加到它。只有当图像的比例发生变化时,我才会调用服务器端脚本来生成新的图像。下面列出了我当前使用的代码。任何帮助将不胜感激。
谢谢
fabric.Image.fromURL(previewPath, function (img) {
var sprite = img.set({ left: leftX, top: topY, angle: angle, borderColor: 'black', cornerColor: 'red', cornerSize: 6, transparentCorners: false });
$scope.canvas.add(sprite);
$scope.canvas.renderAll();
sprite.on('modified', function () {
// Modified event is only executed for scaling
if (1 != sprite.scaleX.toString() || 1 != sprite.scaleY.toString()) {
fabric.Image.fromURL(previewPath, function (img) {
var newSprite = img.set({ left: leftX, top: topY, angle: angle, width: width, height: height });
// Replaces visible object on canvas
// Since new image is replacing old one, need to set the scale back to 1
sprite.scale(1);
$scope.canvas.renderAll();
});
}
});
【问题讨论】:
标签: javascript canvas fabricjs