当前位置: 移动技术网 > IT编程>网页制作>Html5 > html5 更新图片颜色示例代码

html5 更新图片颜色示例代码

2019年07月25日  | 移动技术网IT编程  | 我要评论

安华·依布拉欣,2012sbs歌谣大战exo,鲁妹


复制代码
代码如下:

<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>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网