HTML5 canvas提供了在图纸周围创建漂亮阴影的功能。所有绘图操作均受四个全局阴影属性影响。
| 序号 | 属性和说明 |
|---|---|
| 1 | shadowColor [=值] |
| 2 | shadowOffsetX [=值] |
| 3 | shadowOffsetY [=值] |
| 4 | shadowBlur [=值] |
您可以尝试运行以下代码来创建阴影:
<!DOCTYPE HTML>
<html>
<head>
<style>
#test {
width: 100px;
height:100px;
margin: 0px auto;
}
</style>
<script type>
function drawShape(){
//使用DOM获取canvas元素
var canvas = document.getElementById('mycanvas');
//确保不支持画布时不执行
if (canvas.getContext){
//使用getContext使用画布进行绘制
var ctx = canvas.getContext('2d');
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 2;
ctx.shadowColor = "rgba(0, 0, 0, 0.5)";
ctx.font = "20px Times New Roman";
ctx.fillStyle = "Black";
ctx.fillText("This is shadow test", 5, 30);
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
}
}
</script>
</head>
<body id = "test" onload = "drawShape();">
<canvas id = "mycanvas"></canvas>
</body>
</html>