Rectangles in HTML Canvas

Sample

Ang iyong browser ay hindi sumusuporta sa tag na HTML5 canvas.

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();

Try it yourself

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():

Ang iyong browser ay hindi sumusuporta sa tag na canvas.

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();

Try it yourself

For more information, see:

Complete Canvas Reference Manual of CodeW3C.com