简体   繁体   English

图像 png 颜色 css 或 html 或 javascript

[英]Image png color css or html or javascript

I have this image in my website, is a png.I want to know if there is anyway with HTML5, javascript or css to change the color of the image?我的网站上有这张图片,是一个 png。我想知道是否有 HTML5、javascript 或 css 来更改图像的颜色? At least the image change to white (invert color black to white, not the transparency).至少图像变为白色(将黑色反转为白色,而不是透明度)。

Thanks in advance提前致谢

You cannot do this in plain HTML or CSS.不能在普通的 HTML 或 CSS 中执行此操作。 You can do this with JS/HTML Canvas, but there is probably a much easier and more compatible solution than JS/HTML Canvas for performing image manipulation.可以使用 JS/HTML Canvas 执行此操作,但可能有比 JS/HTML Canvas 更容易和更兼容的解决方案来执行图像处理。

Depending on what you are trying to do, you may just want to create a second image that you swap out with the first.根据您要执行的操作,您可能只想创建第二个图像并与第一个图像交换。 There are also some decent image manipulation packages in server-side languages like PHP, if you need to do something more dynamic.如果您需要做一些更动态的事情,还有一些像 PHP 这样的服务器端语言中不错的图像处理包。

If you are set on using js, the code below is modified from your example .如果您打算使用 js,下面的代码是根据您的示例修改的。

Using your example:使用您的示例:

window.onload = function(){
    var imageObj = new Image();
    imageObj.onload = function(){
        drawImage(this);
    };
    imageObj.src = "darth-vader.jpg";
};

function drawImage(imageObj){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");

    var destX = 69; //update these to set the image position
    var destY = 50;

    context.drawImage(imageObj, destX, destY);

    var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
    var data = imageData.data;

    for (var i = 0, n = data.length; i < n; i += 4) {
        if(data[i] == 0 && data[i+1] == 0 && data[i+2] ==0){ //if black, ie. red, green, and blue are all 0
            //switch to white
            data[i] = 255; //red
            data[i+1] = 255; //green
            data[i+2] = 255; //blue
        }
        // i+3 is alpha (the fourth element)
    }

    // overwrite original image
    context.putImageData(imageData, 0, 0);
}

You haven't explained why you aren't just using Photoshop to do it, but I assume you have your reasons.你还没有解释为什么你不只是使用 Photoshop 来做,但我认为你有你的理由。

Well wanted to support all... but for now Firefox, Chrome will be nice.很想支持所有...但是现在 Firefox,Chrome 会很好。 Latest versions, would be nice... I guess with canvas HTML5 is possible.最新版本,会很好......我想 canvas HTML5 是可能的。

I'm not sure how well it works with transparent .png s, but you could try Pixastic .我不确定它与透明.png的效果如何,但您可以尝试Pixastic

Here's a demo of the "Invert" effect: http://www.pixastic.com/lib/docs/actions/invert/这是“反转”效果的演示: http://www.pixastic.com/lib/docs/actions/invert/

You've lucked out with the browser support for "Invert" - it works in even IE:你已经幸运地获得了浏览器对“反转”的支持——它甚至可以在 IE 中使用:

Although a few of the effects in Pixastic are simulated in IE with proprietary filters, most actions and effects will not work without a canvas enabled browser.尽管 Pixastic 中的一些效果是在 IE 中使用专有过滤器模拟的,但如果没有启用 canvas 的浏览器,大多数动作和效果将无法工作。 Please consider using either Firefox, Opera or Safari请考虑使用 Firefox、Opera 或 Safari

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

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