-
Notifications
You must be signed in to change notification settings - Fork 20
/
floating-random.js
116 lines (108 loc) · 3.33 KB
/
floating-random.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
import AnimationElement from "./animation-element.js";
/**
* @class
* @classdesc Create a Floating Random Element
*
*/
class FloatingRandomElement extends AnimationElement {
/**
* @type {Object<String, Array<Function>>}
*/
#eventListeners = {}
constructor(){
super();
this.attachShadow({mode: 'open'})
}
get travelRadius(){
return parseInt(this.getAttribute('travel-radius')) || 10;
}
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 + 0.2*Math.random()}s linear;
}
`;
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;
/**
* Radius
* @const
* @type {number}
*
*/
const radius = this.travelRadius;
let angle, x, y;
do {
angle = Math.random()*2*Math.PI;
x = o_x + radius*Math.sin(angle);
y = o_y + radius*Math.cos(angle);
} while (x > this.parentElement.offsetWidth || y > this.parentElement.offsetHeight || x < 0 || y < 0);
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('floating-random-element', FloatingRandomElement)