เมธอด fillRect() ของแคนวาส HTML ใช้เพื่อสร้างสี่เหลี่ยมที่เติมสีบนหน้าเว็บ สีเริ่มต้นคือสีดำ
ต่อไปนี้เป็นไวยากรณ์ -
context.fillRect(p,q,width,height);
ข้างบน
- p :พิกัด x ของมุมบนซ้ายของสี่เหลี่ยม
- q :พิกัด y ของมุมบนซ้ายของสี่เหลี่ยม
- ความกว้าง :ความกว้างของสี่เหลี่ยม
- ความสูง :ความสูงของสี่เหลี่ยมผืนผ้า
ให้เราดูตัวอย่างการใช้เมธอด fillRect() ของ canvas−
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="500" height="350" style="border:2px solid orange;">
</canvas>
<script>
var c = document.getElementById("newCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 500, 350);
ctx.clearRect(250, 100, 50, 100);
</script>
</body>
</html> ผลลัพธ์
