Today I have prepared you something really entertaining. Of course, we implement it with our favorite html5. This work will show you how to create a beautiful flying clouds across the sky. In the implementation, I decided to use an additional library: https://github.com/mrdoob/three.js
Here are our demo and downloadable package:
Ok, download the source files and lets start coding !
Step 1. HTML
Here are html code of our clouds page
index.html
04 |
< meta charset = "utf-8" /> |
05 |
< title >HTML5 clouds | Script Tutorials</ title > |
06 |
< link href = "css/main.css" rel = "stylesheet" type = "text/css" /> |
07 |
< script src = "js/ThreeWebGL.js" ></ script > |
08 |
< script src = "js/ThreeExtras.js" ></ script > |
11 |
< script id = "vs" type = "x-shader/x-vertex" > |
15 |
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); |
18 |
< script id = "fs" type = "x-shader/x-fragment" > |
19 |
uniform sampler2D map; |
20 |
uniform vec3 fogColor; |
21 |
uniform float fogNear; |
25 |
float depth = gl_FragCoord.z / gl_FragCoord.w; |
26 |
float fogFactor = smoothstep( fogNear, fogFar, depth ); |
27 |
gl_FragColor = texture2D( map, vUv ); |
28 |
gl_FragColor.w *= pow( gl_FragCoord.z, 20.0 ); |
29 |
gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor ); |
33 |
< div class = "container" > |
34 |
< canvas id = "panel" width = "10" height = "1" ></ canvas > |
36 |
< script type = "text/javascript" src = "js/script.js" ></ script > |
Step 2. CSS
Here are used CSS styles
css/main.css
07 |
font : 14px / 1.3 Arial , sans-serif ; |
08 |
background-image : url (../images/sky.jpg); |
11 |
background-color : #212121 ; |
13 |
box-shadow: 0 -1px 2px #111111 ; |
30 |
footer a.stuts,a.stuts:visited{ |
37 |
margin : 23px 0 0 110px ; |
Step 3. JS
js/ThreeWebGL.js and js/ThreeExtras.js
This is our new service libraries (available in package)
js/script.js
003 |
var camera, scene, renderer, meshMaterial, mesh, geometry, i; |
004 |
var mouseX = 0, mouseY = 0; |
005 |
var startTime = new Date().getTime(); |
006 |
var windowHalfX = window.innerWidth / 2; |
007 |
var windowHalfY = window.innerHeight / 2; |
009 |
if (window.attachEvent) { |
010 |
window.attachEvent( 'onload' , main_init); |
013 |
var curronload = window.onload; |
014 |
var newonload = function () { |
018 |
window.onload = newonload; |
020 |
window.onload = main_init; |
024 |
function main_init() { |
027 |
canvas = document.getElementById( 'panel' ); |
028 |
var ctx = canvas.getContext( '2d' ); |
031 |
camera = new THREE.Camera(30, window.innerWidth / window.innerHeight, 1, 5000); |
032 |
camera.position.z = 6000; |
035 |
scene = new THREE.Scene(); |
038 |
geometry = new THREE.Geometry(); |
041 |
var texture = THREE.ImageUtils.loadTexture( 'images/clouds.png' ); |
042 |
texture.magFilter = THREE.LinearMipMapLinearFilter; |
043 |
texture.minFilter = THREE.LinearMipMapLinearFilter; |
046 |
var fog = new THREE.Fog(0x251d32, - 100, 5000); |
049 |
meshMaterial = new THREE.MeshShaderMaterial({ |
051 |
'map' : {type: 't' , value:2, texture: texture}, |
052 |
'fogColor' : {type: 'c' , value: fog.color}, |
053 |
'fogNear' : {type: 'f' , value: fog.near}, |
054 |
'fogFar' : {type: 'f' , value: fog.far}, |
057 |
vertexShader: document.getElementById( 'vs' ).textContent, |
058 |
fragmentShader: document.getElementById( 'fs' ).textContent, |
063 |
var planeMesh = new THREE.Mesh( new THREE.PlaneGeometry(64, 64)); |
064 |
for (i = 0; i < 10000; i++) { |
065 |
planeMesh.position.x = Math.random() * 1000 - 500; |
066 |
planeMesh.position.y = - Math.random() * Math.random() * 200 - 15; |
067 |
planeMesh.position.z = i; |
068 |
planeMesh.rotation.z = Math.random() * Math.PI; |
069 |
planeMesh.scale.x = planeMesh.scale.y = Math.random() * Math.random() * 1.5 + 0.5; |
071 |
THREE.GeometryUtils.merge(geometry, planeMesh); |
074 |
mesh = new THREE.Mesh(geometry, meshMaterial); |
075 |
scene.addObject(mesh); |
077 |
mesh = new THREE.Mesh(geometry, meshMaterial); |
078 |
mesh.position.z = - 10000; |
079 |
scene.addObject(mesh); |
082 |
renderer = new THREE.WebGLRenderer({ antialias: false }); |
083 |
renderer.setSize(window.innerWidth, window.innerHeight); |
084 |
document.body.appendChild(renderer.domElement); |
087 |
document.addEventListener( 'mousemove' , onMousemove, false ); |
090 |
window.addEventListener( 'resize' , onResize, false ); |
092 |
setInterval(drawScene, 30); |
095 |
function onMousemove(event) { |
096 |
mouseX = (event.clientX - windowHalfX) * 0.3; |
097 |
mouseY = (event.clientY - windowHalfY) * 0.2; |
100 |
function onResize(event) { |
101 |
camera.aspect = window.innerWidth / window.innerHeight; |
102 |
camera.updateProjectionMatrix(); |
103 |
renderer.setSize(window.innerWidth, window.innerHeight); |
106 |
function drawScene() { |
107 |
position = (( new Date().getTime() - startTime) * 0.1) % 10000; |
109 |
camera.position.x += mouseX * 0.01; |
110 |
camera.position.y += - mouseY * 0.01; |
111 |
camera.position.z = - position + 10000; |
113 |
renderer.render(scene, camera); |
Most of code will pretty easy for understanding – I have tried to comment this code as possible.