HTMLキャンバスrect()メソッド

❮HTMLキャンバスリファレンス

150 * 100ピクセルの長方形を描画します。

あなたのブラウザはHTML5canvastagをサポートしていません。

JavaScript:

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

ブラウザのサポート

表の数字は、このメソッドを完全にサポートする最初のブラウザバージョンを示しています。

Method
rect() Yes 9.0 Yes Yes Yes

定義と使用法

rect()メソッドは長方形を作成します。

ヒント:stroke()または fill()メソッドを使用して、実際にキャンバスに長方形を描画します。

JavaScript構文: context .rect(x、y、width、height);

パラメータ値

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

その他の例

rect()メソッドを使用して3つの長方形を作成します。

あなたのブラウザは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();


❮HTMLキャンバスリファレンス