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.

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.

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

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.

Rate this:
1 Star
2 Star
3 Star
4 Star
5 Star
7 Ratings / 4.6 Average