代码如下:! e% u+ q, D/ Y4 Z
<canvas id=c1 width=1220 height = 880 style=background: none repeat scroll 0% 0% transparent; ></canvas> 0 i- y0 L. K0 Z* C; `
<script> O+ C7 ]4 T8 s# @; h3 O: J
var cid = c1; 6 ]. ~5 o, x, z- G& B j1 L
var image = new image();
4 D: D7 _( f' d! b4 h9 Uimage.src = eye/item_eye_1.png;
- A$ E i1 N$ k! e9 wimage.onload = function () {
- c5 X5 a9 G; ?2 C' X) {recolorimage(cid,image, 0, 0, 0, 255, 0, 0);
1 _' Y" T R7 ^8 B3 ~9 t3 p% M& p}
8 \( @! Q8 W7 a ~function recolorimage(c,img, oldred, oldgreen, oldblue, newred, newgreen, newblue) { 7 c8 h9 k( ]/ F# \* D: T
var c = document.getelementbyid(c); 1 q& D; |( C8 d2 l
var ctx = c.getcontext(2d); : o" c# n9 J! e% l, A+ S I) i
var w = img.width; 7 j4 h- }" K/ g. t
var h = img.height; 1 Z! A% {+ s5 W# W* \$ v
c.width = w; 8 b" g7 Q6 l6 ]7 M
c.height = h;
# k( a; u5 w4 _9 B' p// draw the image on the temporary canvas
. ]$ V, j# g! `2 B5 L- v+ D5 {; D9 Gctx.drawimage(img, 0, 0, w, h); 3 S( ~! B( w& _
// pull the entire image into an array of pixel data 8 U7 Z. N( f" c5 |
var imagedata = ctx.getimagedata(0, 0, w, h); % h$ |. R5 \6 a
// examine every pixel, # H# _) l! Y. e# P% o
// change any old rgb to the new-rgb . r& Z- A$ b% [1 G. z$ x( N
for (var i = 0; i < imagedata.data.length; i += 4) {
" X& t1 H$ h2 j( c// is this pixel the old rgb?
$ F7 e' D# t9 D- r3 ^if (imagedata.data == oldred && imagedata.data[i + 1] == oldgreen && imagedata.data[i + 2] == oldblue) {
K% v# H: F1 M6 N// change to your new rgb 7 ~* B' a4 y6 F8 W m, ]
imagedata.data = newred;
0 x% B# O* y ^; l. ?imagedata.data[i + 1] = newgreen; & u$ e& c9 r9 F; `2 R! s7 n$ V0 ?
imagedata.data[i + 2] = newblue; & d: z3 e" c5 I% ]
} ( p2 l+ a' O# `
}
, |% p5 i2 b& e5 r) f( R1 j4 b# {9 A- o// put the altered data back on the canvas
6 Q- m3 a1 Z- }) ?6 [0 mctx.putimagedata(imagedata, 0, 0);
n* @) i5 R6 h; d2 T} 9 _9 e4 {# r. v1 `2 }
</script> 5 q- @4 U* T9 B: i, P+ Q
# y0 A6 g. n9 \4 ^; x* E更多网页制作信息请查看: 网页制作 |