javascript - Multi canvas layer paint application in HTML5 -
i building paint tool eraser option using html5 canvas. canvas loads backgroung image , on image users can draw paintings. on using eraser tool painted part should erased(not part of image). know must use more 1 canvas it. please me code example if can. in advance.
yes, can use 2 canvases support both indestructible background , sketching foreground.
btw, background doesn't need canvas. image or div content.
a quick illustration
you can wrap background , sketching canvases in wrapper div:
<div id="wrapper"> <canvas id="canvas2" width=300 height=200></canvas> <canvas id="canvas" width=300 height=200></canvas> </div>
then use css position sketching canvas directly on top of background canvas:
body{ background-color: ivory; } #wrapper{ position:relative; width:300px; height:200px; } #canvas,#canvas2{ position:absolute; top:0px; left:0px; border:1px solid green; width:100%; height:100%; } #canvas2{ border:2px solid red; }
here small illustration app uses background canvas display blue background.
the sketching canvas (on top) used draw , erase.
here's code , fiddle: http://jsfiddle.net/m1erickson/g6jak/
<!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.com/jquery.min.js"></script> <!--[if lt ie 9]><script type="text/javascript" src="../excanvas.js"></script><![endif]--> <style> body{ background-color: ivory; } #wrapper{ position:relative; width:300px; height:200px; } #canvas,#canvas2{ position:absolute; top:0px; left:0px; border:1px solid green; width:100%; height:100%; } #canvas2{ border:2px solid red; } </style> <script> $(function(){ var canvas2=document.getelementbyid("canvas2"); var ctx2=canvas2.getcontext("2d"); ctx2.fillstyle="skyblue"; ctx2.linewidth=5; ctx2.fillrect(0,0,canvas2.width,canvas2.height); var canvas=document.getelementbyid("canvas"); var ctx=canvas.getcontext("2d"); ctx.linecap = "round"; ctx.linejoin = "round"; ctx.linewidth=3; var lastx; var lasty; var mousex; var mousey; var canvasoffset=$("#canvas").offset(); var offsetx=canvasoffset.left; var offsety=canvasoffset.top; var ismousedown=false; function handlemousedown(e){ mousex=parseint(e.clientx-offsetx); mousey=parseint(e.clienty-offsety); // put mousedown stuff here lastx=mousex; lasty=mousey; ismousedown=true; } function handlemouseup(e){ mousex=parseint(e.clientx-offsetx); mousey=parseint(e.clienty-offsety); // put mouseup stuff here ismousedown=false; } function handlemouseout(e){ mousex=parseint(e.clientx-offsetx); mousey=parseint(e.clienty-offsety); // put mouseout stuff here ismousedown=false; } function handlemousemove(e){ mousex=parseint(e.clientx-offsetx); mousey=parseint(e.clienty-offsety); // put mousemove stuff here if(ismousedown){ ctx.beginpath(); if(mode=="pen"){ ctx.globalcompositeoperation="source-over"; ctx.moveto(lastx,lasty); ctx.lineto(mousex,mousey); ctx.stroke(); }else{ ctx.globalcompositeoperation="destination-out"; ctx.arc(lastx,lasty,5,0,math.pi*2,false); ctx.fill(); } lastx=mousex; lasty=mousey; } } $("#canvas").mousedown(function(e){handlemousedown(e);}); $("#canvas").mousemove(function(e){handlemousemove(e);}); $("#canvas").mouseup(function(e){handlemouseup(e);}); $("#canvas").mouseout(function(e){handlemouseout(e);}); var mode="pen"; $("#pen").click(function(){ mode="pen"; }); $("#eraser").click(function(){ mode="eraser"; }); }); // end $(function(){}); </script> </head> <body> <div id="wrapper"> <canvas id="canvas2" width=300 height=200></canvas> <canvas id="canvas" width=300 height=200></canvas> </div> <button id="pen">pen</button> <button id="eraser">eraser</button> </body> </html>
Comments
Post a Comment