代码如下:
<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 == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) {
// change to your new rgb
imagedata.data = 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>
更多网页制作信息请查看: 网页制作 |