【发布时间】:2014-03-25 09:21:13
【问题描述】:
我尝试只使用一个<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" 时,您的红色矩形将绘制在现有三角形的后面。 (注意:新的矩形只会被绘制成透明像素)
标签: javascript canvas position z-index