javascript - HTML5 Canvas; Image clipping and multiple images -


i'm new canvas , i'm having issues.

i'm trying accomplish 1 end goal: display 2 images in single canvas; 1 image background, other image clipped png , appears on top.

i'm part way there i've hit wall , don't know how past it.

i've create jsfiddle @ http://jsfiddle.net/jhp79yg9/

      function loadimages(sources, callback) {          var images = {};          var loadedimages = 0;          var numimages = 0;          // num of sources          for(var src in sources) {            numimages++;          }          for(var src in sources) {            images[src] = new image();            images[src].onload = function() {              if(++loadedimages >= numimages) {                callback(images);              }            };            images[src].src = sources[src];          }        }        var canvas = document.getelementbyid('canvas');        var context = canvas.getcontext('2d');          var sources = {          img1: 'https://scontent-iad3-1.cdninstagram.com/hphotos-xaf1/t51.2885-15/11351598_1454928098145798_1274636321_n.jpg',          img2: 'https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-15/11379733_1139595366067106_273993739_n.jpg',          mask: 'http://img12.deviantart.net/65e4/i/2013/003/6/6/png_floating_terrain_by_moonglowlilly-d5qb58m.png'        };          loadimages(sources, function(images) {          context.drawimage(images.img2, 0, 0, 240, 240);                    context.drawimage(images.mask, 20, 95, 500, 349);          context.globalcompositeoperation = 'source-in';          context.drawimage(images.img1, 0, 0, 540, 540);          });
<canvas id="canvas" width="540" height="540"></canvas>

in example i've reduced size of first image 140x140 540x540 can tell if it's messing up, is.

what's happening it's not showing second image, it's showing 'img1' variable twice instead of showing 'img2' @ all.

can offer assistance?

i can't tell, because it's rendering same image, in foreground , in background. there appreciated (the clipped image should in foreground).

thanks!

another approach cut out mask current content , draw background behind current content:

loadimages(sources, function(images) {     context.drawimage(images.img2, 0, 0, 540, 540);     context.globalcompositeoperation = 'destination-out';     context.drawimage(images.mask, 20, 95, 500, 349);     context.globalcompositeoperation = 'destination-atop';     context.drawimage(images.img1, 0, 0, 540, 540); }) 

http://jsfiddle.net/jhp79yg9/6/


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -