Javascript examples for Canvas Reference:rect
The rect() method creates a rectangle.
context.rect(x, y, width, height);
Parameter | Description |
---|---|
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 |
Create three rectangles with the rect() method:
JavaScript:
<!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas> <script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); // Red rectangle//w ww. j a v a2s. co m 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(); </script> </body> </html>