简体   繁体   English

如何使用fabricjs将多个图像添加到画布

[英]How to add multiple image to canvas using fabricjs

I have read several questions/answers about this topic, but I can't seem to find my solution. 我已经阅读了有关此主题的几个问题/答案,但是似乎找不到我的解决方案。 So sorry if you guys find this question duplicated. 非常抱歉,如果您发现此问题重复。

Here is my situation; 这是我的情况; Let's say I have a menu for user to click on it and it will display the image of that selected item on canvas, also user can resize/drag/drop that image as they want. 假设我有一个菜单供用户单击,它将在画布上显示该选定项目的图像,用户也可以根据需要调整/拖放该图像的大小。 Right now I can make it works only one time with this code 现在,我可以使用此代码使其仅运行一次

addAction(selectedAction: any) {
  var canvas = new fabric.Canvas('c');
  var imgObj = new Image();
  imgObj.src = selectedAction.image;
  imgObj.onload = function() {
    var imgInstance = new fabric.Image(imgObj, {
        left: 200,
        top: 200,
      });
    canvas.add(imgInstance);
  }

The problem is when user click another item in the menu, the previous item that was on the canvas disappear, instead the current item display on the canvas. 问题是当用户单击菜单中的另一个项目时,画布上的上一个项目消失了,而当前项目显示在画布上。 But if I click on that image, it will disappear and the previous image display on the canvas. 但是,如果我单击该图像,它将消失并且先前的图像显示在画布上。

I want them BOTH stay on the canvas at the same time. 我希望他们都同时留在画布上。 Please advice. 请指教。

Real quick answer: http://jsfiddle.net/8yf15nqp/1/ . 真正的快速答案: http : //jsfiddle.net/8yf15nqp/1/

var canvas = ...// setup canvas

function addImage (url) {
    fabric.Image.fromURL(url, function (img) {
        // deal with image
       canvas.add(img);
    });
}

Setup a function to handle adding and then call it when you want to add a new image. 设置一个函数来处理添加,然后在要添加新图像时调用它。 I'd assume you'll want to pass the url to the function and do other stuff, but this is a pretty stripped down example. 我假设您将要将该URL传递给该函数并执行其他操作,但这是一个精简的示例。

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

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