代码如下:
# \, I; k% _( m, J* i( h<canvas id=c1 width=1220 height = 880 style=background: none repeat scroll 0% 0% transparent; ></canvas>
$ Y- {! j4 C( f* A5 V( w<script> 9 k3 B2 O& ~9 Y
var cid = c1;
- {& L+ H2 Q0 F) v4 pvar image = new image(); 8 d- v) W8 u& @
image.src = eye/item_eye_1.png; 6 d8 |% R3 n. d# ]/ ]5 f" n _0 a
image.onload = function () {
; x7 R6 p: M5 K4 I) Y& Arecolorimage(cid,image, 0, 0, 0, 255, 0, 0); % H5 Q6 W' u) v! R$ G l* [3 b0 l% ]
} * K* a( K R4 Y! P; X
function recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) {
3 Q) q7 P7 k- L" u# O( s. Uvar c = document.getelementbyid(c); 2 g' ?; K$ S9 ?
var ctx = c.getcontext(2d);
' Z% a5 S3 R# Z5 nvar w = img.width; * j( D9 c" s+ j4 |
var h = img.height;
2 _) A) n6 Q: Vc.width = w; 0 |# R! X% {4 |' k) g2 u7 e7 ]
c.height = h; ' u! G- q- @7 T) i1 w# W9 V8 h# v. T
// draw the image on the temporary canvas
" n0 @1 F5 H. |8 |( ictx.drawimage(img, 0, 0, w, h); + [& d. n9 ]- e3 y- g. D
// pull the entire image into an array of pixel data
3 `) [2 h" a/ a, f/ N; M8 Cvar imagedata = ctx.getimagedata(0, 0, w, h); 8 w1 g: G" |. Z. P
// examine every pixel,
* c* X" k9 \* R6 f' W$ f! L. S// change any old rgb to the new-rgb
0 f; Q3 b* I% r5 ufor (var i = 0; i < imagedata.data.length; i += 4) {
, u% y) N+ ?3 `// is this pixel the old rgb? 4 @" w! }5 |. t. U! A& d- @
if (imagedata.data == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) {
, x5 F# @ q9 C// change to your new rgb
0 m/ I' m* v8 Y: t0 y. P7 `imagedata.data = newred; 5 d6 w# s+ g$ J% J! L% F& z3 k, n
imagedata.data[i + 1] = newgreen; ; O, B: S8 v {2 @" }/ y
imagedata.data[i + 2] = newblue;
. E. t6 E0 v* V+ S1 n}
1 T- c0 T7 p+ |5 Q( X4 o3 [- T- Y}
; ]6 u; g7 y: g m, @- k4 I// put the altered data back on the canvas
" v* g& p, {8 v: K8 _- a* x( Qctx.putimagedata(imagedata, 0, 0);
6 ~7 C) R* F4 Q* x9 \3 |* O% _' F}
/ S% e& D' |( c; [</script> & Z8 Y0 c+ w& [/ Q" y0 D
% o8 M5 G; R( K
更多网页制作信息请查看: 网页制作 |