Shapes and Fill Styles

Built into HTML5 is the ability to draw various shapes using various fill styles such as solid colors and gradients.  Lets start with the HTML to declare our Canvas element, this will be used throughout the examples on this page.

<body onload="drawCanvas();">
    <div>
        <canvas id="myCanvas" width="150" height="150">
            <p>Your browser doesn't support canvas.</p>
        </canvas>
    </div>
</body>

Drawing a Rectangle

Here we use the Canvas element to draw a simple rectangle.

As with our other examples we declare our Canvas element and call the drawCanvas() method when the page loads.

function drawCanvas() {
    // Get our Canvas element
    var surface = document.getElementById("myCanvas");

    if (surface.getContext) {
        // If Canvas is supported
        var context = surface.getContext('2d');

        // Set the fill style to Red
        context.fillStyle = "rgb(255,0,0)";
        
        // Draw the rectangle 50x50 pixels in dimension at x/y 10,10
        context.fillRect(10, 10, 50, 50);
    }
}

The property fillStyle as its name suggests should be set to your desired fill type, in this example we simple set a solid color.  After setting the fill style we call the fillRect() method, this takes 4 parameters, the top left position and the width and height of the rectangle to draw.

Solid Fill Style

Gradient Fills

In our rectangle above we use a solid color to fill it, here we will use a gradient fill instead.

function drawCanvas() {
    // Get our Canvas element
    var surface = document.getElementById("myCanvas");

    if (surface.getContext) {
        // If Canvas is supported
        var context = surface.getContext('2d');
        // Create our gradient
        var gradient = context.createLinearGradient(0, 0, 150, 0);
        // Add 2 steps to it
        gradient.addColorStop(0, "rgb(100,0,0)");
        gradient.addColorStop(1, "rgb(255,0,0)");
        // Assign our gradient to the fillStyle
        context.fillStyle = gradient;
        // Draw the rectangle 150x150 pixels in dimension
        context.fillRect(0, 0, 150, 150);
    }
}

Here we create a linear gradient object using the createLinearGradient() method, this takes 4 parameters that specify the top left coordinates and the width/height of the gradient.  In this example we make it 150x150 in size, the same as our Canvas.

Gradient Fill

What if you wanted the gradient to go from top to bottom?  You simply change the x and y coordinates you used when creating the gradient.

// Create our gradient
var gradient = context.createLinearGradient(0, 0, 0, 150);

Vertical Gradient

Drawing a Circle

There isn't a drawCircle() method in HTML5, instead we use the arc() method.

function drawCanvas() {
    // Get our Canvas element
    var surface = document.getElementById("myCanvas");

    if (surface.getContext) {
        // If Canvas is supported
        var context = surface.getContext('2d');

        // Draw our circle using the arc() method
        context.beginPath();
        context.arc(30, 30, 20, 0, Math.PI * 2, false);
        context.closePath();

        // Our circle will not appear until we specify a stroke/fill
        context.fillStyle = "#ff0000";
        context.lineWidth = 2;
        context.strokeStyle = "#0000ff";
        context.stroke();
        context.fill();
    }
}

Here we set the stroke to #0000ff (Red) with a thickness of 2 pixels.  To actually draw the stroke we call stroke().  The fill style used here is simpy a solid Red.

Solid Circle

Post your Comments

 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 707

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 729

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 724

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

3 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 751

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 520

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 768

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 573

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 643

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 994

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

2 Ratings/4 Average
Fruit Slicer
Jan 02, 2016
Plays: 553

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average