代码如下:
5 E. F' l2 [% G+ m( w0 f4 S<canvas id=c1 width=1220 height = 880 style=background: none repeat scroll 0% 0% transparent; ></canvas> . w6 X/ E. W1 U' v0 `, s9 s
<script> 4 h2 N8 b+ O" I7 z6 W$ M
var cid = c1;
, J6 Q Q6 t" A+ h$ P0 wvar image = new image(); 8 O- w+ e# T0 C
image.src = eye/item_eye_1.png;
' d0 _* _) z ~# u: X7 Aimage.onload = function () { 8 s5 n' W! x8 `* O
recolorimage(cid,image, 0, 0, 0, 255, 0, 0); + {; D- {2 g; [
}
! f" O6 Q- J. U* z9 [5 Zfunction recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) {
% `5 l( W! ~* }# K; {5 O. k4 A2 yvar c = document.getelementbyid(c);
; W; m( ?) E/ Nvar ctx = c.getcontext(2d);
( S1 o' e$ ~" vvar w = img.width;
/ z$ h+ |( Q3 h- Vvar h = img.height; # A* }5 o+ [! G# i# K9 ]
c.width = w;
. d# f( g; @- n$ ^, K% e" x/ y% zc.height = h; / `% q$ A& \3 d
// draw the image on the temporary canvas 6 d F" q% Y( ?, @
ctx.drawimage(img, 0, 0, w, h);
/ v% J8 ~& }4 A// pull the entire image into an array of pixel data
" v; t* Q1 ?- \9 {- [* y# Mvar imagedata = ctx.getimagedata(0, 0, w, h); ; U1 T8 V, H2 W' i; y$ k1 T
// examine every pixel,
! [4 s0 m+ [% [# n5 g3 s// change any old rgb to the new-rgb
; \/ v# E' ?0 F8 ^. e4 Bfor (var i = 0; i < imagedata.data.length; i += 4) {
, r' q9 _: {4 V% z) i- B// is this pixel the old rgb?
7 K# h3 v& p* p: I& Nif (imagedata.data == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) { & }/ Z6 {3 z$ n) C' h% Q
// change to your new rgb
# d" l, J! Q! V% Bimagedata.data = newred;
$ I3 O4 V0 g- M+ rimagedata.data[i + 1] = newgreen; . S/ Q2 c1 D8 x
imagedata.data[i + 2] = newblue;
- {; N, o, F( m} 7 ^, `8 F6 I: H2 {' u$ _6 i
}
$ V; o4 `0 j& h8 ]// put the altered data back on the canvas
5 N0 P1 H$ Z, g5 w5 F ]ctx.putimagedata(imagedata, 0, 0);
3 t$ R% P! N; I3 \5 p3 q}
% F) O8 d8 t. F) E+ m</script>
4 N) s6 | G- k
7 r! e/ V6 A+ a* w1 b& V& W$ ?( Q: K8 ^更多网页制作信息请查看: 网页制作 |