我正在通过画布将一张图片裁剪成一个形状。现在我想在它的外面添加一个投影。我该怎么做呢?
var canvas1 = document.createElement("canvas");
var ctx = canvas1.getContext("2d");
var canvas1_img = new Image();
canvas1_img.src = "https://placeimg.com/300/300/nature";
canvas1_img.addEventListener("load", draw);
function draw() {
var canvas_wd = canvas1.width;
var canvas_ht = canvas1.height;
var y1 = 20;
var y2 = 130;
ctx.beginPath();
ctx.moveTo(0, y1);
ctx.lineTo(0, y2);
ctx.lineTo(canvas_wd, canvas_ht);
ctx.lineTo(canvas_wd, 0);
ctx.closePath();
ctx.clip();
ctx.drawImage(canvas1_img, 0, 0);
document.body.appendChild(canvas1);
}
1条答案
按热度按时间iyfjxgzm1#
在
ctx.clip()
附近执行此操作: