-
Notifications
You must be signed in to change notification settings - Fork 20
/
bouncing-ball.js
129 lines (121 loc) · 4.15 KB
/
bouncing-ball.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
import AnimationElement from "./animation-element.js";
class BouncingBallElement extends AnimationElement {
/**
* @type {Object<String, Array<Function>>}
*/
#eventListeners = {}
constructor(){
super();
this.attachShadow({mode: 'open'})
this.yvelocity = 0
this.timeelapsed = 0;
console.log("Parent element height: ");
console.log(this.parentElement.offsetHeight )
// add ball styling from HTML
this.backgroundColor = this.getAttribute('color') || null;
this.radius = this.getAttribute('radius') || null;
}
get velocity() {
return this.yvelocity;
}
connectedCallback(){
this.render()
}
/**
* Gets a random point at a given distance.
* Done using a parametric circle function given an angle.
* @property {function} render
* @member {Object} FloatingRandomElement
* @returns {void} Prints out FloatingRandomElement object
*
*/
render(){
this.shadowRoot.innerHTML = ""
const style = document.createElement('style');
style.innerHTML = `
:host{
position: absolute;
top: 0px;
left: 0px;
transition: ${this.animationSpeed/1000}s linear;
background-color: ${this.backgroundColor || 'blue'};
border-radius: 999px;
width: ${(this.radius * 2) || 40 }px;
height: ${(this.radius * 2) || 40 }px;
}
`;
this.shadowRoot.append(style);
this.shadowRoot.append(document.createElement('slot'));
this.addEventListener('click', ()=>{
if(this.getAttribute('clonable') != 'false') this.cloneNode()
})
}
/**
* @description Animates FloatingRandomElement object
* @property {function} animate
* @member {Object} FloatingRandomElement
* @returns {Object<number>} Angle, X and Y
*
*/
animate(){
// console.log("Hi")
/**
* Original x position of the element
* @const
* @type {number}
*
*/
const o_x = parseFloat( this.style.left ) || 0;
/**
* Original y position of the element
* @const
* @type {number}
*
*/
const o_y = parseFloat( this.style.top ) || 0;
const gravity = 10;
let x, y;
x = o_x;
this.yvelocity += gravity;
y = o_y + this.yvelocity;
const angle = 0;
if(y === this.parentElement.offsetHeight) {
this.yvelocity *= -1;
}
// collision detection
else if(y > this.parentElement.offsetHeight ) {
const ydelta = y - this.parentElement.offsetHeight;
// calculate time when ball bounces at parentElement offset height.
let bouncetime = Math.sqrt(this.parentElement.offsetHeight * 2 / gravity);
// set real y to offset height
y = this.parentElement.offsetHeight;
// set velocity to what it would be at the offsetheight, then make it go the reverse direction.
this.yvelocity = gravity * bouncetime;
this.yvelocity *= -1;
}
this.style.left = x + 'px';
this.style.top = y + 'px';
return {angle, x, y};
}
addClonableEventListener(event, callback, options){
console.log('Added');
if(this.#eventListeners[event] == undefined) this.#eventListeners[event] = []
this.#eventListeners[event].push(callback);
super.addEventListener(event, callback)
}
cloneNode(){
let clone = super.cloneNode(true);
for(let event in this.#eventListeners){
for(let callback of this.#eventListeners[event]){
clone.addClonableEventListener(event, callback.bind(clone))
}
}
this.parentElement.append(clone);
this.dispatchEvent(new CustomEvent('clone', {
detail: {node: clone, content: clone.shadowRoot.querySelector('slot').assignedElements()[0]}
}))
}
reset(){
}
}
customElements.define('bouncing-ball', BouncingBallElement)