我尽量只使用一个<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