【问题标题】:HTML Canvas & Javascript - Shadow Around Image Rather Than BorderHTML Canvas & Javascript - 图像周围的阴影而不是边框
【发布时间】:2017-10-03 20:09:30
【问题描述】:

我正在尝试在包含图像的弯曲矩形的边框后面绘制阴影。然而,下面的代码将阴影放在图像后面而不是边框​​。如何更改此代码以使阴影位于边框后面?

为了澄清,边框目前被图像阴影遮蔽,但我不希望图像有任何阴影。我想要阴影是图像周围的白色边框。我不希望画布周围有阴影,而是希望在画布有弯曲的矩形边框。

var c=document.getElementById('game'),
		canvasX=c.offsetLeft,
		canvasY=c.offsetTop,
		ctx=c.getContext('2d')
		elements = [];
    
var x=25, y=25, w=150, h=150;

var img=new Image(); 
img.src='https://i.stack.imgur.com/ddSWa.jpg';
ctx.beginPath();
ctx.lineWidth='8';
ctx.strokeStyle='white';
ctx.moveTo(x+10, y);
ctx.lineTo(x+w-10, y);
ctx.quadraticCurveTo(x+w, y, x+w, y+10);
ctx.lineTo(x+w, y+h-10);
ctx.quadraticCurveTo(x+w, y+h, x+w-10, y+h);
ctx.lineTo(x+10, y+h);
ctx.quadraticCurveTo(x, y+h, x, y+h-10);
ctx.lineTo(x, y+10);
ctx.quadraticCurveTo(x, y, x+10, y);
ctx.shadowColor = '#000000';
ctx.shadowBlur = 20;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.stroke();
ctx.drawImage(img, x+2.5, y+2.5, w-5, h-5);
canvas {
  display: block;
  margin: 1em auto;
  border: 1px solid black;
  background: #FF9900;
}
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>uTalk Demo</title>
	<link rel='stylesheet' type='text/css' href='game.css' media='screen'></style>
</head>
<body>
	<canvas id="game" width = "200" height = "200"></canvas>
</body>
</html>

【问题讨论】:

  • 很抱歉我的回答,删除它,但把它放在你的问题中,在里面

标签: javascript html image canvas


【解决方案1】:

您正在绘制的形状只是一个图像,而不是border。它只会保留在画布内。但如果你做这样的事情,你只需要玩x,y coordinates。否则,您可以使用borderbox-shadow 属性

var c=document.getElementById('game'),
		canvasX=c.offsetLeft,
		canvasY=c.offsetTop,
		ctx=c.getContext('2d')
		elements = [];
    
var x= 20, y=20, w=160, h=160;

var img=new Image(); 
img.src='https://i.stack.imgur.com/ddSWa.jpg';
ctx.beginPath();
ctx.lineWidth='8';
ctx.strokeStyle='white';
ctx.moveTo(x+10, y);
ctx.lineTo(x+w-10, y);
ctx.quadraticCurveTo(x+w, y, x+w, y+10);
ctx.lineTo(x+w, y+h-10);
ctx.quadraticCurveTo(x+w, y+h, x+w-10, y+h);
ctx.lineTo(x+10, y+h);
ctx.quadraticCurveTo(x, y+h, x, y+h-10);
ctx.lineTo(x, y+10);
ctx.quadraticCurveTo(x, y, x+10, y);
ctx.shadowColor = '#000000';
ctx.shadowBlur = 20;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.stroke();
ctx.drawImage(img, x+27.5, y+27.5, w-55, h-55);
canvas {
  display: block;
  margin: 1em auto;
  background: transparent;
}
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>uTalk Demo</title>
	<link rel='stylesheet' type='text/css' href='game.css' media='screen'></style>
</head>
<body>
	<canvas id="game" width = "200" height = "200"></canvas>
</body>
</html>

【讨论】:

  • 对不起,我想我的问题一定不够清楚。我已经添加了一些额外的细节,但 Kaiido 的答案正是我想要的。
【解决方案2】:

不确定您需要什么。我认为您的代码没有任何问题。您是否尝试过使用:

ctx.strokeStyle = 'grey'; //or the color you want.
ctx.lineWidth = 5; //For thickness

【讨论】:

    【解决方案3】:

    只需在绘制图像之前移除 shadowOffset。 设置此属性后,所有后续绘图都会有阴影。

    var c=document.getElementById('game'),
    		canvasX=c.offsetLeft,
    		canvasY=c.offsetTop,
    		ctx=c.getContext('2d')
    		elements = [];
        
    var x=25, y=25, w=150, h=150;
    
    var img=new Image(); 
    img.src='https://i.stack.imgur.com/ddSWa.jpg';
    ctx.beginPath();
    ctx.lineWidth='8';
    ctx.strokeStyle='white';
    ctx.moveTo(x+10, y);
    ctx.lineTo(x+w-10, y);
    ctx.quadraticCurveTo(x+w, y, x+w, y+10);
    ctx.lineTo(x+w, y+h-10);
    ctx.quadraticCurveTo(x+w, y+h, x+w-10, y+h);
    ctx.lineTo(x+10, y+h);
    ctx.quadraticCurveTo(x, y+h, x, y+h-10);
    ctx.lineTo(x, y+10);
    ctx.quadraticCurveTo(x, y, x+10, y);
    ctx.shadowColor = '#000000';
    ctx.shadowBlur = 20;
    ctx.shadowOffsetX = 5;
    ctx.shadowOffsetY = 5;
    ctx.stroke();
    // now reset the shadow
    ctx.shadowBlur = 0;
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
    
    ctx.drawImage(img, x+2.5, y+2.5, w-5, h-5);
    canvas {
      display: block;
      margin: 1em auto;
      border: 1px solid black;
      background: #FF9900;
    }
    <!doctype html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>uTalk Demo</title>
    	<link rel='stylesheet' type='text/css' href='game.css' media='screen'></style>
    </head>
    <body>
    	<canvas id="game" width = "200" height = "200"></canvas>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2010-12-06
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 2011-06-08
      • 2014-07-28
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多