代码如下:
4 P% p, z& n: m7 \5 a$ K1 ^$ f, ~<canvas id=c1 width=1220 height = 880 style=background: none repeat scroll 0% 0% transparent; ></canvas> 5 i8 B B1 i w7 _/ h9 O
<script> & z- r( b0 T: p
var cid = c1; ) Z2 Z- p; e. r. D
var image = new image(); ( e0 e* g1 p! E1 ~; R+ [* F
image.src = eye/item_eye_1.png; # R; F6 e" @+ M' ]# |
image.onload = function () { : R: U' v8 @5 N" ~! B1 r8 K* Y. x
recolorimage(cid,image, 0, 0, 0, 255, 0, 0);
8 |! b1 U' w* L ^} 9 K+ d# D, k0 v p- A
function recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) {
( _4 m) I. }- Svar c = document.getelementbyid(c);
% k3 r( _- h6 V; \/ \$ S' ]var ctx = c.getcontext(2d);
- y w8 t- N' T4 U2 n& Uvar w = img.width; 7 t% G/ y( h4 q2 A8 Z {9 \, V( n3 G
var h = img.height;
; Z" N* u5 A# O7 W+ W7 e' ]5 oc.width = w; * p4 a g! a: c0 f
c.height = h; * `5 F7 Q# F! p6 k' n: ?$ E p
// draw the image on the temporary canvas 4 b' J7 p" T6 h4 C) l" s6 r F: `4 e
ctx.drawimage(img, 0, 0, w, h);
y, @9 t. w5 U// pull the entire image into an array of pixel data - I( Y/ i5 y9 x" p* E
var imagedata = ctx.getimagedata(0, 0, w, h); - b% n* H+ b3 G( ~+ R
// examine every pixel, " i% F( i+ m r8 a4 J% W
// change any old rgb to the new-rgb + f" P, a$ p# Y
for (var i = 0; i < imagedata.data.length; i += 4) {
# E7 c. ^1 U8 v; G. t& P c3 Q// is this pixel the old rgb?
! c2 J, L: y7 _. k0 J. ?if (imagedata.data == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) {
$ @- f4 w% q: [9 b6 k+ r// change to your new rgb
. w- I4 P+ u1 bimagedata.data = newred; * L0 r! m6 }6 B: O c
imagedata.data[i + 1] = newgreen; ' q8 s8 F8 @9 u" ]: w
imagedata.data[i + 2] = newblue; + p, r/ l" C, \* ~+ B) M; B9 }
} 1 D7 j1 d1 J9 K
}
7 ^; X+ E' t$ a, k5 M// put the altered data back on the canvas
; V: ^: y v; b5 C" v4 P4 wctx.putimagedata(imagedata, 0, 0); 1 p, ?1 ^. k% d
} & A3 f# `2 Z) e7 z1 t/ W
</script> ' h& D. q t( `8 |
! w6 O" r% m {+ i( I3 ?' j
更多网页制作信息请查看: 网页制作 |