我儘量只使用一個<canvas>
元素把對象前在畫布
1:剪輯中的圖像
2:製作一個更矩形
但如何把矩形放在前面?
var canvas = document.getElementById('example');
var ctx = canvas.getContext('2d');
var image = document.createElement('IMG');
image.onload = function() {
ctx.save();
ctx.beginPath();
ctx.moveTo(29, 96);
ctx.lineTo(157, 0);
ctx.lineTo(288, 93);
ctx.closePath();
ctx.clip();
ctx.drawImage(image, 0, 0);
ctx.restore();
};
image.src = 'http://lorempixel.com/500/500/';
// This
ctx.rect(20,20,150,100);
ctx.fillStyle="red";
ctx.fill();
只是一個附加回想起。您可以使用合成功能在現有圖像後面繪製新圖像。當你設置context.globalCompositeOperation =「destination-over」時,你的紅色矩形將被繪製在現有的三角形後面。 (注意:新矩形只能繪製成透明像素) – markE