简体   繁体   中英

issue with image object in fabric.js

I am complete newbie in fabric.js . I need to load several images from URL in canvas and add them as image object so I can set their z index.

I tried using Image.fromURL but it did not return object so I can't set their z index. New fabric.Image take images from page so I am not getting results from it.

This is what I have done so far

var canvas = new fabric.Canvas('myCanvas');
var front_wall = fabric.Image.fromURL('img/bedroom1-front/front_wall.png', 
    function(img) {
        img.scale(0.65);
        canvas.add(img);
    }
);

I add five more images same way.

Can fabric.Image.fromURL return the image object (by using any attribute) ?

So I can change their z-index after loading.

Sorry I don't have jsfiddle to show.

Welcome to Stack Overflow,

Please check this fiddle

I added some comments in that fiddle. Code is below:

var srcImg = "http://fabricjs.com/lib/pug.jpg";
var canvas = new fabric.Canvas('c');
var icon = null;
var image = fabric.Image.fromURL(srcImg, function(oImg) {
                    oImg.set({width: oImg.width / 3,
                              height: oImg.height / 3,
                              left: 100,
                              top: 100,
                              originX: 'center',
                              originY: 'center',
                              selectable: false,
                              transparentCorners: false});

          //magic strats here to make active Image and you can control image object here
                    canvas.setActiveObject(oImg);
                    icon = canvas.getActiveObject();
                    icon.hasBorders = false;
                    icon.hasControls = false;                   
                    canvas.add(oImg);
                });

//if you want to control outside somewhere you can do this:
setTimeout(function(){
    icon.set({'top': 200, 'left': 200});
  canvas.renderAll();
}, 1500);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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