You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
8.1 KiB

4 years ago
  1. <html lang="en">
  2. <head>
  3. <title>Clouds</title>
  4. <meta charset="utf-8">
  5. <style type="text/css">
  6. *{
  7. box-sizing: border-box;
  8. margin: 0;
  9. padding: 0
  10. }
  11. body {
  12. color: #eee;
  13. text-shadow: 0 -1px 0 rgba( 0, 0, 0, .6 );
  14. font-family: 'Open Sans', sans-serif;
  15. font-size: 13px;
  16. line-height: 16px;
  17. overflow: hidden;
  18. }
  19. #viewport {
  20. -webkit-perspective: 1000;
  21. -moz-perspective: 1000px;
  22. -o-perspective: 1000;
  23. perspective: 1000px;
  24. position: absolute;
  25. left: 0;
  26. top: 0;
  27. right: 0;
  28. bottom: 0;
  29. overflow: hidden;
  30. background-image: url('http://www.incoplast-srl.com/SiteAssets/Cielo-despejado-1363594685_18.jpg');
  31. background-size: cover;
  32. /*background-image: linear-gradient(bottom, rgb(69,132,180) 28%, rgb(31,71,120) 64%);
  33. background-image: -o-linear-gradient(bottom, rgb(69,132,180) 28%, rgb(31,71,120) 64%);
  34. background-image: -moz-linear-gradient(bottom, rgb(69,132,180) 28%, rgb(31,71,120) 64%);
  35. background-image: -webkit-linear-gradient(bottom, rgb(69,132,180) 28%, rgb(31,71,120) 64%);
  36. background-image: -ms-linear-gradient(bottom, rgb(69,132,180) 28%, rgb(31,71,120) 64%);
  37. background-image: -webkit-gradient(
  38. linear,
  39. left bottom,
  40. left top,
  41. color-stop(0.28, rgb(69,132,180)),
  42. color-stop(0.64, rgb(31,71,120))
  43. );*/
  44. }
  45. #world {
  46. position: absolute;
  47. left: 50%;
  48. top: 50%;
  49. margin-left: -256px;
  50. margin-top: -256px;
  51. height: 512px;
  52. width: 512px;
  53. -webkit-transform-style: preserve-3d;
  54. -moz-transform-style: preserve-3d;
  55. -o-transform-style: preserve-3d;
  56. transform-style: preserve-3d;
  57. pointer-events: none;
  58. }
  59. #world div {
  60. -webkit-transform-style: preserve-3d;
  61. -moz-transform-style: preserve-3d;
  62. -o-transform-style: preserve-3d;
  63. transform-style: preserve-3d;
  64. }
  65. .cloudBase {
  66. position: absolute;
  67. left: 256px;
  68. top: 256px;
  69. width: 20px;
  70. height: 20px;
  71. margin-left: -10px;
  72. margin-top: -10px;
  73. }
  74. .cloudLayer {
  75. position: absolute;
  76. left: 50%;
  77. top: 50%;
  78. width: 256px;
  79. height: 256px;
  80. margin-left: -128px;
  81. margin-top: -128px;
  82. -webkit-transition: opacity .5s ease-out;
  83. -moz-transition: opacity .5s ease-out;
  84. -o-transition: opacity .5s ease-out;
  85. transition: opacity .5s ease-out;
  86. }
  87. </style>
  88. <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
  89. <script type="text/javascript">
  90. $( document ).ready(function() {
  91. /*
  92. Defining our variables
  93. world and viewport are DOM elements,
  94. worldXAngle and worldYAngle are floats that hold the world rotations,
  95. d is an int that defines the distance of the world from the camera
  96. */
  97. var world = document.getElementById( 'world' ),
  98. viewport = document.getElementById( 'viewport' ),
  99. worldXAngle = 0,
  100. worldYAngle = 0,
  101. d = 0;
  102. /*
  103. Event listener to transform mouse position into angles
  104. from -180 to 180 degress, both vertically and horizontally
  105. */
  106. window.addEventListener( 'mousemove', function( e ) {
  107. worldYAngle = -( .5 - ( e.clientX / window.innerWidth ) ) * 360;
  108. worldXAngle = ( .5 - ( e.clientY / window.innerHeight ) ) * 360;
  109. updateView();
  110. } );
  111. /*
  112. Changes the transform property of world to be
  113. translated in the Z axis by d pixels,
  114. rotated in the X axis by worldXAngle degrees and
  115. rotated in the Y axis by worldYAngle degrees.
  116. */
  117. function updateView() {
  118. world.style.transform = 'translateZ( ' + d + 'px ) \
  119. rotateX( ' + worldXAngle + 'deg) \
  120. rotateY( ' + worldYAngle + 'deg)';
  121. }
  122. /*
  123. objects is an array of cloud bases
  124. layers is an array of cloud layers
  125. */
  126. var objects = [],
  127. layers = [];
  128. textures = [
  129. { name: 'white cloud', file: 'cloud.png' , opacity: 1, weight: 0 },
  130. { name: 'dark cloud', file: 'darkCloud.png' , opacity: 1, weight: 0 },
  131. { name: 'smoke cloud', file: 'smoke.png' , opacity: 1, weight: 0 },
  132. { name: 'explosion', file: 'explosion.png' , opacity: 1, weight: 0 },
  133. { name: 'explosion 2', file: 'explosion2.png' , opacity: 1, weight: 0 },
  134. { name: 'box', file: 'box.png' , opacity: 1, weight: 0 }
  135. ];
  136. /*
  137. Clears the DOM of previous clouds bases
  138. and generates a new set of cloud bases
  139. */
  140. function generate() {
  141. objects = [];
  142. if ( world.hasChildNodes() ) {
  143. while ( world.childNodes.length >= 1 ) {
  144. world.removeChild( world.firstChild );
  145. }
  146. }
  147. computedWeights = [];
  148. var total = 0;
  149. for( var j = 0; j < textures.length; j++ ) {
  150. if( textures[ j ].weight > 0 ) {
  151. total += textures[ j ].weight;
  152. }
  153. }
  154. var accum = 0;
  155. for( var j = 0; j < textures.length; j++ ) {
  156. if( textures[ j ].weight > 0 ) {
  157. var w = textures[ j ].weight / total;
  158. computedWeights.push( {
  159. src: textures[ j ].file,
  160. min: accum,
  161. max: accum + w
  162. } );
  163. accum += w;
  164. }
  165. }
  166. for( var j = 0; j < 100; j++ ) {
  167. objects.push( createCloud() );
  168. }
  169. }
  170. /*
  171. Creates a single cloud base and adds several cloud layers.
  172. Each cloud layer has random position ( x, y, z ), rotation (a)
  173. and rotation speed (s). layers[] keeps track of those divs.
  174. */
  175. function createCloud() {
  176. var div = document.createElement( 'div' );
  177. div.className = 'cloudBase';
  178. var x = 256 - ( Math.random() * 1512 );
  179. var y = 256 - ( Math.random() * 1512 );
  180. var z = 256 - ( Math.random() * 1512 );
  181. var t = 'translateX( ' + x + 'px ) translateY( ' + y + 'px ) translateZ( ' + z + 'px )';
  182. div.style.webkitTransform =
  183. div.style.MozTransform =
  184. div.style.oTransform =
  185. div.style.transform = t;
  186. world.appendChild( div );
  187. for( var j = 0; j < 5 + Math.round( Math.random() * 10 ); j++ ) {
  188. var cloud = document.createElement( 'img' );
  189. cloud.style.opacity = 1;
  190. var r = Math.random();
  191. var src = 'cloud10.png';
  192. cloud.setAttribute( 'src', src );
  193. cloud.className = 'cloudLayer';
  194. var x = 256 - ( Math.random() * 1512 );
  195. var y = 256 - ( Math.random() * 1512 );
  196. var z = 100 - ( Math.random() * 1200 );
  197. var a = Math.random() * 360;
  198. var s = .25 + Math.random();
  199. x *= .2; y *= .2;
  200. cloud.data = {
  201. x: x,
  202. y: y,
  203. z: z,
  204. a: a,
  205. s: s,
  206. speed: .1 * Math.random(),
  207. r: Math.random()
  208. };
  209. var t = 'translateX( ' + x + 'px ) translateY( ' + y + 'px ) translateZ( ' + z + 'px ) rotateZ( ' + a + 'deg ) scale( ' + s + ' )';
  210. cloud.style.webkitTransform =
  211. cloud.style.MozTransform =
  212. cloud.style.oTransform =
  213. cloud.style.transform = t;
  214. div.appendChild( cloud );
  215. layers.push( cloud );
  216. }
  217. return div;
  218. }
  219. generate();
  220. function update (){
  221. for( var j = 0; j < layers.length; j++ ) {
  222. var layer = layers[ j ];
  223. if(Math.random() >= layer.data.r){
  224. layer.data.a += layer.data.speed;
  225. }else{
  226. layer.data.a -= layer.data.speed;
  227. }
  228. if(Math.random() >= layer.data.r){
  229. layer.data.x += layer.data.speed;
  230. }else{
  231. layer.data.x -= layer.data.speed;
  232. }
  233. if(Math.random() >= layer.data.r){
  234. layer.data.y += layer.data.speed;
  235. }else{
  236. layer.data.y -= layer.data.speed;
  237. }
  238. 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 + ')';
  239. layer.style.webkitTransform =
  240. layer.style.MozTransform =
  241. layer.style.oTransform =
  242. layer.style.transform = t;
  243. //layer.style.webkitFilter = 'blur(5px)';
  244. }
  245. requestAnimationFrame( update );
  246. }
  247. update();
  248. });
  249. </script>
  250. </head>
  251. <body>
  252. <div id="viewport" >
  253. <div id="world" ></div>
  254. </div>
  255. </body>
  256. </html>