I have a 3 layers of canvas, which I have given a separate id to each.
This is what I am doing.
HTML
<div style="position:relative;">
<canvas id="layer1" style="position: absolute; left: 0; top: 0; z-index: 0;">
</canvas>
<canvas id="layer2" style="position: absolute; left: 0; top: 0; z-index: 1;">
</canvas>
<canvas id="layer3" style="position: absolute; left: 0; top: 0; z-index: 1;">
</canvas>
</div>
I can draw images on layer1
JS
var canvas = document.getElementById('layer1');
var context = canvas.getContext('2d');
context.canvas.width = window.innerWidth;
context.canvas.height = window.innerHeight*0.7;
var imageObj = new Image();
var imageObj1 = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0,context.canvas.width,context.canvas.height*0.9);
context.drawImage(imageObj1, x1, y1,6,canvasHeight);
};
imageObj1.src='vertical.png';
imageObj.src = 'horizontal.png';
How will I draw images on all the layers and create single canvas.toDataUrl ?
I have a 3 layers of canvas, which I have given a separate id to each.
This is what I am doing.
HTML
<div style="position:relative;">
<canvas id="layer1" style="position: absolute; left: 0; top: 0; z-index: 0;">
</canvas>
<canvas id="layer2" style="position: absolute; left: 0; top: 0; z-index: 1;">
</canvas>
<canvas id="layer3" style="position: absolute; left: 0; top: 0; z-index: 1;">
</canvas>
</div>
I can draw images on layer1
JS
var canvas = document.getElementById('layer1');
var context = canvas.getContext('2d');
context.canvas.width = window.innerWidth;
context.canvas.height = window.innerHeight*0.7;
var imageObj = new Image();
var imageObj1 = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0,context.canvas.width,context.canvas.height*0.9);
context.drawImage(imageObj1, x1, y1,6,canvasHeight);
};
imageObj1.src='vertical.png';
imageObj.src = 'horizontal.png';
How will I draw images on all the layers and create single canvas.toDataUrl ?
Share Improve this question edited Sep 18, 2015 at 12:27 ROMANIA_engineer 56.7k30 gold badges209 silver badges205 bronze badges asked Jul 9, 2013 at 17:07 BeginnerBeginner 1,4242 gold badges21 silver badges43 bronze badges 3- possible duplicate of How can I save div as image at client side where div contains one or more than one HTML5 canvas elements? – Jeff B Commented Jul 9, 2013 at 17:16
- but i am unable to display images on layer 2 and layer 3 – Beginner Commented Jul 9, 2013 at 17:24
-
You would do that just like with
layer1
. You need another context based onlayer2
, etc.var canvas2 = document.getElementById('layer2'); var context2 = canvas.getContext('2d');
– Jeff B Commented Jul 9, 2013 at 18:07
1 Answer
Reset to default 6To draw on all three “layers” you must create contexts for all layers
// references to layer1
var canvas1 = document.getElementById('layer1');
var context1 = canvas1.getContext('2d');
// references to layer2
var canvas2 = document.getElementById('layer2');
var context2 = canvas2.getContext('2d');
// references to layer3
var canvas3 = document.getElementById('layer3');
var context3 = canvas3.getContext('2d');
Then after you’re done drawing on all layers, merge them (here merged onto layer1):
// merge all layers onto layer1
context1.drawImage(canvas2,0,0);
context1.drawImage(canvas3,0,0);
And finally save the merged canvas to an image and set your img src to that image.
// save the merged drawings as an image
// and set the img element src to that merged image
var img=new Image();
img.onload=function(){
document.getElementById("results").src=img.src;
}
img.src=canvas1.toDataURL();
Here is code and a Fiddle: http://jsfiddle/m1erickson/fhjwY/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery./jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
// references to layer1
var canvas1 = document.getElementById('layer1');
var context1 = canvas1.getContext('2d');
// references to layer2
var canvas2 = document.getElementById('layer2');
var context2 = canvas2.getContext('2d');
// references to layer3
var canvas3 = document.getElementById('layer3');
var context3 = canvas3.getContext('2d');
// draw stuff on the layers
context1.fillStyle="red";
context2.fillStyle="blue";
context3.fillStyle="green";
context1.fillRect(20,20,50,50);
context2.fillRect(50,50,50,50);
context3.fillRect(80,80,50,50);
// merge all layers onto layer1
context1.drawImage(canvas2,0,0);
context1.drawImage(canvas3,0,0);
// save the merged drawings as an image
// and set the img element src to that merged image
var img=new Image();
img.onload=function(){
document.getElementById("results").src=img.src;
}
img.src=canvas1.toDataURL();
}); // end $(function(){});
</script>
</head>
<body>
<p>Draw rects on 3 canvases</p>
<p>Merge all drawings to 1st canvas</p>
<p>Create an image from merged drawings</p>
<p>Set the image element with that merged image</p>
<canvas id="layer1" width=150 height=150></canvas>
<canvas id="layer2" width=150 height=150></canvas><br>
<canvas id="layer3" width=150 height=150></canvas>
<img id="results" width=150 height=150>
</body>
</html>