简体   繁体   English

画布(带盖bg的属性globalCompositeOperation)

[英]Canvas (property globalCompositeOperation with cover bg)

How to make a mask so that the picture was on the background of another picture? 如何制作遮罩以使图片位于另一张图片的背景上?

I comment out the line because of that it's not working properly http://jsfiddle.net/LZY5A/5 我注释掉该行,因为它无法正常运行http://jsfiddle.net/LZY5A/5

If the example no pictures refresh the page 如果示例中没有图片刷新页面

Code from the jsFiddle: 来自jsFiddle的代码:

var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');

var imageObj = new Image();
var imageObj2 = new Image();
var bg = new Image();
imageObj.src = 'http://codepo8.github.io/canvas-masking/centerblur.png';
imageObj2.src = 'http://codepo8.github.io/canvas-masking/red-panda.jpg';
bg.src = 'http://s5.goodfon.ru/image/391868-3318x2212.jpg';



setInterval(loop, 17);

function loop() {
  // ctx.drawImage(bg, 0, 0, 500,500); // It should be behind

  ctx.drawImage(imageObj, 100, 100, 100,100);
  ctx.globalCompositeOperation = 'source-atop';
  ctx.drawImage(imageObj2, 100, 100, 100,100);
}

You can use destination-over compositing to draw your background behind your knockout effect: 您可以使用目标上方合成在背景效果后面绘制背景:

在此处输入图片说明

Example code and a Demo: http://jsfiddle.net/m1erickson/Uyz7Y/ 示例代码和演示: http : //jsfiddle.net/m1erickson/Uyz7Y/

ctx.drawImage(imgs[0], 100, 100, 100,100);
ctx.globalCompositeOperation = 'source-atop';
ctx.drawImage(imgs[1], 100, 100, 100,100);
ctx.globalCompositeOperation="destination-over";
ctx.drawImage(imgs[2],0,0);
ctx.globalCompositeOperation="source-over";  // reset to default compositing

BTW, your loop is unnecessary... 顺便说一句,您的循环是不必要的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM