How to create realistic waterfall simulation in html5 canvas using javascript

how to make waterfall effect in html5 canvas using javascript - Nibunan

how to create waterfall in html5 canvas using javascript - Nibunan

how to write program to display waterfall effect in html5 canvas using javascript - Nibunan

how to write program to create waterfall animation in html5 canvas using javascript - Nibunan

how to write code to create waterfall effect in html5 canvas using javascript - Nibunan

Html5 canvas is evolving to give the rich graphics on browser without using flash or other plugins. Html5 canvas is being used in many places like games animations and rich user interfaces on web and so on.This airticle explains how to create realistic waterfall simulation in html5 canvas using javascript.

Demo

Open with new tab

Source


      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title>Creating waterfall effect using html5 canvas - Nibunan</title>
      </head>
      <body style="margin: 0px">
      <canvas width="700" height="450" id="drawing-surface"></canvas>
      <audio style="display:none" id="myAudio" controls="" loop="">
      <source src="rain.wav" />
      </audio>
      <script type="text/javascript">
      var audio = document.getElementById("myAudio");
      var timer = setTimeout(function () { audio.play() }, 2000);

      function Random(range) {
      return Math.floor((Math.random() * range) + 1);
      }
      function RandomFloat(range) {
      return (Math.random() * range) + 1;
      }
      var viewport = new Viewport();
      for (var i = 0; i < 500; i++) {
      var particle = new Particle();
      viewport.addObject(particle);
      }
      viewport.init();
      </script>
      </body>
      </html>
    

Viewport


      function Viewport() {
      var me = this;
      me.objects = [];
      var canvas = document.getElementById("drawing-surface");
      function resizeCanvas() {
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
      }
      resizeCanvas();
      window.onresize = function () {
      resizeCanvas();
      }
      var drawingSurface = document.getElementById("drawing-surface").getContext("2d");
      var image = new Image();
      image.src = "background.png";
      image.onload = function () {
      me.backgroundImage = image;
      me.doPaint = true;
      }
      me.doPaint = true;
      function drawBackgroundImage() {
      if (me.backgroundImage) {
      drawingSurface.drawImage(me.backgroundImage, 0, 0, canvas.width, canvas.height);
      }
      }
      function drawObjects() {
      for (var i = 0; i < me.objects.length; i++) {
      me.objects[i].paint(drawingSurface);
      }
      }
      me.paintInit = function () {
      me.doPaint = true;
      }
      function paint() {
      me.paintInit();
      if (me.doPaint) {
      drawBackgroundImage();
      drawObjects();
      me.doPaint = false;
      }
      }
      var timer;
      me.init = function () {
      timer = setInterval(function () { paint(); }, 1000 / 30);
      };
      me.repaint = function () {
      me.doPaint = true;
      };
      me.addObject = function (obj) {
      me.objects.push(obj);
      }
      }
    

Water Particle


      function Particle() {
      var me = this;
      var source = {};
      source.location = { x: window.innerWidth / 2, y: 0 };
      me.reset = function () {
      me.speed = RandomFloat(10) + 5;
      me.radius = 20;
      me.opacity = 10;
      me.color = 'rgb(255,255,255)';
      me.location = { x: source.location.x + Random(100), y: source.location.y };
      me.angle = 45;
      };
      me.reset();

      me.move = function () {
      if (me.location.y>window.innerHeight || me.location.x > window.innerWidth) {
      me.reset();
      }
      var xval = Math.cos(me.angle);
      if (xval > 0) {
      me.angle += 0.01;
      }
      else {
      me.angle -= 0.01;
      }
      me.location.x += Math.cos(me.angle) * me.speed;
      me.location.y += Math.sin(me.angle) * me.speed;
      };
      me.paint = function (context) {
      me.move();
      context.beginPath();
      context.arc(me.location.x, me.location.y, me.radius, 0, 2 * Math.PI, false);
      context.fillStyle = me.color;
      context.globalAlpha = me.opacity / 255;
      context.fill();
      context.closePath();
      };
      }