HTML5 animation – patterns with loops

Tutorials

HTML5 animation – patterns with loops. I have prepared new HTML5 tutorial for you. This is not quite regular ‘html5 game development’ tutorial, but I like to show you another demonstration of html5 canvas animation. We will create patterns with loops and animate it. Today I have prepared nice demo with the sun and with three cars.

Here are our demo and downloadable package:

Live Demo

[sociallocker]

download in package

[/sociallocker]


Ok, download the source files and lets start coding !


Step 1. HTML

Small code with canvas element (where we are going to experiment)

index.html

01 <!DOCTYPE html>
02 <html lang="en" >
03     <head>
04         <meta charset="utf-8" />
05         <title>HTML5 animation - patterns with loops | Script Tutorials</title>
06         <link href="css/main.css" rel="stylesheet" type="text/css" />
07         <script type="text/javascript" src="js/script.js"></script>
08     </head>
09     <body>
10         <header>
11             <h2>HTML5 animation - patterns with loops</h2>
12             <a href="https://www.script-tutorials.com/html5-animation-patterns-with-loops/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
13         </header>
14         <div class="container">
15             <div class="contr">
16                 <input type="radio" name="mode" onchange="bPlay = false" /><label>Pause</label>
17                 <input type="radio" name="mode" onchange="bPlay = true" checked /><label>Play</label>
18             </div>
19             <canvas id="panel" width="800" height="600"></canvas>
20         </div>
21     </body>
22 </html>

Step 2. CSS

css/main.css

We are going to skip of publishing styles today again (no need).

Step 3. JS

js/script.js

