Skip to content

Instantly share code, notes, and snippets.

@quezo
Created August 8, 2012 04:36
Show Gist options
  • Save quezo/3292076 to your computer and use it in GitHub Desktop.
Save quezo/3292076 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- IF PEN IS PRIVATE -->
<!-- <meta name="robots" content="noindex"> -->
<!-- END -->
<title>Tekken Particles &middot; CodePen</title>
<!--
Copyright (c) 2012 Timo Hausmann, http://codepen.io/whootboy
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<style>
* {
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
padding: 40px;
background: #333;
}
canvas {
display: block;
margin: 0 auto;
background: #000;
}
</style>
<style>
#codepen-footer, #codepen-footer * {
-webkit-box-sizing: border-box !important;
-moz-box-sizing: border-box !important;
box-sizing: border-box !important;
}
#codepen-footer {
display: block !important;
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
width: 100% !important;
padding: 0 10px !important;
margin: 0 !important;
height: 30px !important;
line-height: 30px !important;
font-size: 12px !important;
color: #eeeeee !important;
background-color: #505050 !important;
text-align: left !important;
background: -webkit-linear-gradient(top, #505050, #383838) !important;
background: -moz-linear-gradient(top, #505050, #383838) !important;
background: -ms-linear-gradient(top, #505050, #383838) !important;
background: -o-linear-gradient(top, #505050, #383838) !important;
border-top: 1px solid black !important;
border-bottom: 1px solid black !important;
border-radius: 0 !important;
border-image: none !important;
box-shadow: inset 0 1px 0 #6e6e6e, 0 2px 2px rgba(0, 0, 0, 0.4) !important;
z-index: 300 !important;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif !important;
letter-spacing: 0 !important;
word-spacing: normal !important;
word-spacing: 0 !important;
-webkit-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
-o-transform: none !important;
transform: none !important;
}
#codepen-footer a {
color: #a7a7a7 !important;
text-decoration: none !important;
text-shadow: none !important;
border: 0 !important;
}
#codepen-footer a:hover {
color: white !important;
}
</style>
<script>
// Kill alerts, confirmations and prompts
// window.alert = function(){}; we're going to allow alerts for now
window.confirm = function(){};
window.prompt = function(){};
window.open = function(){};
window.print = function(){};
</script>
</head>
<body>
<canvas id="myCanvas" width="800" height="250"></canvas>
<script>
(function() {
/*
* shim layer with setTimeout fallback
* @see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
/*
* FX will be our global object
*/
var FX = {};
/*
* basic canvas variables
*/
FX.canvas = document.getElementById('myCanvas');
FX.ctx = FX.canvas.getContext('2d');
FX.width = FX.canvas.width;
FX.height = FX.canvas.height;
//our particles will be stored in this array
FX.particles = [];
/*
* constructor for new particle
* @param Object args configuration object
* x : X-Position
* y : Y-Position
* z : Z-Index (defined depth)
*/
var particle = function( args ) {
this.sincount = 0; //helper variable for sinus transform-transitions
this.x = args.x;
this.y = args.y;
this.z = args.z;
this.origin = {
x : 0,
y : 0
};
this.target = {
x: 0,
y: 0
};
this.color = FX.gradient.get();
this.polys = [];
//create random polygon with 3 points
for( var i = 0;i <3; i=i+1) {
this.polys.push([
(-5 + Math.random()*10 ),
(-5 + Math.random()*10 )
]);
}
}
particle.prototype.update = function() {
//e is our entity
var e = this;
//if sincount is zero, save origin and determine new random target position
if( e.sincount === 0 ) {
e.origin.x = e.x;
e.origin.y = e.y;
e.target.x = (-30 + Math.random() * 60);
e.target.y = -30 - Math.random() * 10;
}
//sincount gets increased by z-index, which means front particles move faster
e.sincount += e.z;
//calculate new x positions by origin + target
e.x = e.origin.x + (e.target.x * Math.sin( FX.rad(e.sincount) ));
e.y = e.origin.y + (e.target.y * Math.sin( FX.rad(e.sincount) ));
//if sincount reaches 70, it is close to stopping (which happens at 90)
//we don't want our particles to pause movement, so reset at 70
if (e.sincount > 70) {
e.sincount = 0;
//make sure particles stay in visible range
if(e.y < 0) e.y = FX.height - Math.random() * 10;
if(e.x < 0) e.x = FX.width - Math.random() * 10;
if(e.x > FX.width) e.x = Math.random() * 10;
}
};
particle.prototype.draw = function() {
//e is our entity
var e = this;
var c = this.color;
//transparency depending on z-index
FX.ctx.fillStyle = 'rgba('+ c.r +','+ c.g +','+ c.b +', ' + e.z + ')';
FX.ctx.beginPath();
FX.ctx.moveTo(e.x + e.polys[0][0], e.y + e.polys[0][1]);
for (var i = 1, k = e.polys.length; i < k; i = i + 1) {
FX.ctx.lineTo(e.x + e.polys[i][0], e.y + e.polys[i][1]);
}
FX.ctx.lineTo(e.x + e.polys[0][0], e.y + e.polys[0][1]);
//draw the defined paths
FX.ctx.fill();
};
/*
* FX.gradient provides methods to generate a color gradient in X Steps
* @return Object public functions
* get: get the next gradient color
*/
FX.gradient = (function () {
var amount = 32, //how many steps
start = [255, 196, 0], //startcolor, RGB
end = [5, 239, 209], //endcolor, RGB
steps = [], //a single gradient step
colors = []; //array with all gradient steps
/*
* create single gradient out of start & end
*/
var create = (function () {
//calculate single step size by color channel
steps = [
( start[0] - end[0] ) / amount, //R
( start[1] - end[1] ) / amount, //G
( start[2] - end[2] ) / amount //B
];
//save each gradient step in colors[]
for (var i = 0; i <= amount; i = i + 1) {
colors[i] = [
Math.round(start[0] - steps[0] * i), //R
Math.round(start[1] - steps[1] * i), //G
Math.round(start[2] - steps[2] * i) //B
];
}
})();
/*
* Get a random gradient color as rgb
* @return String rgb()
*/
var get = function () {
var rand = Math.round( Math.random() * amount );
return {
r: colors[rand][0],
g: colors[rand][1],
b: colors[rand][2]
};
};
return {
get : get
};
})();
/*
* Converts degrees to radients
* @param Number deg The degrees to be convertet
* @return Number the radient value
*/
FX.rad = function (deg) {
return deg * Math.PI / 180;
};
/*
* FX.loop is our main loop
*/
FX.loop = function() {
//clear the canvas
FX.ctx.clearRect(0, 0, FX.width, FX.height);
for(var i in FX.particles) {
FX.particles[ i ].update();
FX.particles[ i ].draw();
}
//request next loop
requestAnimFrame(FX.loop);
};
/*
* Create particles
*/
for( var i =0;i<256;i++) {
FX.particles.push( new particle({
x: Math.round( Math.random() * FX.width ),
y: Math.round( Math.random() * FX.height ),
z: Math.round(Math.random() * 10) / 10
}) );
}
/*
* Call the loop for the first time
*/
FX.loop();
})();
</script>
<div id="codepen-footer">
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" target="_blank">Report Abuse</a>
&nbsp;
<a href="/whootboy/pen/qgeaC" target="_blank">Edit this Pen</a>
</div>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30102653-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
/*
* shim layer with setTimeout fallback
* @see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
/*
* FX will be our global object
*/
var FX = {};
/*
* basic canvas variables
*/
FX.canvas = document.getElementById('myCanvas');
FX.ctx = FX.canvas.getContext('2d');
FX.width = FX.canvas.width;
FX.height = FX.canvas.height;
//our particles will be stored in this array
FX.particles = [];
/*
* constructor for new particle
* @param Object args configuration object
* x : X-Position
* y : Y-Position
* z : Z-Index (defined depth)
*/
var particle = function( args ) {
this.sincount = 0; //helper variable for sinus transform-transitions
this.x = args.x;
this.y = args.y;
this.z = args.z;
this.origin = {
x : 0,
y : 0
};
this.target = {
x: 0,
y: 0
};
this.color = FX.gradient.get();
this.polys = [];
//create random polygon with 3 points
for( var i = 0;i <3; i=i+1) {
this.polys.push([
(-5 + Math.random()*10 ),
(-5 + Math.random()*10 )
]);
}
}
particle.prototype.update = function() {
//e is our entity
var e = this;
//if sincount is zero, save origin and determine new random target position
if( e.sincount === 0 ) {
e.origin.x = e.x;
e.origin.y = e.y;
e.target.x = (-30 + Math.random() * 60);
e.target.y = -30 - Math.random() * 10;
}
//sincount gets increased by z-index, which means front particles move faster
e.sincount += e.z;
//calculate new x positions by origin + target
e.x = e.origin.x + (e.target.x * Math.sin( FX.rad(e.sincount) ));
e.y = e.origin.y + (e.target.y * Math.sin( FX.rad(e.sincount) ));
//if sincount reaches 70, it is close to stopping (which happens at 90)
//we don't want our particles to pause movement, so reset at 70
if (e.sincount > 70) {
e.sincount = 0;
//make sure particles stay in visible range
if(e.y < 0) e.y = FX.height - Math.random() * 10;
if(e.x < 0) e.x = FX.width - Math.random() * 10;
if(e.x > FX.width) e.x = Math.random() * 10;
}
};
particle.prototype.draw = function() {
//e is our entity
var e = this;
var c = this.color;
//transparency depending on z-index
FX.ctx.fillStyle = 'rgba('+ c.r +','+ c.g +','+ c.b +', ' + e.z + ')';
FX.ctx.beginPath();
FX.ctx.moveTo(e.x + e.polys[0][0], e.y + e.polys[0][1]);
for (var i = 1, k = e.polys.length; i < k; i = i + 1) {
FX.ctx.lineTo(e.x + e.polys[i][0], e.y + e.polys[i][1]);
}
FX.ctx.lineTo(e.x + e.polys[0][0], e.y + e.polys[0][1]);
//draw the defined paths
FX.ctx.fill();
};
/*
* FX.gradient provides methods to generate a color gradient in X Steps
* @return Object public functions
* get: get the next gradient color
*/
FX.gradient = (function () {
var amount = 32, //how many steps
start = [255, 196, 0], //startcolor, RGB
end = [5, 239, 209], //endcolor, RGB
steps = [], //a single gradient step
colors = []; //array with all gradient steps
/*
* create single gradient out of start & end
*/
var create = (function () {
//calculate single step size by color channel
steps = [
( start[0] - end[0] ) / amount, //R
( start[1] - end[1] ) / amount, //G
( start[2] - end[2] ) / amount //B
];
//save each gradient step in colors[]
for (var i = 0; i <= amount; i = i + 1) {
colors[i] = [
Math.round(start[0] - steps[0] * i), //R
Math.round(start[1] - steps[1] * i), //G
Math.round(start[2] - steps[2] * i) //B
];
}
})();
/*
* Get a random gradient color as rgb
* @return String rgb()
*/
var get = function () {
var rand = Math.round( Math.random() * amount );
return {
r: colors[rand][0],
g: colors[rand][1],
b: colors[rand][2]
};
};
return {
get : get
};
})();
/*
* Converts degrees to radients
* @param Number deg The degrees to be convertet
* @return Number the radient value
*/
FX.rad = function (deg) {
return deg * Math.PI / 180;
};
/*
* FX.loop is our main loop
*/
FX.loop = function() {
//clear the canvas
FX.ctx.clearRect(0, 0, FX.width, FX.height);
for(var i in FX.particles) {
FX.particles[ i ].update();
FX.particles[ i ].draw();
}
//request next loop
requestAnimFrame(FX.loop);
};
/*
* Create particles
*/
for( var i =0;i<256;i++) {
FX.particles.push( new particle({
x: Math.round( Math.random() * FX.width ),
y: Math.round( Math.random() * FX.height ),
z: Math.round(Math.random() * 10) / 10
}) );
}
/*
* Call the loop for the first time
*/
FX.loop();
<canvas id="myCanvas" width="800" height="250"></canvas>
* {
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
padding: 40px;
background: #333;
}
canvas {
display: block;
margin: 0 auto;
background: #000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment