Have I possibility to draw image in canvas, with layers? Particularly using fabric.js or literallycanvas.js ? Will be appreciate for any information or examples, ty
Asked
Active
Viewed 1,904 times
-2
-
Multiple layers requires multiple canvases. Each canvas is a layer :) – Reinstate Monica Cellio Jul 07 '16 at 13:45
-
can I then merge it in one image? – Viktor Moroz Jul 07 '16 at 14:08
-
FabricJS will let you (effectively) apply a z-index to each of the shapes it draws. See this previous [Q&A](http://stackoverflow.com/questions/15032497/layering-canvas-objects-in-fabric-js). – markE Jul 07 '16 at 19:45
-
Possible duplicate of [html5 - canvas element - Multiple layers](http://stackoverflow.com/questions/3008635/html5-canvas-element-multiple-layers) – Laurel Jul 08 '16 at 23:21
1 Answers
0
Layers are software abstractions. They define groups of items that are processed in a specific order (layered).
Anything can be layered, so drawing on the canvas it is easy to make layers.Group all the objects to be drawn in each layer together. Then render all objects in each layer together in the order defined by the layers.
A very simple layer example
var layers = []; // holds layers
function addLayer(order){ /// creates and adds a layer
var l;
layers.push(l = {
order : order,
items : [],
render : function(){
this.items.forEach(function(img){
ctx.drawImage(img.bitmap,img.x,img.y);
});
});
return l;
}
// some items for the layers
var img1 = {
bitmap : new Image("URL"),
x : 100,
y : 200,
}
var img2 = {
bitmap : new Image("URL"),
x : 300,
y : 200,
}
// create a layer order 1
var lay1 = AddLayer(1);
// add some items to lay1
lay1.items.push(img1);
lay1.items.push(img2);
// add a second layer order 0
var lay2 = AddLayer(0);
lay1.items.push(img3);
lay1.items.push(img4);
// sort layers by order
layers.sort(function(a,b){return a.order-b.order;});
// process each layer in the sorted order
layers.forEach(function(layer){
layer.render();
});

Blindman67
- 51,134
- 11
- 73
- 136