我使用HTML5畫布創建一系列樹及其相應的陰影。我希望每個陰影都基於光源的位置。我可以繪製每棵樹,它的陰影以及一次歪斜所有的陰影,但試圖歪斜每個陰影單獨逃避我。這裏是我的代碼:使用畫布單獨晃動圖像
var ctx = cvs[0].getContext('2d');
ctx.save();
//skew the canvas
ctx.transform(1,0,0.3,-1,0,0);
ctx.drawImage(tree1,74,-117,20,40);
ctx.drawImage(tree1,126,-125,20,40);
var imgd = ctx.getImageData(0, 0, 500, 300);
var pix = imgd.data;
//convert the drawn trees to shadows
for (var i = 0, n = pix.length; i < n; i += 4) {
pix[i ] = 0; // red
pix[i+1] = 0; // green
pix[i+2] = 0; // blue
// alpha
}
ctx.putImageData(imgd, 0, 0);
//remove the skewing
ctx.restore();
//draw full color trees
ctx.drawImage(tree1,50,40,20,40);
ctx.drawImage(tree1,100,50,20,40);
是否有一種方法來傾斜圖像,因爲我使用drawImage繪製圖像?提前致謝!
只是一個想法,如果有可能不知道。有沒有辦法指定要轉換的畫布的某個區域?我可以在「劃痕」區域做歪斜,然後將每個陰影移動到正確的位置。 – Jeremy 2012-02-12 22:29:46