Creating 2-D Animations on Canvas - lightning

In this tutorial, you will learn how to:

  • Make a cloud move in the dark sky.
  • Make electrons gather on the cloud as it moves closer to the objects on the ground.
  • Trigger off the thunderbolt when the cloud is over a specified object.
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:

Thunderbolt - lightning strike

This animation, which looks a bit comical, is not a joke. It shows the danger in real life if you carry an umbrella - unless you attach a metal chord to it, of course.

The induced electrostatic charges can put the man in danger unless the massive current is provided with an exit path.


 

The Code for the animation is as follows:

 

<script>
{
var canvas = document.getElementById('Canvas_Six');
var context_six = canvas.getContext('2d');
var y = new Date();
var s = y.getSeconds();
var imgCloud = new Image();
var imgShock = new Image();
var xc, yc;
var a = 0;
function orbit() {
context_six.clearRect(0, 0, 240, 400);
a = a + 1;
xc = 10 * a; yc = 10;
/*context_six.font = '12pt Calibri';
context_six.fillStyle = 'red';
context_six.fillText(a, 150,150);*/
if (a == 35) { a = 0; }
context_six.beginPath();
for (var j = 0; j <= 500; j++) {
context_six.font = '12pt Calibri';
context_six.fillStyle = 'grey';
context_six.fillText(',', Math.random() * 240 + 1, Math.random() * 390 + 1);
}
context_six.drawImage(imgCloud, 240 - xc, yc + 2 * Math.sin(a));
if (a < 10) { imgCloud.src = '../images/cloud1.png'; }
if (a > 10 && a < 20) {
context_six.font = '12pt Calibri';
context_six.fillStyle = 'white';
for (var i = 72; i < 186; i = i + 10) {
context_six.fillText('+', i, 155 + 5 * Math.random());
}
context_six.fillText('+', 125, 128);
context_six.fillText('+', 130, 120);
context_six.fillText('+', 135, 128);
imgCloud.src = '../images/cloud2.png';
//document.getElementById('Canvas_Six').style.backgroundImage = "";
}
if (a > 20) { imgCloud.src = '../images/cloud.png'; }
if (a > 17 && a < 20) {
context_six.beginPath();
context_six.moveTo(134, 50);
context_six.lineTo(134, 120);
context_six.lineWidth = 3;
context_six.moveTo(205, 200);
context_six.lineTo(206, 230);
context_six.lineWidth = 3;
context_six.moveTo(195, 330);
context_six.lineTo(198, 360);
context_six.lineWidth = 3;
context_six.strokeStyle = 'white';
context_six.stroke();
//context.six.closePath();
//context_six.beginPath();
context_six.drawImage(imgShock, 186, 380);
imgShock.src = '../images/shock.png
}
//context_six.font = '12pt Calibri';
//context_six.fillStyle = 'white';
// context_six.fillText('Year: ' + year, 40, 20);
// document.getElementById('Canvas_Six').style.backgroundImage = "";
}
window.setInterval('orbit()', 500);
</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