Created
May 7, 2014 15:58
-
-
Save adamrbrown/8a6c3c1da7bb37f40c7f to your computer and use it in GitHub Desktop.
Creates floating bubbles on HTML5 Canvas
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
/** | |
* Splash bubbles | |
* Copyright © 85times.com | |
*/ | |
$(function() { | |
var canvas = $('#bubbles'); | |
var context = canvas.get(0).getContext("2d"); | |
var bubbles = new Array(); | |
var bubblesNum = 75; | |
var minRadius = 3; | |
var maxRadius = 50; | |
var minMove = 5; | |
var maxMove = 8; | |
for (var i = 0; i < bubblesNum; i++) { | |
bubbles[i] = { | |
xcoord: getRandomNum(Math.ceil(canvas.width() * 0.2), Math.ceil(canvas.width() * 0.8)), | |
ycoord: getRandomNum(0, canvas.height()), | |
radius: getRandomNum(minRadius, maxRadius), | |
move: getRandomNum(minMove, maxMove) | |
} | |
} | |
setInterval(function() { | |
context.clearRect(0, 0, canvas.width(), canvas.height()); | |
for (var i = 0; i < bubbles.length; i++) { | |
bubbles[i].ycoord -= bubbles[i].move; | |
if (bubbles[i].ycoord < (0 - maxRadius)) { | |
bubbles[i].xcoord = getRandomNum(Math.ceil(canvas.width() * 0.2) ,Math.ceil(canvas.width() * 0.8)); | |
bubbles[i].ycoord = canvas.height() + maxRadius; | |
bubbles[i].radius = getRandomNum(minRadius, maxRadius); | |
bubbles[i].move = getRandomNum(minMove, maxMove); | |
} | |
context.beginPath(); | |
context.arc(bubbles[i].xcoord, bubbles[i].ycoord, bubbles[i].radius, 0, Math.PI*2, false); | |
context.fillStyle = "rgba(127, 138, 153, 0.1)"; | |
context.fill(); | |
context.closePath(); | |
} | |
}, 1000/33); | |
}); | |
/** | |
* Get Random Integer | |
*/ | |
function getRandomInt (min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
/** | |
* Get Random Number | |
*/ | |
function getRandomNum(min, max) { | |
return (Math.random() * (max - min + 1)) + min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi !!! This is what I looking for to my work. Could you please help me to change the bubbles direction left to right or right to left