Método HTML canvas rect()

❮ Referencia de lienzo HTML

Ejemplo

Dibuja un rectángulo de 150*100 píxeles:

Su navegador no es compatible con HTML5canvastag.

JavaScript:

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

Compatibilidad con navegador

Los números en la tabla especifican la primera versión del navegador que soporta totalmente este método.

Method
rect() Yes 9.0 Yes Yes Yes

Definición y uso

El método rect() crea un rectángulo.

Sugerencia: use el método stroke() o fill() para dibujar el rectángulo en el lienzo.

Sintaxis de JavaScript: contexto .rect( x,y,ancho,alto );

Valores paramétricos

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

Más ejemplos

Ejemplo

Crea tres rectángulos con el método rect():

Su navegador no es compatible con el lienzo.

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


❮ Referencia de lienzo HTML