css 将阴影投影到画布的剪切区域之外

5t7ly7z5  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在通过画布将一张图片裁剪成一个形状。现在我想在它的外面添加一个投影。我该怎么做呢?

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);
}
iyfjxgzm

iyfjxgzm1#

ctx.clip()附近执行此操作:

ctx.shadowOffsetX = 2; // Sets the shadow offset x, positive number is right
ctx.shadowOffsetY = 2; // Sets the shadow offset y, positive number is down
ctx.shadowBlur = 4; // Sets the shadow blur size
ctx.shadowColor = 'rgba(0, 0, 0, 0.6)'; // Sets the shadow color
ctx.fillStyle = 'none';
ctx.fill();

相关问题