<br><div class="msgheader"> <div class="right"><span style="CURSOR: pointer" onclick="copycode(getid('phpcode17'));"><u>复制代码</u></span></div>代码如下:</div> <div class="msgborder" id="phpcode17"> <br><canvas id="c12" 20 " height = "880" style="background: none repeat scroll 0% 0% transparent; <br /><skrip> <br>var cID = "c1"; <br>imej var = Imej baharu(); <br>image.src = "Mata/item_eye_1.png"; <br>image.onload = fungsi () { <br>recolorImage(cID,imej, 0, 0, 0, 255, 0, 0); <br>} <br>fungsi recolorImage(c,img, oldRed, oldGreen, oldBlue, newRed, newGreen, newBlue) { <br>var c = document.getElementById(c); <br>var ctx = c.getContext("2d"); <br>var w = img.width; <br>var h = img.height; <br>c.lebar = w; <br>c.tinggi = h; <br>// lukis imej pada kanvas sementara <br>ctx.drawImage(img, 0, 0, w, h); <br>// tarik keseluruhan imej ke dalam tatasusunan data piksel <br>var imageData = ctx.getImageData(0, 0, w, h); <br>// periksa setiap piksel, <br>// tukar mana-mana rgb lama kepada rgb baharu <br>untuk (var i = 0; i < imageData.data.length; i = 4) { <br /> // adakah piksel ini rgb lama? <br />jika (imageData.data[i] == oldRed && imageData.data[i 1] == oldGreen && imageData.data[i 2] == oldBlue) { <br />// tukar kepada rgb baharu anda <br />imageData.data[i] = newRed; <br />imageData.data[i 1] = newGreen; <br />imageData.data[i 2] = newBlue; <br />} <br />} <br />// letakkan semula data yang diubah pada kanvas <br />ctx.putImageData(imageData, 0, 0); <br />} <br /></skrip> <br> </div>