HTML5 canvas ให้ความสามารถสร้างเงา (shadows) ที่สวยง้มรอบการวาดภาพ ทุก drawing operations ได้รับผลกระทบจาก 4 global shadow attributes
| Sr.No. | Property and Description |
|---|---|
| 1 | shadowColor [ = value ] คืนค่าสีเงาปัจจุบัน และตั้งค่าเพื่อเปลี่ยนสีเงา |
| 2 | shadowOffsetX [ = value ] คืนค่า shadow offset X ปัจจุบัน และตั้งค่าเพื่อเปลี่ยน offset X |
| 3 | shadowOffsetY [ = value ] คืนค่า shadow offset Y ปัจจุบัน และตั้งค่าเพื่อเปลี่ยน offset Y |
| 4 | shadowBlur [ = value ] คืนค่าระดับ blur ของเงา และตั้งค่าเพื่อเปลี่ยนระดับ blur |
ตัวอย่าง
ลองรันโค้ดเพื่อสร้างเงา:
<!DOCTYPE HTML>
<html>
<head>
<style>
#test { width: 100px; height: 100px; margin: 0px auto; }
</style>
<script>
function drawShape() {
var canvas = document.getElementById('mycanvas');
if (canvas.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>