我发现这可以使用 Canvas 完成。我发现了这个:画布 - 使用 HTML5/CSS/JS 更改图像的颜色?
第一条评论详细描述了它并提供了这个 jsfiddle 示例:http: //jsfiddle.net/pHwmL/1/
function tintImage(imgElement,tintColor) {
// create hidden canvas (using image dimensions)
var canvas = document.createElement("canvas");
canvas.width = imgElement.offsetWidth;
canvas.height = imgElement.offsetHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(imgElement,0,0);
var map = ctx.getImageData(0,0,320,240);
var imdata = map.data;
// convert image to grayscale
var r,g,b,avg;
for(var p = 0, len = imdata.length; p < len; p+=4) {
r = imdata[p]
g = imdata[p+1];
b = imdata[p+2];
// alpha channel (p+3) is ignored
avg = Math.floor((r+g+b)/3);
imdata[p] = imdata[p+1] = imdata[p+2] = avg;
}
ctx.putImageData(map,0,0);
// overlay filled rectangle using lighter composition
ctx.globalCompositeOperation = "lighter";
ctx.globalAlpha = 0.5;
ctx.fillStyle=tintColor;
ctx.fillRect(0,0,canvas.width,canvas.height);
// replace image source with canvas data
这适用于支持 HTML5 的智能手机/浏览器。