代码如下:
( Z0 t$ r' L, W4 P<canvas id=c1 width=1220 height = 880 style=background: none repeat scroll 0% 0% transparent; ></canvas> % b. h4 o4 g9 i( s3 \% z8 Q
<script>
) t6 U7 h: h3 Dvar cid = c1;
( X! f5 f* A' v8 Ovar image = new image(); $ u( {! A! l2 ]) i& j. \2 p( G! j3 V
image.src = eye/item_eye_1.png; , ~0 Q; v' s$ I, O$ b0 x
image.onload = function () { 2 i6 @. k3 n) t: `% P0 ^2 D
recolorimage(cid,image, 0, 0, 0, 255, 0, 0);
% a X. c: [: Z F% p( q} ) O- `0 Q. G' x. y
function recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) { 7 h! M; A) L; J- x& S* q
var c = document.getelementbyid(c);
# \+ V! l- o: T- i: _. vvar ctx = c.getcontext(2d);
|3 s6 t" K7 W& Z8 Y4 ~var w = img.width; 3 b2 V' E( y# C
var h = img.height;
: V& {! e: ?& A5 Z! R0 R& S' lc.width = w; 2 x" _' X2 v& U% F1 U) J
c.height = h;
& G) G" z" x6 A y9 y5 Q// draw the image on the temporary canvas * j6 Q- U& `# j! O5 ?4 B0 L
ctx.drawimage(img, 0, 0, w, h);
% _0 s+ c8 I; F9 i& t1 d// pull the entire image into an array of pixel data
, |4 [) S; [. T( m1 Jvar imagedata = ctx.getimagedata(0, 0, w, h);
! c, e5 V( I7 S// examine every pixel,
8 c( a- D/ b4 g: v" ?// change any old rgb to the new-rgb
; E! R; H; @& C4 p4 kfor (var i = 0; i < imagedata.data.length; i += 4) { # s/ S% b# }+ t
// is this pixel the old rgb? / \* F- S* [" Z7 @, k- r% w: T
if (imagedata.data == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) {
1 Y1 h" i8 C. c7 E// change to your new rgb 2 z- V; m3 Z; o+ A$ I, t6 T
imagedata.data = newred;
6 \. {; D. V1 r: k7 n; u" A6 |imagedata.data[i + 1] = newgreen;
9 j/ p0 i* p/ w5 h/ V' \imagedata.data[i + 2] = newblue; : `: j6 I2 W7 s& M
}
( h6 L2 B5 n& o1 g7 ]( `. J1 j; k}
; s7 p$ c; o6 d3 }- T7 \. _// put the altered data back on the canvas : {: P. ~& M; t8 }8 g) ?
ctx.putimagedata(imagedata, 0, 0);
$ I0 w( } A+ T+ h; |1 S}
8 I% {+ u" Z8 \8 |</script>
) s7 j" ]% V9 F
6 N6 r* T, B% s3 j( C( g2 i更多网页制作信息请查看: 网页制作 |