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:
[sociallocker]
[/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
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> |
11 | <h2>HTML5 animation - patterns with loops</h2> |
14 | <div class="container"> |
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> |
19 | <canvas id="panel" width="800" height="600"></canvas> |
Step 2. CSS
css/main.css
We are going to skip of publishing styles today again (no need).
Step 3. JS
js/script.js
005 | var theCar1, theCar2, theCar3; |
007 | function Sun(iCenterX, iCenterY, iRad, iRaysCnt, sRayColor, sSunColor){ |
008 | this.iCenterX = iCenterX; |
009 | this.iCenterY = iCenterY; |
011 | this.iRaysCnt = iRaysCnt; |
012 | this.sRayColor = sRayColor; |
013 | this.sSunColor = sSunColor; |
018 | Sun.prototype.draw = function(){ |
021 | this.iExAngle += 0.05; |
023 | this.iExAngle -= 0.05; |
025 | if (this.iExAngle > 3 || this.iExAngle < -3) { |
026 | this.bDir = ! this.bDir; |
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; |
046 | ctx.fillStyle = this.sRayColor; |
050 | ctx.arc(this.iCenterX, this.iCenterY, this.iRad, 0, 2 * Math.PI, false); |
051 | ctx.fillStyle = this.sSunColor; |
055 | function CarWheel(iCenterX, iCenterY, iRad, iCnt, iSpeed, sColor1, sColor2, sColor3){ |
056 | this.iCenterX = iCenterX; |
057 | this.iCenterY = iCenterY; |
060 | this.iSpeed = iSpeed; |
061 | this.sColor1 = sColor1; |
062 | this.sColor2 = sColor2; |
063 | this.sColor3 = sColor3; |
067 | CarWheel.prototype.draw = function(){ |
068 | this.iAngle += 0.1 * this.iSpeed / 2; |
069 | this.iCenterX += this.iSpeed; |
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); |
084 | ctx.fillStyle = this.sColor2; |
088 | ctx.strokeStyle = this.sColor1; |
092 | ctx.arc(this.iCenterX, this.iCenterY, this.iRad/2.5, 0, 2 * Math.PI, false); |
094 | ctx.fillStyle = this.sColor3; |
098 | function Car(iPosX, iPosY, iSpeed, iWidth, iHeight){ |
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'); |
107 | this.imageObj = new Image(); |
108 | this.imageObj.onload = function(){}; |
109 | this.imageObj.src = 'images/car.png'; |
112 | Car.prototype.draw = function(){ |
113 | this.iPosX += this.iSpeed; |
114 | if (this.iPosX > ctx.canvas.width ) { |
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; |
122 | ctx.drawImage(this.imageObj, this.iPosX, this.iPosY, this.iWidth, this.iHeight); |
124 | this.theWheel1.draw(); |
125 | this.theWheel2.draw(); |
129 | ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
131 | function drawScene() { |
141 | function getRand(x, y) { |
142 | return Math.floor(Math.random()*y)+x; |
145 | if (window.attachEvent) { |
146 | window.attachEvent('onload', main_init); |
149 | var curronload = window.onload; |
150 | var newonload = function() { |
154 | window.onload = newonload; |
156 | window.onload = main_init; |
159 | function main_init() { |
161 | canvas = document.getElementById('panel'); |
162 | ctx = canvas.getContext('2d'); |
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); |
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).
Conclusion
Welcome back to read something new and interesting about HTML5. Good luck in your projects.