001 // variables
002 var canvas, ctx;
003 var bPlay = true;
004 var theSun;
005 var theCar1, theCar2, theCar3;
006 // define Sun constructor
007 function Sun(iCenterX, iCenterY, iRad, iRaysCnt, sRayColor, sSunColor){
008     this.iCenterX = iCenterX;
009     this.iCenterY = iCenterY;
010     this.iRad = iRad;
011     this.iRaysCnt = iRaysCnt;
012     this.sRayColor = sRayColor;
013     this.sSunColor = sSunColor;
014     this.iExAngle = 0;
015     this.bDir = true;
016 }
017 // Define Sun draw method
018 Sun.prototype.draw = function(){
019     // change angle
020     if (this.bDir) {
021         this.iExAngle += 0.05;
022     else {
023         this.iExAngle -= 0.05;
024     }
025     if (this.iExAngle > 3 || this.iExAngle < -3) {
026         this.bDir = ! this.bDir;
027     }
028     // draw rays
029     ctx.beginPath();
030     for (var n = 0; n < this.iRaysCnt; n++) {
031         var iAng1 = ((Math.PI * 2) / this.iRaysCnt) * (n + 1);
032         var iAng2 = ((Math.PI * 2) / this.iRaysCnt) * (n);
033         var iAng3 = ((Math.PI * 2) / this.iRaysCnt) * (n+0.5);
034         var x1 = (this.iRad * Math.sin(iAng1 + this.iExAngle)) + this.iCenterX;
035         var y1 = (this.iRad * Math.cos(iAng1 + this.iExAngle)) + this.iCenterY;
036         var x2 = (this.iRad * Math.sin(iAng2 + this.iExAngle)) + this.iCenterX;
037         var y2 = (this.iRad * Math.cos(iAng2 + this.iExAngle)) + this.iCenterY;
038         var x3 = (2 * this.iRad * Math.sin(iAng3 + this.iExAngle)) + this.iCenterX;
039         var y3 = (2 * this.iRad * Math.cos(iAng3 + this.iExAngle)) + this.iCenterY;
040         ctx.moveTo(x1, y1);
041         ctx.lineTo(x3, y3);
042         ctx.lineTo(x2, y2);
043     }
044     ctx.closePath();
045     // fill rays with color
046     ctx.fillStyle = this.sRayColor;
047     ctx.fill();
048     // draw sun center
049     ctx.beginPath();
050     ctx.arc(this.iCenterX, this.iCenterY, this.iRad, 0, 2 * Math.PI, false);
051     ctx.fillStyle = this.sSunColor;
052     ctx.fill();
053 };
054 // define CarWheel constructor
055 function CarWheel(iCenterX, iCenterY, iRad, iCnt, iSpeed, sColor1, sColor2, sColor3){
056     this.iCenterX = iCenterX;
057     this.iCenterY = iCenterY;
058     this.iRad = iRad;
059     this.iCnt = iCnt;
060     this.iSpeed = iSpeed;
061     this.sColor1 = sColor1;
062     this.sColor2 = sColor2;
063     this.sColor3 = sColor3;
064     this.iAngle = 0;
065 }
066 // Define CarWheel draw method
067 CarWheel.prototype.draw = function(){
068     this.iAngle += 0.1 * this.iSpeed / 2;
069     this.iCenterX += this.iSpeed;
070     // draw wheel details
071     ctx.beginPath();
072     for (var n = 0; n < this.iCnt; n++) {
073         var theta1 = ((Math.PI * 2) / this.iCnt) * (n + 1);
074         var theta2 = ((Math.PI * 2) / this.iCnt) * (n);
075         var x1 = (this.iRad * Math.sin(theta1-this.iAngle)) + this.iCenterX;
076         var y1 = (this.iRad * Math.cos(theta1-this.iAngle)) + this.iCenterY;
077         var x2 = (this.iRad * Math.sin(theta2-this.iAngle)) + this.iCenterX;
078         var y2 = (this.iRad * Math.cos(theta2-this.iAngle)) + this.iCenterY;
079         ctx.moveTo(this.iCenterX, this.iCenterY);
080         ctx.bezierCurveTo(x1, y1, x2, y2, this.iCenterX, this.iCenterY);
081     }
082     ctx.closePath();
083     // fill
084     ctx.fillStyle = this.sColor2;
085     ctx.fill();
086     // stroke
087     ctx.lineWidth = 1;
088     ctx.strokeStyle = this.sColor1;
089     ctx.stroke();
090     // draw center
091     ctx.beginPath();
092     ctx.arc(this.iCenterX, this.iCenterY, this.iRad/2.5, 0, 2 * Math.PI, false);
093     ctx.closePath();
094     ctx.fillStyle = this.sColor3;
095     ctx.fill();
096 };
097 // define Car constructor
098 function Car(iPosX, iPosY, iSpeed, iWidth, iHeight){
099     this.iPosX = iPosX;
100     this.iPosY = iPosY;
101     this.iSpeed = iSpeed;
102     this.iWidth = iWidth;
103     this.iHeight = iHeight;
104     this.theWheel1 = new CarWheel(this.iPosX + 41, this.iPosY + 52, 30, 8, this.iSpeed, 'black''brown''#5e5e5e');
105     this.theWheel2 = new CarWheel(this.iPosX + 191, this.iPosY + 52, 30, 8, this.iSpeed, 'black''brown''#5e5e5e');
106     // load image for car
107     this.imageObj = new Image();
108     this.imageObj.onload = function(){};
109     this.imageObj.src = 'images/car.png';
110 }
111 // Define Car draw method
112 Car.prototype.draw = function(){
113     this.iPosX += this.iSpeed;
114     if (this.iPosX > ctx.canvas.width /*+ this.iWidth*/) {
115         this.iPosX = -this.iWidth;
116         this.theWheel1.iCenterX = this.iPosX + 41;
117         this.theWheel2.iCenterX = this.iPosX + 191;
118         this.theWheel1.iAngle = 0;
119         this.theWheel2.iAngle = 0;
120     }
121     // draw car image
122     ctx.drawImage(this.imageObj, this.iPosX, this.iPosY, this.iWidth, this.iHeight);
123     // and animated wheels
124     this.theWheel1.draw();
125     this.theWheel2.draw();
126 };
127 // drawing functions
128 function clear() { // clear canvas function
129     ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
130 }
131 function drawScene() { // main drawScene function
132     if (bPlay == 1) {
133         clear(); // clear canvas
134         // draw animated objects
135         theSun.draw();
136         theCar1.draw();
137         theCar2.draw();
138         theCar3.draw();
139     }
140 }
141 function getRand(x, y) {
142     return Math.floor(Math.random()*y)+x;
143 }
144 // binding onload event
145 if (window.attachEvent) {
146     window.attachEvent('onload', main_init);
147 else {
148     if(window.onload) {
149         var curronload = window.onload;
150         var newonload = function() {
151             curronload();
152             main_init();
153         };
154         window.onload = newonload;
155     else {
156         window.onload = main_init;
157     }
158 }
159 function main_init() {
160     // create canvas and context objects
161     canvas = document.getElementById('panel');
162     ctx = canvas.getContext('2d');
163     // initialization of animated objects
164     theSun = new Sun(725, 100, 25, 10, 'red''yellow');
165     theCar1 = new Car(0, 300, 4, 226, 66);
166     theCar2 = new Car(100, 400, 3, 226, 66);
167     theCar3 = new Car(200, 500, 2, 226, 66);
168     setInterval(drawScene, 40); // loop drawScene
169 }

Exactly 200 lines of code 🙂 Here you can see three kind of objects (classes): Sun, Car and CarWheel. Each type have own constructor, own set of properties and Draw function. Sun rays we will draw as triangles. Car itself, consist of image and two wheels. Sun and Wheels uses patterns with loops. To create the sun (as example) we can create a loop with X iterations (where X is amount of rays or sectors). And, we will draw each ray separately (in loop).


Live Demo

Conclusion

Welcome back to read something new and interesting about HTML5. Good luck in your projects.

Rate article