简体   繁体   English

HTML5 Canvas 使黑色透明

[英]HTML5 Canvas Make Black Transparent

I have a large amount of images with a black background, here is one for example:我有大量带有黑色背景的图像,例如:

在此处输入图像描述

Is it possible through Javascript to have to ignore the black (#000000) and have it draw on canvas?是否可以通过 Javascript 忽略黑色 (#000000) 并将其绘制在画布上? to appear like this?出现这样的情况?

在此处输入图像描述

Basically trying to take the black pixels and make it an alpha channel.基本上试图获取黑色像素并使其成为 Alpha 通道。

So you'll need to run through all the pixels and change the alpha value of all the black pixels. 因此,您需要遍历所有像素并更改所有黑色像素的Alpha值。

https://jsfiddle.net/0kuph15a/2/ https://jsfiddle.net/0kuph15a/2/

This code creates a buffer (empty canvas) to draw the original image to. 此代码创建一个缓冲区(空画布)以绘制原始图像。 Once thats done, it takes all the pixels of this buffer canvas and then iterates over all the pixels and checks their values. 一旦完成,它将获取此缓冲区画布的所有像素,然后迭代所有像素并检查其值。 I add up the Red, Blue, and Green values to see if they are less then 10 (just incase some pixels aren't pure black 0), but would visually appear black to the human eye. 我将红色,蓝色和绿色值相加,看看它们是否小于10(只是某些像素不是纯黑色0),但在人眼看来会看起来是黑色的。 If it is less then 10 it simply turns the alpha value of that pixel to 0. 如果它小于10,它只是将该像素的alpha值变为0。

var canvas = document.getElementById('main');

var ctx = document.getElementById('main').getContext('2d');
var tile = new Image();
tile.src = document.getElementById('image').src;
tile.onload = function() {
    draw(tile);
}

function draw(img) {
    var buffer = document.createElement('canvas');
    var bufferctx = buffer.getContext('2d');
    bufferctx.drawImage(img, 0, 0);
    var imageData = bufferctx.getImageData(0,0,buffer.width,  buffer.height);
    var data = imageData.data;
    var removeBlack = function() {
        for (var i = 0; i < data.length; i += 4) {
            if(data[i]+ data[i + 1] + data[i + 2] < 10){ 
                data[i + 3] = 0; // alpha
            }
        } 
        ctx.putImageData(imageData, 0, 0); 
    }; 
 removeBlack(); 
} 

You can easily change this line if(data[i]+ data[i + 1] + data[i + 2] < 10){ to if(data[i]+ data[i+1] + data[i+2]==0){ if you know for a fact only #000 blacks are used. if(data[i]+ data[i + 1] + data[i + 2] < 10){ to if(data[i]+ data[i+1] + data[i+2]==0){您可以轻松更改此行if(data[i]+ data[i+1] + data[i+2]==0){如果你知道一个事实,只使用#000黑人。

You can accomplish that using blend modes. 您可以使用混合模式完成此操作。

Change the context globalCompositeOperation to screen , and you can get that result. 将上下文globalCompositeOperation更改为screen ,您可以获得该结果。 Here's an example: 这是一个例子:

 var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); var image = new Image(); image.src = "https://images.blogthings.com/thecolorfulpatterntest/pattern-4.png"; image.onload = function() { context.drawImage(image, 0, 0, canvas.width, canvas.height); var blackImage = new Image(); blackImage.src="http://www.printmag.com/wp-content/uploads/Sillitoe-black-white.gif"; blackImage.onload = function(){ context.globalCompositeOperation = "screen"; context.drawImage(blackImage, 0, 0, canvas.width, canvas.height); } }; 
 <canvas id="canvas" width="300" height="250"></canvas> <hr/> <h1>Images used:</h1> <img src="https://images.blogthings.com/thecolorfulpatterntest/pattern-4.png"/> <img src="http://www.printmag.com/wp-content/uploads/Sillitoe-black-white.gif"/> 

如何将图片保存为.svg文件...从那里你可以更改所有颜色和其他设置

Felipe's answer addressed my issue.费利佩的回答解决了我的问题。 Alpha pixel manipulation does not work (eg, setting every 4th pixel to 0) for preserving alphatransparency with multiple images added into the same context at the same time.阿尔法像素的操作工作(例如,每4个像素设定为0),与加入到相同情况下在同一时间多个图像保存alphatransparency。

eg:例如:

this.ctx1.putImageData(output, 0, 0); // without setting the context's globalCompositeOperation, this image is written over by the next putImage operation
this.ctx1.putImageData(output, 20, 0);

Go here to review the blending options.去这里查看混合选项。 https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation

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

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