-
Notifications
You must be signed in to change notification settings - Fork 32
/
canvasmask.js
34 lines (27 loc) · 992 Bytes
/
canvasmask.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(function(){
var imagecanvas = document.createElement('canvas');
var imagecontext = imagecanvas.getContext('2d');
/* uncomment do see the canvas to debug
document.body.appendChild(imagecanvas);
*/
window.addEventListener('load', function(){
[].forEach.call(document.querySelectorAll('.mask'), function(img){
var newImg = document.createElement('img');
newImg.src = img.src;
newImg.onload = function() {
var width = newImg.width;
var height = newImg.height;
var mask = document.createElement('img');
mask.src = img.getAttribute('data-mask');
mask.onload = function() {
imagecanvas.width = width;
imagecanvas.height = height;
imagecontext.drawImage(mask, 0, 0, width, height);
imagecontext.globalCompositeOperation = 'source-in';
imagecontext.drawImage(img, 0, 0);
img.src = imagecanvas.toDataURL();
}
}
});
}, false);
})();