-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
178 lines (155 loc) · 5.51 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Defining our variables
world and viewport are DOM elements,
worldXAngle and worldYAngle are floats that hold the world rotations,
d is an int that defines the distance of the world from the camera
*/
var world = document.getElementById( 'world' ),
viewport = document.getElementById( 'viewport' ),
worldXAngle = 0,
worldYAngle = 0,
d = 0;
/*
Event listener to transform mouse position into angles
from -180 to 180 degress, both vertically and horizontally
*/
window.addEventListener( 'mousemove', function( e ) {
worldYAngle = -( .5 - ( e.clientX / window.innerWidth ) ) * 360;
worldXAngle = ( .5 - ( e.clientY / window.innerHeight ) ) * 360;
updateView();
} );
window.addEventListener('mousewheel', onContainerMouseWheel);
window.addEventListener('DOMMouseScroll', onContainerMouseWheel);
/*
Changes the transform property of world to be
translated in the Z axis by d pixels,
rotated in the X axis by worldXAngle degrees and
rotated in the Y axis by worldYAngle degrees.
*/
function updateView() {
var t = 'translateZ( ' + d + 'px ) rotateX( ' + worldXAngle + 'deg) rotateY( ' + worldYAngle + 'deg)';
world.style.webkitTransform = t;
}
///////////////
/*
objects is an array of cloud bases
layers is an array of cloud layers
*/
var objects = [],
layers = [];
/*
Clears the DOM of previous clouds bases
and generates a new set of cloud bases
*/
function generate() {
objects = [];
layers = [];
if ( world.hasChildNodes() ) {
while ( world.childNodes.length >= 1 ) {
world.removeChild( world.firstChild );
}
}
//Create j amount of clouds
for( var j = 0; j < 10; j++ ) {
//Push cloud created from createCloud() function to objects array
objects.push( createCloud() );
}
}
generate();
function makeMoon(){
var moon = document.createElement( 'div' );
moon.className = 'moon';
world.appendChild( moon);
var random_x = 256 - ( Math.random() * 512 );
var random_y = 256 - ( Math.random() * 512 );
var random_z = 256 - ( Math.random() * 512 );
moon.data = {
x: random_x,
y: random_y,
z: random_z
};
var t = 'translateX( ' + random_x + 'px ) translateY( ' + random_y + 'px ) translateZ( ' + random_z + 'px )';
moon.style.webkitTransform = t;
objects.push(moon);
};
makeMoon();
/*
Creates a single cloud base: a div in world
that is translated randomly into world space.
Each axis goes from -256 to 256 pixels.
*/
function createCloud() {
var div = document.createElement( 'div' );
div.className = 'cloudBase';
var random_x = 256 - ( Math.random() * 512 );
var random_y = 256 - ( Math.random() * 512 );
var random_z = 256 - ( Math.random() * 512 );
var t = 'translateX( ' + random_x + 'px ) translateY( ' + random_y + 'px ) translateZ( ' + random_z + 'px )';
div.style.webkitTransform = t;
var colorR = Math.floor(Math.random()*255);
var colorG = Math.floor(Math.random()*255);
var colorB = Math.floor(Math.random()*255);
// div.style.backgroundColor = "rgb(" + colorR+"," + colorG+ ","+ colorB + ")";
world.appendChild( div );
//add layers to cloud
for( var j = 0; j < 10 + Math.round( Math.random() * 10 ); j++ ) {
var cloud = document.createElement( 'img' );
cloud.setAttribute('src', "cloud.png");
cloud.className = 'cloudLayer';
// var random_x = 256 - ( Math.random() * 512 );
// var random_y = 256 - ( Math.random() * 512 );
var random_z = 100 - ( Math.random() * 200 );
var random_d = Math.random() * 360;
var random_s = .25 + Math.random();
// add an object for data to allow for the 3D effect
cloud.data = {
x: random_x,
y: random_y,
z: random_z,
a: random_d,
s: random_s,
speed: .1 * Math.random()
};
// var random_y = 256 - ( Math.random() * 512 );
var t = 'translateX( ' + random_x + 'px ) translateY( ' + random_y + 'px ) translateZ( ' + random_z + 'px ) rotateZ( ' + random_d + 'deg ) scale( ' + random_s + ' )';
cloud.style.webkitTransform = t;
div.appendChild( cloud );
layers.push( cloud );
}
return div;
}
//function to determine z translation viewpoint
function onContainerMouseWheel( event ) {
event = event ? event : window.event;
d = d - ( event.detail ? event.detail * -5 : event.wheelDelta / 8 );
updateView();
}
/*
Iterate layers[], update the rotation and apply the
inverse transformation currently applied to the world.
Notice the order in which rotations are applied.
*/
function update (){
for( var j = 0; j < layers.length; j++ ) {
var layer = layers[ j ];
layer.data.a += layer.data.speed;
var t = 'translateX( ' + layer.data.x + 'px ) \
translateY( ' + layer.data.y + 'px ) \
translateZ( ' + layer.data.z + 'px ) \
rotateY( ' + ( - worldYAngle ) + 'deg ) \
rotateX( ' + ( - worldXAngle ) + 'deg ) \
rotateZ( ' + (layer.data.a) + 'deg ) \
scale( ' + layer.data.s + ')';
layer.style.webkitTransform = t;
}
var moon = objects[10];
var m = 'translateX( ' + moon.data.x + 'px ) \
translateY( ' + moon.data.y + 'px ) \
translateZ( ' + moon.data.z + 'px ) \
rotateY( ' + ( - worldYAngle ) + 'deg ) \
rotateX( ' + ( - worldXAngle ) + 'deg ) \
rotateZ( ' + 10 + 'deg)'
moon.style.webkitTransform = m;
requestAnimationFrame( update );
}
update();