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.
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.
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.
In our rectangle above we use a solid color to fill it, here we will use a gradient fill instead.
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.
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.
Drawing a Circle
There isn't a drawCircle() method in HTML5, instead we use the arc() method.
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.
Drive fast before the crazy mutant zombies get you!
Guide the car to its parking space in this fun Car Parking game.
Aim for the Trash Can and get the various items of Trash in the bin.
Fly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.
Pop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!
Here 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!
Guide 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.
You 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!
Play Pool against the computer or battle against your friends in the online mode!
Slice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!