【发布时间】:2022-01-19 17:21:37
【问题描述】:
我正在尝试在剪切的图像上绘制阴影。
function drawCircle(ctx,w,h) {
ctx.beginPath();
ctx.ellipse(w/2, h/2, w/2, h/2, 0, 2 * Math.PI, false);
//ctx.fill(); // This helps, but not suitable for transparent images!
}
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = () => {
// Draw ellipse & clip (removing these 2 lines makes the dropshadow appear)
drawCircle(ctx, img.width,img.height);
ctx.clip();
ctx.shadowOffsetX = 4;
ctx.shadowOffsetY = 4;
ctx.shadowBlur = 4;
ctx.shadowColor = '#000';
ctx.clip();
ctx.drawImage(img, 0, 0);
}
img.src = 'https://i.imgur.com/NdrmFsr.jpg';
没有剪辑,画布上的图像有一个非常明显的阴影。 应用剪裁后,投影不可见。 我需要更改哪些内容才能使裁剪图像上的投影可见?
【问题讨论】:
标签: html canvas clipping dropshadow