Last active
July 16, 2018 17:03
-
-
Save Bubblemelon/5af9671c36fd554a79f193473e913a1d to your computer and use it in GitHub Desktop.
Ocean waves Javascript Animation External Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Waves( $canvas, $width, $height ){ | |
this.numberOfWaves = 10; | |
this.waveGap = 20; | |
this.width = Waves.width = $width; | |
this.height = Waves.height = $height; | |
Waves.globalY = 0; | |
this.move = 1; | |
this.ctx = $canvas.getContext( '2d' ); | |
this.colour = Math.round(Math.random()*255)+", "+Math.round(Math.random()*255)+", "+Math.round(Math.random()*255); | |
this.wavesArray = new Array(); | |
this.beginingY = Waves.height / 2; | |
while(this.numberOfWaves--){ | |
this.wavesArray.push(new Wave($canvas, this.beginingY, this.colour, 1 / this.numberOfWaves)); | |
this.beginingY += this.waveGap; | |
} | |
this.update = function(){ | |
var bL = this.wavesArray.length; | |
while( bL-- ){ | |
this.wavesArray[ bL ].update( ); | |
} | |
Waves.globalY += this.move; | |
if(Waves.globalY > (Waves.height / 2)-50){ | |
this.move = -1; | |
}else if(Waves.globalY < -(Waves.height / 2)){ | |
this.move = 1; | |
} | |
} | |
this.draw = function(){ | |
this.ctx.clearRect(0,0,this.width,this.height); | |
this.ctx.save(); | |
var bL = this.wavesArray.length; | |
while( bL-- ){ | |
this.wavesArray[ bL ].draw( ); | |
} | |
this.ctx.restore(); | |
} | |
} | |
function Wave($canvas, $y, $colour, $alpha ){ | |
this.ctx = $canvas.getContext( '2d' ); | |
this.force = 0; | |
this.wavePower = 40; | |
this.count = $y; | |
this.y = $y + Waves.globalY; | |
this.alpha = $alpha; | |
this.update = function(){ | |
this.y = $y + Waves.globalY; | |
this.force = Math.sin(this.count); | |
this.count += 0.05; | |
} | |
this.draw = function(){ | |
this.ctx.fillStyle = "rgba("+$colour+", "+this.alpha+")"; | |
this.ctx.beginPath(); | |
this.ctx.moveTo(0, this.y); | |
this.ctx.quadraticCurveTo(Waves.width / 4, this.y + ( this.wavePower * this.force ), Waves.width / 2, this.y); | |
this.ctx.quadraticCurveTo(Waves.width * 0.75, this.y - ( this.wavePower * this.force ), Waves.width, this.y); | |
this.ctx.lineTo(Waves.width, Waves.height); | |
this.ctx.lineTo(0, Waves.height); | |
this.ctx.lineTo(0, this.y); | |
this.ctx.closePath(); | |
this.ctx.fill(); | |
} | |
} | |
//CALLS to Make Waves | |
var canvas = null; | |
var waves = null; | |
function loadCanvas( ) { | |
canvas = document.getElementById( 'canvas' ); | |
canvas.width = window.innerWidth; | |
canvas.height = window.innerHeight; | |
waves = new Waves( canvas, 1000, 800 ); | |
setInterval( "run()", 80 ); | |
} | |
function run(){ | |
waves.update( ); | |
waves.draw( ); | |
} | |
window.addEventListener( 'load', loadCanvas, false ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment