Metoda rect() kanwy HTML

❮ Odniesienie do płótna HTML

Przykład

Narysuj prostokąt o wymiarach 150*100 pikseli:

Twoja przeglądarka nie obsługuje tagu HTML5canvastag.

JavaScript:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(20, 20, 150, 100);
ctx.stroke();

Obsługa przeglądarki

Liczby w tabeli określają pierwszą wersję przeglądarki, która w pełni obsługuje tę metodę.

Method
rect() Yes 9.0 Yes Yes Yes

Definicja i użycie

Metoda rect() tworzy prostokąt.

Wskazówka: użyj metody stroke() lub fill() , aby narysować prostokąt na kanwie.

Składnia JavaScript: kontekst .rect( x,y,szerokość,wysokość );

Wartości parametrów

Parameter Description Play it
x The x-coordinate of the upper-left corner of the rectangle
y The y-coordinate of the upper-left corner of the rectangle
width The width of the rectangle, in pixels
height The height of the rectangle, in pixels

Więcej przykładów

Przykład

Utwórz trzy prostokąty za pomocą metody rect():

Twoja przeglądarka nie obsługuje tagu canvastag.

JavaScript:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Red rectangle
ctx.beginPath();
ctx.lineWidth = "6";
ctx.strokeStyle = "red";
ctx.rect(5, 5, 290, 140);
ctx.stroke();

// Green rectangle
ctx.beginPath();
ctx.lineWidth = "4";
ctx.strokeStyle = "green";
ctx.rect(30, 30, 50, 50);
ctx.stroke();

// Blue rectangle
ctx.beginPath();
ctx.lineWidth = "10";
ctx.strokeStyle = "blue";
ctx.rect(50, 50, 150, 80);
ctx.stroke();


❮ Odniesienie do płótna HTML