Water bubbles generation in html5 canvas using javascript

how to produce or generate or make or create water bubbles 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 article explains how to create the realistic bubbles generator in water for fish tank 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>Water bubbles generation using html5 canvas - Nibunan</title>
      </head>
      <body style="margin: 0px">
      <canvas width="700" height="450" id="drawing-surface"></canvas>
      <script type="text/javascript">
      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 bubble = new Bubble();
      var size = RandomFloat(15);
      bubble.size = { width: size + 1, height: size + 1 };
      viewport.addObject(bubble);
      }
      for (var i = 0; i < 500; i++) {
      var bubble = new Bubble();
      var size = RandomFloat(4);
      bubble.size = { width: size + 1, height: size + 1 };
      viewport.addObject(bubble);
      }
      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.jpg";
      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);
      }
      }
    

Bubble


      function Bubble() {
      var me = this;
      me.reset = function () {
      me.wind = RandomFloat(1) - 1.5;
      me.speed = RandomFloat(5) + 1;
      me.location = { x: 300, y: window.innerHeight };
      };
      me.reset();
      var image = new Image();
      image.src = "bubble.png";
      image.onload = function () {
      me.image = image;
      }
      me.move = function () {
      if (me.location.y < 0 || me.location.x < 0 || me.location.x > window.innerWidth) {
      me.reset();
      }
      me.location.x += me.wind;
      me.location.y -= me.speed;
      };
      me.paint = function (context) {
      if (!me.image) return;
      me.move();
      context.drawImage(me.image, me.location.x, me.location.y,me.size.width,me.size.height);
      };
      }