将阴影拖放到画布剪切区域之外

问题描述 投票:0回答:1

我正在通过画布将图像剪切成形状。现在我想在它外面添加一个阴影。我怎样才能做到这一点?

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

html image-processing css canvas html5-canvas
1个回答
0
投票

ctx.beginPath()
:

之前添加此内容
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();
© www.soinside.com 2019 - 2024. All rights reserved.