简体   繁体   English

使用CSS3或JS叠加透明黑色轮廓PNG的可见区域

[英]Overlay visible areas of transparent black silhouette PNG with pattern using CSS3 or JS

I'm looking for a way to overlay the visible areas of a transparent black silhouette PNG file with a certain pattern. 我正在寻找一种方法来覆盖具有特定图案的透明黑色轮廓PNG文件的可见区域。

Is there a CSS, JavaScript, mixed solution for this? 是否有CSS,JavaScript,混合解决方案?

Example: I have one image of a weapon silhoutte and a set of camo patterns which I want to lay over the weapon. 示例:我有一个武器silhoutte和一组迷彩图案的图像,我想放在武器上。

Demo: In Photoshop the same result is givin when selecting layer mask > overlay. 演示:在Photoshop中,选择图层蒙版>叠加时,同样的结果是givin。

Ps: my question is similar to this thread: Silhouette a PNG image using CSS except I am looking for the exact opposite. Ps:我的问题类似于这个主题: 使用CSS剪影PNG图像,除了我正在寻找完全相反的情况。

You can do this using canvas with globalCompositeOperation set to destination-in. 您可以使用canvas将globalCompositeOperation设置为destination-in来执行此操作。 For example 例如

var canvas = document.createElement('canvas');
canvas.width = 250;
canvas.height = 250;

var canvas_context = canvas.getContext("2d");

var img = new Image();
img.onload = function(){
    var msk = new Image();
    msk.onload = function(){
        canvas_context.drawImage(img, 0, 0);
        canvas_context.globalCompositeOperation = "destination-in";
        canvas_context.drawImage(msk, 0, 0);
        canvas_context.globalCompositeOperation = "source-over";
    };

    msk.src = 'silhouette.png';
}
img.src = 'pattern.jpg';

document.body.appendChild(canvas);

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

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