Since I've not been innovating much outside work for a while now, I thought I could make any new "thing" to aggregate a few benefits I've been aiming for. The output was this HTML5 canvas, game-like, Egytian-themed profile widget. It was a chance to play with HTML5 canvas, and still stick to the design of the game loop. It was also a chance to change the look of my boring blogs.
You play the game. Drop the egg from the profile you want to visit. If the egg hits the nest, you earn the visit to the profile.
So without any more talking, the code is pretty simple:
Notes:
I preferred to post the code here again for better syntax highlighting
[profile_widget_script.js]
Resources:
HTML5 Canvas Deep Dive
HTML5 Canvas Mouse Coordinates Tutorial
You play the game. Drop the egg from the profile you want to visit. If the egg hits the nest, you earn the visit to the profile.
So without any more talking, the code is pretty simple:
- A game loop
- Check for user input (mouse clicks)
- Update the values of some objects
- Detect collisions of some objects and acting accordingly
- Draw objects
Notes:
- The javaScript code is hosted online and only referenced to in my HTML code. This was better to update the code once and apply it to all the places where I use it. (as a desktop/mobile developer, this is great).
- Loading an image at runtime made a lag of maybe one second when a egg is dropped, but it looked ugly so I commented the code regarding loading the egg images in runtime and used a single preloaded image instead.
- I've looked about three times for performance boosters. But still this does not mean this is the best to get from HTML5 canvas and Javascript. It is just about the amount of time I gave to this code.
- If I did not put much comments, it was because I thought the code is self-explaining.
I preferred to post the code here again for better syntax highlighting
[profile_widget_script.js]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | var canvas = document.getElementById( 'canvas' ); var context = canvas.getContext( '2d' ); var globalFPS = 20; var maxItemSpeed = 2; var eggSpeed = 8; var eggSize = 32; var minItemHeight = 200; var iconSize = 32; var wingSize = 32; var nestX = 100; var nestY = 330; var nestMaxX = 150; var nestMinX = 50; var nestWidth = 96; var nestHeight = 57; var nestSpeed = 2; var mouseEventFlag = false ; var mouseEventX = 0; var mouseEventY = 0; var wave1X = -10; var wave1Y = 360; var wave1Direction = 1; var wave2X = -25; var wave2Y = 375; var wave2Direction = 1; var wave3X = -40; var wave3Y = 390; var wave3Direction = 1; var waveXThreshold = -50; // where to toggle the direction of wave var waveSpeed = 2; // put the items you want to show here {icon to fly, redirecting url, egg to drop} var profilesArray = [ { }, { }, { }, { }, { }, { }, { } ]; var items = []; var eggs = []; var wingImage = new Image; var nestImage = new Image(); var background = new Image(); var waveImage1 = new Image(); var waveImage2 = new Image(); var waveImage3 = new Image(); var eggImage = new Image(); // loading one egg to use it later, for performance reasons var nest = { image: nestImage, x: nestX, y: nestY, width: nestWidth, height: nestHeight, speed: nestSpeed, xDirection: 1, minX: nestMinX, maxX: nestMaxX }; createItems(); setInterval(gameLoop, 1000/globalFPS); function gameLoop(){ updateUserInput(); updateItems(); updateEggs(); updateNest(); updateWaves(); checkEggsCollision(); drawBackground(); drawWaves() drawItems(); drawNest(); drawEggs(); } function createItems() { for ( var i in profilesArray) { var profile = profilesArray[i]; var img = new Image(); img.src = profile.icon; items.push({ x: 0- Math.random()*500, // so icons do not appear at once y: Math.random()*minItemHeight, speed: 1+Math.random()*maxItemSpeed, image: img, url: profile.url, egg: profile.egg, wingRotation: 0, wingRotationDirection: 1 }); } } function updateItems() { for ( var i in items) { var item = items[i]; item.x += item.speed; item.wingRotation += item.wingRotationDirection * 0.1 if (item.wingRotation > Math.PI/4) { item.wingRotationDirection = -1; } else if (item.wingRotation < 0) { item.wingRotationDirection = 1; } // if item gets out of canvas scope if (item.x > canvas.width) { // recycle the item and set different values instead of removing it item.x = 0-Math.random()*100; item.y = Math.random()*minItemHeight; item.speed = 1+Math.random()*maxItemSpeed //between 2 and 5 item.wingRotation = 0; } } } function updateEggs(){ for ( var i in eggs){ var egg = eggs[i]; egg.y += egg.speed; // if egg gets out of canvas scope if (egg.x > canvas.height) { // remove egg from array eggs.splice(i, 1); } } } function updateNest(){ nest.x += nest.xDirection * nest.speed; if (nest.x > nest.maxX){ nest.xDirection = -1; } else if (nest.x < nest.minX){ nest.xDirection = 1; } } function updateWaves(){ wave1X += wave1Direction*waveSpeed; if (wave1X > 0) wave1Direction = -1; else if (wave1X < waveXThreshold) wave1Direction = 1; wave2X += wave2Direction*waveSpeed; if (wave2X > 0) wave2Direction = -1; else if (wave2X < waveXThreshold) wave2Direction = 1; wave3X += wave3Direction*waveSpeed; if (wave3X > 0) wave3Direction = -1; else if (wave3X < waveXThreshold) wave3Direction = 1; } function checkEggsCollision(){ for ( var i in eggs){ var egg = eggs[i]; if (egg.x+eggSize > nest.x+nest.width/3 && egg.x < nest.x+2*nest.width/3 && egg.y+eggSize > nest.y+nest.height/3 && egg.y < nest.y+2*nest.height/3) { alert( "Thanks for trying my profile widget. You will now be redirected to: \n" + egg.url); location.href = egg.url; // remove egg from array eggs.splice(i, 1); } } } function updateUserInput() { if (!mouseEventFlag) return ; for ( var i in items) { var item = items[i]; if ( item.x < mouseEventX && mouseEventX < item.x + iconSize && item.y < mouseEventY && mouseEventY< item.y + iconSize) { /* instead of loading the egg in runtime, load it once (eggImage) at startup and use it. Because for a web view, it takes a second to load the image, which looks bad.*/ //var img = new Image(); //img.src = item.egg; eggs.push({ x: item.x, y: item.y + iconSize, speed: eggSpeed, image: eggImage, url: item.url }); mouseEventFlag = false ; return ; } } } function drawBackground() { context.drawImage(background, 0, 0); } function drawWaves(){ context.drawImage(waveImage1, wave1X, wave1Y); context.drawImage(waveImage2, wave2X, wave2Y); context.drawImage(waveImage3, wave3X, wave3Y); } function drawItems() { for ( var i in items) { var item = items[i]; context.drawImage(item.image, item.x, item.y); context.save(); context.translate(item.x+10, item.y+10); context.rotate(-item.wingRotation); context.drawImage(wingImage, -wingSize, -wingSize); context.restore(); } } function drawNest(){ context.drawImage(nest.image, nest.x, nest.y); } function drawEggs() { for ( var i in eggs) { var egg = eggs[i]; context.drawImage(egg.image, egg.x, egg.y); } } canvas.onmousedown = function (e) { var mousePos = getMousePos(canvas, e); mouseEventX = mousePos.x; // - currentTranslationX; mouseEventY = mousePos.y; // - currentTranslationY; mouseEventFlag = true ; }; canvas.onmouseup = function (e) { mouseEventFlag = false ; }; function getMousePos(canvas, evt){ // get canvas position var obj = canvas; var top = 0; var left = 0; while (obj && obj.tagName != 'BODY' ) { top += obj.offsetTop; left += obj.offsetLeft; obj = obj.offsetParent; } // return relative mouse position var mouseX = evt.clientX - left + window.pageXOffset; var mouseY = evt.clientY - top + window.pageYOffset; return { x: mouseX, y: mouseY }; } |
Resources:
HTML5 Canvas Deep Dive
HTML5 Canvas Mouse Coordinates Tutorial