Rectangle

Rectangle is the only shape existing in Canvas that can be stroked and filled immediately. All others are paths and require stroke/fill functions for actual drawing.

#

Rectangle is positioned with x and y parameters, and is sized with width and height parameters.

Functions:

Properties:


To Do

Try the following options:

  1. Using clearRect
    ctx.clearRect(0, 0, Canvas.width, Canvas.height);
    ctx.fillRect(25,25,100,100);
    ctx.clearRect(45,45,60,60);
    ctx.strokeRect(50,50,50,50);
  2. Various lineJoin and lineWidth
     ctx.lineWidth = "3";
     ctx.strokeStyle = "red";
     ctx.strokeRect(5, 5, 200, 250);
     ctx.lineWidth = "25";
     ctx.lineJoin = "bevel";
     ctx.strokeStyle = "blue";
     ctx.strokeRect(50, 200, 300, 150);
     ctx.lineJoin = "round";
     ctx.lineWidth = "45";
     ctx.strokeStyle = "green";
     ctx.strokeRect(150, 50, 150, 250);   
  3. Nested rectangles
    ctx.fillStyle='rgb(0,0,255)';
    ctx.fillRect(0,0,400,400);
    ctx.fillStyle='rgb(255,0,0)';
    ctx.fillRect(50,50,300,300);
    ctx.fillStyle='rgb(0,255,0)';
    ctx.fillRect(100,100,200,200);
    ctx.fillStyle='rgb(100,100,100)';
    ctx.fillRect(125,175,150,25);  
  4. Transparency
    ctx.fillStyle='rgb(0,0,255)';
    ctx.fillRect(30,30,300,300);
    ctx.fillStyle='rgba(0,255,0,0.5)';
    ctx.fillRect(60,60,300,300);
    ctx.fillStyle='rgba(255,0,0,0.25)';
    ctx.fillRect(90,90,300,300);