Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/*jslint plusplus: true, sloppy: true, indent: 4 */ | ||
var canvas = null, | ||
ctx = null, | ||
letters = [], | ||
bg = null; | ||
|
||
(function () { | ||
|
||
"use strict"; | ||
// this function is strict... | ||
// RequestAnimFrame: a browser API for getting smooth animations | ||
window.requestAnimFrame = (function () { | ||
return window.requestAnimationFrame || | ||
window.webkitRequestAnimationFrame || | ||
window.mozRequestAnimationFrame || | ||
window.oRequestAnimationFrame || | ||
window.msRequestAnimationFrame || | ||
function (callback) { | ||
window.setTimeout(callback, 1000 / 60); | ||
}; | ||
})(); | ||
|
||
}()); | ||
|
||
function clearCanvas() { | ||
// clear canvas | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
} | ||
|
||
function update() { | ||
var i = letters.length; | ||
clearCanvas(); | ||
while (i--) { | ||
letters[i].move(); | ||
letters[i].draw(); | ||
} | ||
} | ||
|
||
function setUpLetterArray() { | ||
return [ | ||
{ | ||
/* G */ | ||
bgimagex: 0, | ||
bgimagey: 0, | ||
factor: 1, | ||
height: 156, | ||
width: 130, | ||
top: (0 - Math.floor((Math.random() * 400) + 1)) | ||
}, | ||
{ | ||
/* o */ | ||
bgimagex: 133, | ||
bgimagey: 35, | ||
factor: 1, | ||
height: 100, | ||
width: 90, | ||
top: (0 - Math.floor((Math.random() * 400) + 1)) | ||
}, | ||
{ | ||
/* o */ | ||
bgimagex: 222, | ||
bgimagey: 35, | ||
factor: 1, | ||
height: 100, | ||
width: 90, | ||
top: (0 - Math.floor((Math.random() * 400) + 1)) | ||
}, | ||
{ | ||
/* g */ | ||
bgimagex: 315, | ||
bgimagey: 50, | ||
factor: 1, | ||
height: 142, | ||
width: 88, | ||
top: (0 - Math.floor((Math.random() * 400) + 1)) | ||
}, | ||
{ | ||
/* l */ | ||
bgimagex: 405, | ||
bgimagey: 0, | ||
factor: 1, | ||
height: 192, | ||
width: 40, | ||
top: (0 - Math.floor((Math.random() * 400) + 1)) | ||
}, | ||
{ | ||
/* e */ | ||
bgimagex: 450, | ||
bgimagey: 45, | ||
factor: 1, | ||
height: 100, | ||
width: 75, | ||
top: (0 - Math.floor((Math.random() * 400) + 1)) | ||
} | ||
]; | ||
} | ||
|
||
function loop() { | ||
update(); | ||
window.requestAnimFrame(loop); | ||
} | ||
|
||
function setUpBalls() { | ||
var iCounter, | ||
x = 0, | ||
lettersConf = setUpLetterArray(); | ||
|
||
for(iCounter = 0; iCounter < lettersConf.length; iCounter++) { | ||
letters.push(new Letter(x, bg, lettersConf[iCounter])); | ||
x += lettersConf[iCounter].width; | ||
} | ||
|
||
loop(); | ||
} | ||
|
||
function loadBackground(callback) { | ||
// Load the background | ||
bg = new Image(); | ||
bg.src = 'background.png'; | ||
bg.onload = callback; | ||
} | ||
|
||
window.addEventListener('load', function (ev) { | ||
canvas = document.getElementById('canvas'); | ||
ctx = canvas.getContext('2d'); | ||
loadBackground(function() { setUpBalls(); }); | ||
}, false); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Bounce Animation</title> | ||
<script src='bounce.js'></script> | ||
<script src='letter.js'></script> | ||
</head> | ||
<body> | ||
<canvas id="canvas" width="600" height="600">Canvas Not Supported</canvas> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var Letter = function (x, bg, ballSettings) { | ||
this.vy = 0; | ||
this.vx = 0; | ||
this.vyAdjust = -13; | ||
this.width = ballSettings.width; | ||
this.height = ballSettings.height; | ||
this.x = x; | ||
this.y = ballSettings.top; | ||
this.originaly = ballSettings.top; | ||
this.imagex = ballSettings.bgimagex; | ||
this.imagey = ballSettings.bgimagey; | ||
this.bg = bg; | ||
this.bounceFactor = ballSettings.factor; | ||
this.bounces = 0; | ||
|
||
this.draw = function () { | ||
ctx.drawImage(this.bg, | ||
this.imagex, this.imagey, | ||
this.width, this.height, | ||
this.x, this.y, | ||
this.width, this.height | ||
); | ||
}; | ||
|
||
this.impact = function () { | ||
this.vy = this.vyAdjust; | ||
this.bounces++; | ||
}; | ||
|
||
this.move = function () { | ||
this.y += this.vy; | ||
this.vy += 0.25; //gravity; | ||
// Bounce the ball when it hits the bottom | ||
if(this.bounces > 1) { | ||
|
||
if ((this.y + this.height) > canvas.height + 200) { | ||
this.bounces = 0; | ||
this.vyAdjust = -13; | ||
this.y = this.originaly; | ||
} | ||
} | ||
else if ((this.y + this.height) > canvas.height - 10) { | ||
this.impact(); | ||
this.vyAdjust = (this.vyAdjust * this.bounceFactor); | ||
} | ||
}; | ||
}; |