-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbounce.js
99 lines (85 loc) · 2.46 KB
/
bounce.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
var Bounce = function () {
/**
* Default settings for the module
* @type {{selector: string, gravity: number}}
*/
var defaultSettings = {
selector: '.bounce',
gravity: 9.81,
updateSpeed: 1 //In milliseconds
};
/**
* The bouncing element
* @type {HTMLElement}
*/
var element;
/**
* The vertical speed
* @type {number}
*/
var speedY;
/**
* The timer that updates the model and the screen
* @type {object}
*/
var timer;
/**
* The x and y position of the element
* @type {{x: number, y: number}}
*/
var position = {
x: 0,
y: 0
};
/**
* Updates the x and y position to the bouncing element
*/
var updateElement = function() {
element.style.marginLeft = position.x + 'px';
element.style.marginTop = position.y + 'px';
};
/**
* Moves changes the x and the y
* @param {number} xChange
* @param {number} yChange
*/
var move = function(xChange, yChange) {
position.x += xChange;
position.y += yChange;
//If the element reaches the bottom of the parent element reverse the speed
if(element.parentElement.clientHeight <= position.y + element.clientHeight) {
speedY = -speedY;
}
updateElement();
};
/**
* Update the variables to the new reality
*/
var update = function() {
move(0, speedY);
speedY += defaultSettings.gravity * (defaultSettings.updateSpeed/1000);
};
var mergeObjects = function(object1, object2) {
for (var attrname in object1) {
if(object2.hasOwnProperty(attrname)) {
object1[attrname] = object2[attrname];
}
}
};
/**
* Initializes the module
* @param {string} [selector] Css selector that targets the element that needs to bounce
* @param {object} [settings] Object that contain overrides for the default settings
*/
var init = function(selector, settings) {
mergeObjects(defaultSettings, settings || {});
selector = selector || defaultSettings.selector;
element = document.querySelector(selector);
speedY = 0;
timer = setInterval(update, defaultSettings.updateSpeed);
};
//Return the functions that should be accessible from the outside. The rest is only accessible from within the object
return {
init: init
};
};