-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.js
86 lines (69 loc) · 3.26 KB
/
app.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
let scene = document.querySelector('a-scene');
let sky = document.querySelector('a-sky');
let objectContainer = document.querySelector('#object-container');
// random num generator
function getRandomNumber(x, y) {
return Math.floor(Math.random() * x + y);
}
// get random hex color
function getRandomColor() {
let letters = '0123456789abcdef';
let randomColor = '';
for (let i = 0; i < 6; i++) {
randomColor += letters[Math.floor(Math.random() * 16)];
}
return randomColor;
}
// set sky values
sky.setAttribute('color', `#${getRandomColor()}`);
sky.setAttribute('animation__color', `property: color; dir: alternate; dur: 2000; easing: easeInOutSine; loop: true; to: #${getRandomColor()}`);
// change this value for more or less rings
let totalRingElements = 10;
function generateAllElements() {
for(let a = 0; a < totalRingElements; a++){
// element params
let totalCircleElements = getRandomNumber(10, 3);
let elementScale = getRandomNumber(3, 1);
let scaleDuration = getRandomNumber(3000, 1000);
// path params
let pathValOne = getRandomNumber(21, -10);
let pathValTwo = getRandomNumber(11, -20);
let pathDuration = getRandomNumber(6000, 5000);
for (let i = 1; i <= totalCircleElements; i++) {
let currentRotation = 360 / totalCircleElements * i;
let rotateContainer = document.createElement('a-entity');
rotateContainer.setAttribute('rotation', `0 0 ${currentRotation}`);
// generate circle element and set params
let circleElementContainer = document.createElement('a-entity');
circleElementContainer.setAttribute('position', `0 1 0`);
let circleElement = document.createElement('a-entity');
circleElement.setAttribute('class', `circleElement`);
circleElement.setAttribute('scale', `${elementScale} ${elementScale} ${elementScale}`);
circleElement.setAttribute('material', `color:#${getRandomColor()}; metalness: 0; roughness: 0`);
circleElement.setAttribute('geometry', `primitive: sphere; radius: 1.5`);
circleElement.setAttribute('animation__yoyo', `property: scale; dir: alternate; dur: ${scaleDuration}; easing: easeInOutSine; loop: true; to: 0 0 0`);
circleElementContainer.appendChild(circleElement);
rotateContainer.appendChild(circleElementContainer);
// generate path and apply it
let track1 = document.createElement('a-curve');
track1.setAttribute('class', `track${a}`);
scene.append(track1);
let point1 = document.createElement('a-curve-point');
point1.setAttribute('position', '0 0 0');
track1.append(point1);
let point2 = document.createElement('a-curve-point');
point2.setAttribute('position', `${pathValOne} ${pathValTwo} ${pathValOne}`);
track1.append(point2);
let point3 = document.createElement('a-curve-point');
point3.setAttribute('position', `${pathValTwo} ${pathValOne} ${pathValTwo}`);
track1.append(point3);
let point4 = document.createElement('a-curve-point');
point4.setAttribute('position', '0 0 0');
track1.append(point4);
circleElement.setAttribute(`alongpath`, `curve: .track${a}; dur: ${pathDuration}; loop: true`);
// append element to main container
objectContainer.appendChild(rotateContainer);
}
}
}
generateAllElements()