Rectangles in HTML Canvas
- Previous Page Shapes in Canvas
- Next Page Circular in Canvas
Sample
Ihanda ang isang rectangle na 150*100 pixel:
const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.rect(20, 20, 150, 100); ctx.stroke();
rect() method
Ang method na rect() ay nagdodagdag ng isang rectangle sa path.
Gramata:
context.rect(x, y, width, height)
Parametro | Paglalarawan |
---|---|
x | Ang x-coordinate ng kanang itaas ng rectangle. |
y | Ang y-coordinate ng kanang itaas ng rectangle. |
width | Ang lapad ng rectangle, sa mga pixel. |
height | Ang taas ng rectangle, sa mga pixel. |
More samples
Sample
gumawa ng tatlong rectangle gamit ang method na rect():
JavaScript:
const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); // 红色矩形 ctx.beginPath(); ctx.lineWidth = "6"; ctx.strokeStyle = "red"; ctx.rect(5, 5, 290, 140); ctx.stroke(); // 绿色矩形 ctx.beginPath(); ctx.lineWidth = "4"; ctx.strokeStyle = "green"; ctx.rect(30, 30, 50, 50); ctx.stroke(); // 蓝色矩形 ctx.beginPath(); ctx.lineWidth = "10"; ctx.strokeStyle = "blue"; ctx.rect(50, 50, 150, 80); ctx.stroke();
For more information, see:
- Previous Page Shapes in Canvas
- Next Page Circular in Canvas