วิธี rect() ของผ้าใบ HTML ใช้เพื่อสร้างสี่เหลี่ยมผืนผ้า
ต่อไปนี้เป็นไวยากรณ์ -
context.fillRect(p,q,width,height);
ข้างบน
- หน้า: พิกัด x ของมุมบนซ้ายของสี่เหลี่ยม
- q: พิกัด y ของมุมบนซ้ายของสี่เหลี่ยมผืนผ้า
- ความกว้าง: ความกว้างของสี่เหลี่ยม
- ความสูง: ความสูงของสี่เหลี่ยมผืนผ้า
ให้เราดูตัวอย่างการใช้วิธี rect() ของผ้าใบ -
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="450" height="350" style="border:2px solid blue;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(100, 60, 200, 200);
ctx.fillStyle = "orange";
ctx.fill();
ctx.beginPath();
ctx.rect(110, 90, 180, 120);
ctx.fillStyle = "yellow";
ctx.fill();
</script>
</body>
</html> ผลลัพธ์
