代码如下:3 d5 R* n0 n/ S
<canvas id=c1 width=1220 height = 880 style=background: none repeat scroll 0% 0% transparent; ></canvas> 0 \3 B/ Z( h& D w8 ?+ T
<script> / O' q5 S; `& O' u' H6 y3 a
var cid = c1;
% p' P8 j7 D, c: T0 cvar image = new image();
2 Y$ A& N, \' `3 x ^image.src = eye/item_eye_1.png;
4 E$ r1 \$ |5 h' p0 fimage.onload = function () { 1 Z _* o' o3 }% r( O( x
recolorimage(cid,image, 0, 0, 0, 255, 0, 0);
' h! y. r: G3 L; J: W' A" t} 8 J e6 Q4 E: c4 [5 }) T# \; p
function recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) {
" O" ^" u& j9 N+ U( r. ^8 tvar c = document.getelementbyid(c);
/ X) Y$ \$ \: | K, r& {0 q0 ]var ctx = c.getcontext(2d); ' X& |6 w" Y4 ^ [1 c" g( z
var w = img.width;
' C9 W0 k1 r1 f: p; v L. zvar h = img.height; * V: [+ S- k0 M! |
c.width = w; " w1 S( O* | L0 T2 G. u2 I. e
c.height = h; : r U0 [; s1 e0 Z1 A
// draw the image on the temporary canvas $ F- i4 V% Z9 b. f
ctx.drawimage(img, 0, 0, w, h); ! K: `2 E6 U' z+ L) e7 j' K5 g6 p
// pull the entire image into an array of pixel data 9 R) L T; |3 |; ~" h8 Q1 o( r. K; d
var imagedata = ctx.getimagedata(0, 0, w, h);
{+ ~! e. A/ k3 Z; u4 l// examine every pixel,
7 g3 Y# R* H4 G9 r% B2 i// change any old rgb to the new-rgb ; I7 l3 g4 B" i& P; h& `
for (var i = 0; i < imagedata.data.length; i += 4) { U: C4 k- X" I) J5 O
// is this pixel the old rgb?
/ |7 K. ]( L8 Fif (imagedata.data == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) { 4 u' K. K& Q( F3 K
// change to your new rgb 8 M+ v4 C+ [4 o; b4 r) [1 x
imagedata.data = newred;
) L* A1 H0 u( b1 e2 Ximagedata.data[i + 1] = newgreen; N1 _: J' g& l& M
imagedata.data[i + 2] = newblue; - X8 c: k3 t6 V% ~& O/ Q [4 I3 X: c
}
3 f- Q, c, t' i; \) v; s} 9 M% A: |* a: X7 _3 u6 @. E! l
// put the altered data back on the canvas
6 h3 n Y$ d2 U5 t2 x: ?) D, jctx.putimagedata(imagedata, 0, 0);
, y/ a5 k& ?5 P- j8 ~! e$ R/ n1 h) H}
/ a0 k3 {; k0 {2 @3 h0 g& v, N9 d</script> - Z x& M0 t- u- Q: X" Y
( A: h! _4 i( _' V# D& Z更多网页制作信息请查看: 网页制作 |