使用Canvas将不透明像素变成白色
我正在寻找一种方法来拍摄图像(徽标、应用程序图标等)并使用 javascript/canvas 将它们转换为白色(不包括透明度)。
这是我想要的示例(显然使用静态图像): http://jsfiddle.net/4ubyj/
I'm looking for a way to take images (logos, app icons, etc.) and convert them to white (excluding transparency) using javascript/canvas.
Here's an example of what I'd like (using static images, obviously):
http://jsfiddle.net/4ubyj/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
canvas API 具有专门用于“仅在原始图像中不透明的像素上绘制”之类的合成方法。这比弄乱图像数据容易得多。
jsFiddle 示例(现在带有内嵌图像)
向@WilliamVanRensselaer 的初始小提琴致敬。
您想要的复合操作是
source-in
,这意味着“仅绘制我试图绘制的不透明部分,这些部分位于正在绘制的图像中的不透明像素之上。”HTML:
Javascript:
参考
The canvas API has compositing methods specifically for things like "draw only on pixels that are not transparent in the original image." This is much easier than messing with the image data.
jsFiddle example (now with inlined image)
hat tip to @WilliamVanRensselaer's initial fiddle.
The composite operation you want is
source-in
, which means "draw the opaque parts of what I'm trying to paint only where they are on top of opaque pixels in the image being drawn upon."HTML:
Javascript:
reference
这里有一些可以帮助您入门的东西。如果 alpha 不为零,这基本上会将像素更改为白色,但如果您愿意,您可以进行修改。
示例: http://jsfiddle.net/Q27Qc/
Here's something to get you started. This basically changes the pixel to white if the alpha is not zero, you can make modifications if you want though.
Example: http://jsfiddle.net/Q27Qc/