简体   繁体   English

使用Fabric.js,在缩放图像时,如何用另一个图像替换它?

[英]Using Fabric.js, when an image is scaled, how do you replace it with another image?

I'm building a new version of my online t-shirt designer using FabricJS to replace an existing Flash one that I created several years ago. 我正在使用FabricJS构建我的在线T恤设计师的新版本,以替换几年前创建的现有Flash。

I've run into a problem that I need help with. 我遇到了需要帮助的问题。 All images that I work with are raster images. 我使用的所有图像都是光栅图像。 When I scale an image or make changes to it such as recoloring it, I make a call to the server to create the new image and then I need to be able to replace the existing image on the canvas. 缩放图像或对其进行更改(例如重新着色)时,我会调用服务器来创建新图像,然后需要能够替换画布上的现有图像。

The only way I've been able to figure out how to do it is by using the canvas method getActiveObject(). 我能够弄清楚如何做的唯一方法是使用画布方法getActiveObject()。 This works fine, but if you have multiple objects selected, you don't know which one to update. 这可以正常工作,但是如果选择了多个对象,则不知道要更新哪个对象。

When the image is originally added to the canvas, I'm attaching an onModified event handler to it. 当图像最初添加到画布上时,我在上面附加了onModified事件处理程序。 Only when the scale of the image changes, do I call the server side script to generate the new image. 仅当图像的比例改变时,我才调用服务器端脚本来生成新图像。 The code I'm currently using is listed below. 下面列出了我当前使用的代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks 谢谢

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


    }
});

Not sure if I've read it correctly, but wouldn't you be able to add an extra property to the fabric object when it is created indicating what it is and then use that to figure out which object to remove? 不知道我是否正确阅读了它,但是在创建Fabric对象时,您是否能够向其添加一个额外的属性,指示其是什么,然后使用该属性找出要删除的对象?

eg in the above, you'd do sprite.objectName = "tshirtSleeves", then when you get new t-shirt sleeves, get the fabric object with objectName = "tshirtSleeves" and remove that. 例如,在上面,您将执行sprite.objectName =“ tshirtSleeves”,然后在获得新的T恤袖子时,使用objectName =“ tshirtSleeves”获​​取布料对象并将其删除。

You could loop through all of the objects on the canvas, check that it's an image, and then make your image changes there as needed. 您可以遍历画布上的所有对象,检查它是否为图像,然后根据需要更改图像。

This option of course has it's possible efficiency issues if you have a lot of non-image objects on the canvas. 如果您在画布上有很多非图像对象,那么此选项当然会带来效率问题。 But it's an option. 但这是一个选择。

You do need to do a renderAll after this of course. 当然,您需要在此之后执行一次renderAll。

var objects = document.getElementById('canvasId').fabric._objects;

jQuery.each( objects, function( key, eachObject ) {
  if( eachObject.type == "image" ) {
    //Perform scale and image replacement as needed here
  }
});

$scope.canvas.renderAll();

Actually turned out to be pretty simple. 实际上结果非常简单。 Just pass the new image url to the setSrc method of the image object as below. 只需将新的图像URL传递给图像对象的setSrc方法,如下所示。

sprite.setSrc(previewPath, function (img) {
     sprite.scale(1);
     $scope.canvas.renderAll();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM