clipping image to image using canvas -
has here have done clipping image within image? i've seen far clipping on canvas regular shapes(rectangle, circle, etc...). nice if have done it.
p.s. fabric.js or regular canvas.
sure, can use compositing draw second image first image exists:
ctx.drawimage(image1,0,0); // creates 'mask' ctx.globalcompositeoperation='source-in'; ctx.drawimage(image2,0,0); // image draws inside mask illustration: house image drawn first , second grass image drawn house pixels exist:

example code demo: http://jsfiddle.net/m1erickson/r71d8d8b/
<!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> <style> body{ background-color: ivory; } canvas{border:1px solid red;} </style> <script> $(function(){ var canvas=document.getelementbyid("canvas"); var ctx=canvas.getcontext("2d"); // put paths images in imageurls[] var imageurls=[]; // push image urls! imageurls.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house100x100.png"); imageurls.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/mower_start.png"); // loaded images placed in images[] var imgs=[]; var imagesok=0; loadallimages(start); function loadallimages(callback){ (var i=0; i<imageurls.length; i++) { var img = new image(); imgs.push(img); img.onload = function(){ imagesok++; if (imagesok>=imageurls.length ) { callback(); } }; img.onerror=function(){alert("image load failed");} img.crossorigin="anonymous"; img.src = imageurls[i]; } } function start(){ // imgs[] array holds loaded images // imgs[] in same order imageurls[] ctx.drawimage(imgs[0],50,50); ctx.globalcompositeoperation='source-in'; ctx.drawimage(imgs[1],0,0); } }); // end $(function(){}); </script> </head> <body> <h4>second grass image drawn where<br>house pixels existed<br>(uses 'source-in' compositing)</h4> <canvas id="canvas" width=300 height=300></canvas> </body> </html>
Comments
Post a Comment