HTML5 Canvas - drawing shapes and creating gradients

This HTML5 canvas tutorial explains the following:

  • Drawing shapes, changing the border and filling them.
  • Linear gradient.
  • Radial gradient.
  • Introducing alpha transparency.
  • Interactive programmes to practise both linear and radial gradients on site.

 

Finding a good book to master HTML5 can be very challenging: there are so many around - most with eye-catching titles and very complex substance.
Therefore, Vivax Solutions strongly recommends Core HTML5 Canvas for those who really want to delve into HTML5.
Please click the image to access Amazon:

Filling the Shapes

In order to fill the closed shapes,
context.fillStyle='colour' property and
context.fill() method can be used.

The 'colour' property can be Hexadecimal such as #FF0000 for red or rgb(255,0,0) or even plain English such as 'red', for the same


The Source Code for Filling:

<canvas id="Canvas_Three" width="400" height="300" style="border:5px solid red;"></canvas>
<script type="text/javascript">
var cv = document.getElementById('Canvas_Three');
var cnt = cv.getContext('2d');
//<-----------------Filling a rectangle in cyan----------------------------------->
cnt.beginPath();
cnt.rect(20, 40, 140, 60);
// filling colour
cnt.fillStyle = 'blue';
cnt.fill();
// border width
cnt.lineWidth = 5;
// border colour
cnt.strokeStyle = 'cyan';
cnt.stroke();
//<-----------------Filling a circle in blue--------------------------------------->
cnt.beginPath();
cnt.arc(200, 150, 50, 0, 2 * Math.PI, false);
// filling colour
cnt.fillStyle = 'green';
cnt.fill();
// border width
cnt.lineWidth = 3;
// border colour
cnt.strokeStyle = 'blue';
cnt.stroke();
//<----------------Filling a segment in red---------------------------------------->
cnt.beginPath();
cnt.arc(300, 220, 50, 0.5*Math.PI, 1.3* Math.PI, true);
// close the two ends of the arc for filling
cnt.closePath();
// border width
cnt.lineWidth = 5;
// fill colour
cnt.fillStyle = 'red';
cnt.fill();
// border colour
cnt.strokeStyle = '#550000';
cnt.stroke();
</script>

Gradients - linear and Radial

Linear Gradient

The getContext() object draws an 'invisible' line, defined by the two pairs of coordinates, on the canvas. Then, the spread of two selected colours are controlled along the line.

First, read the code; then, click the interactive button to see the effect on the canvas.

 

  Gradient Line:  

 

The Code for Linear Gradient

 

<canvas id="Canvas_Four" width="400" height="300" style="border:5px solid red;"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('Canvas_Four');
var cnt = canvas.getContext('2d');
// Draw a rectangle
cnt.rect(0, 0, canvas.width, canvas.height);
// add linear gradient - defined by a pair of coordinates for a line
var grd = cnt.createLinearGradient(0, 0, 400, 300);
// white for first colour stop
grd.addColorStop(0, 'white');
// green for second colour stop
grd.addColorStop(1, 'green');
cnt.fillStyle = grd;
cnt.fill();
</script>

Radial Gradient

The radial gradient is determined by two co-centric circles - the inner circle and outer circle; they have their own colours which collectively create the gradient.

First, read the code; then, click the interactive button to see the effect on the canvas.




 

Radii of Inner Circle, Outer Circle and Main Circle

 




The Code for Radial Gradient:

<canvas id="Canvas_Five" width="400" height="300" style="border:5px solid red;"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('Canvas_Five');
var cnt = canvas.getContext('2d');
cnt.arc(200, 150, 120, 0, 2 * Math.PI, false);
// create radial gradient - inner circle and outer circle with centre(200,150)
var grd = cnt.createRadialGradient(200, 150, 10, 200, 150, 100);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#0000FF');
cnt.fillStyle = grd;
cnt.fill();
</script>




Transparency

The transparency of an image drawn on canvas is controlled by context.fillStyle property. The colour is given as rgba where a represents alpha transparencty. Its value can be anything between 0 and 1.




01

Select the transparency of the larger circle



The Source Code for Alpha Trasnsparency:

<canvas id="Canvas_Six" width="400" height="300" style="border:5px solid red;"></canvas>
<script type="text/javascript">
context_six.beginPath();
context_six.arc(200, 150, 50, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.lineWidth = 5;
context_six.fillStyle = 'rgb(255,0,0)';
context_six.fill();
// begin larger green circle with alpha transparency
context_six.beginPath();
context_six.arc(200, 150, 100, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.lineWidth = 5;
context_six.fillStyle = 'rgba(0,255,0,' + 0.5 + ')';
context_six.fill();
// slider calls the function
function alpha_tra() {
// value of the slider is stored in the variable a
var a = eval(document.getElementById('transp').value);
// begin smaller red circle
context_six.beginPath();
context_six.arc(200, 150, 50, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.lineWidth = 5;
context_six.fillStyle = 'rgb(255,0,0)';
context_six.fill();
// begin larger green circle with alpha transparency
context_six.beginPath();
context_six.arc(200, 150, 100, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.lineWidth = 5;
context_six.fillStyle = 'rgba(0,255,0,' + a + ')';
context_six.fill();
</script>
}

 

 

 

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Email: 

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Advertisements

Advertisement