【发布时间】:2019-12-18 16:29:20
【问题描述】:
我有 2 张海盗的图像,一张带有形状,一张带有海盗本身。我想将这 2 张图片合并在一起,以创建一个带有透明边框的图像,以便在我的画布中使用。唯一的问题是我找不到真正管理它的方法。
图像本身自然根本不透明,所以我在 javascript 中使用复合选项,尝试使用“更轻”选项使其透明,但我找不到摆脱周围白色边框的方法图片。
这是我目前拥有的代码的一部分:
ctx.canvas.width = pirate_shape.width;
ctx.canvas.height = pirate_shape.height;
ctx.clearRect(0, 0, pirate_shape.width, pirate_shape.height);
// Draw the shape in reversed colors so i can draw the picture inside the shape.
ctx.drawImage(pirate_shape, 0, 0);
ctx.globalCompositeOperation='difference';
ctx.fillStyle='white';
ctx.fillRect(0, 0, pirate_shape.width, pirate_shape.height);
// Draw the pirate itself
ctx.globalCompositeOperation = 'lighter';
ctx.drawImage(pirate, 0, 0);
完整代码:https://jsfiddle.net/0kbj2Leg/
我试图得到的结果:
我使用的两张图片:
(海盗) https://imgur.com/8R1qutU
(形状) https://imgur.com/qemOzU2
你们知道获得这个结果的方法吗?
谢谢!
【问题讨论】:
标签: javascript html image-processing canvas