Html5 canvas 在彩色正方形上创建半透明的内部笔划?
我在画布上画不同颜色的正方形,大小固定为50px x 50px 我已经成功地为这些彩色方块添加了一个5px的透明内部笔划,但我这样做似乎有点过分了Html5 canvas 在彩色正方形上创建半透明的内部笔划?,html5-canvas,Html5 Canvas,我在画布上画不同颜色的正方形,大小固定为50px x 50px 我已经成功地为这些彩色方块添加了一个5px的透明内部笔划,但我这样做似乎有点过分了 ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, engine.cellSize, engine.cellSize); ctx.fillStyle = 'rgba(0, 0, 0, 0.2)'; ctx.fillRect(this.x, this.y, engine.cellSize,
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, engine.cellSize, engine.cellSize);
ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
ctx.fillRect(this.x, this.y, engine.cellSize, engine.cellSize);
ctx.fillStyle = this.color;
ctx.fillRect(this.x + 5, this.y + 5, engine.cellSize - 10, engine.cellSize - 10);
有没有比画3个单独的矩形更好的方法来实现我的目标?是的
可以在矩形内使用填充颜色,也可以在矩形周围使用笔划颜色
下面是一段代码:
正文{背景色:象牙;}
画布{边框:1px纯红;}
$(函数(){
var canvas=document.getElementById(“canvas”);
var ctx=canvas.getContext(“2d”);
ctx.beginPath();
ctx.fillStyle=“红色”;
ctx.fillRect(100100,50,50);
ctx.fillStyle='rgba(0,0,0,0.2)';
ctx.fillRect(100100,50,50);
ctx.fillStyle=this.color;
ctx.fillRect(105,105,40,40);
ctx.fill();
ctx.beginPath();
ctx.rect(160102.5,45,45);
ctx.fillStyle='rgb(163,0,0)';
ctx.fill();
ctx.lineWidth=5;
ctx.strokeStyle='rgb(204,0,0)';
ctx.stroke();
}); // end$(函数(){});
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.beginPath();
ctx.fillStyle = "red";
ctx.fillRect(100,100,50,50);
ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
ctx.fillRect(100,100,50,50);
ctx.fillStyle = this.color;
ctx.fillRect(105, 105, 40, 40);
ctx.fill();
ctx.beginPath();
ctx.rect(160,102.5,45,45);
ctx.fillStyle = 'rgb(163,0,0)';
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = 'rgb(204,0,0)';
ctx.stroke();
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=600 height=400></canvas>
</body>
</html>