欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

html5 更新图片颜色示例代码

程序员文章站 2023-11-05 23:23:58
本文为为大家介绍了html5 更新图片颜色的具体实现,喜欢的朋友可以练练手... 14-07-29...

复制代码
代码如下:

<canvas id="c1" width="1220" height = "880" style="background: none repeat scroll 0% 0% transparent; "></canvas>
<script>
var cid = "c1";
var image = new image();
image.src = "eye/item_eye_1.png";
image.onload = function () {
recolorimage(cid,image, 0, 0, 0, 255, 0, 0);
}
function recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) {
var c = document.getelementbyid(c);
var ctx = c.getcontext("2d");
var w = img.width;
var h = img.height;
c.width = w;
c.height = h;
// draw the image on the temporary canvas
ctx.drawimage(img, 0, 0, w, h);
// pull the entire image into an array of pixel data
var imagedata = ctx.getimagedata(0, 0, w, h);
// examine every pixel,
// change any old rgb to the new-rgb
for (var i = 0; i < imagedata.data.length; i += 4) {
// is this pixel the old rgb?
if (imagedata.data[i] == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) {
// change to your new rgb
imagedata.data[i] = newred;
imagedata.data[i + 1] = newgreen;
imagedata.data[i + 2] = newblue;
}
}
// put the altered data back on the canvas
ctx.putimagedata(imagedata, 0, 0);
}
</script>