Created
July 24, 2012 19:26
-
-
Save maxw3st/3172076 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io.
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
### | |
Text masked spring particles | |
Author: Kushagra Gour a.k.a. Chin Chang | |
### | |
# requestanimationframe polyfill | |
window.requestAnimFrame = (-> | |
return window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.oRequestAnimationFrame || | |
window.msRequestAnimationFrame || | |
(callback) -> | |
window.setTimeout callback, 1000 / 60 | |
)(); | |
# variables | |
canvas = null | |
ctx = null | |
num_particles = 800 | |
density = 8 | |
min_distance = 70 | |
text_array = [] | |
display_list = [] | |
last_time = null | |
spring_constant = 0.2 | |
damping_constant = 0.08 | |
colors = ['#184DF4', '#F49A03', '#E01730', '#00A415'] | |
W = window.innerWidth | |
H = window.innerHeight | |
init = (e) -> | |
canvas = document.getElementById 'c' | |
ctx = canvas.getContext '2d' | |
canvas.width = W | |
canvas.height = H | |
canvas.addEventListener 'mousemove', onMouseMove | |
changeWord 'Koi Mundo' | |
# create particles | |
if text_array.length | |
for i in [0...num_particles] | |
display_list.push new Particle | |
getTextArray = (ox, oy, w, h) -> | |
text_array = [] | |
data = ctx.getImageData(ox, oy, w, h).data | |
for i in [0...h] by density | |
for j in [0...w] by density | |
linear_index = (i * w + j) * 4 | |
if data[linear_index + 3] isnt 0 | |
text_array.push {x: j, y: i} | |
return text_array | |
onMouseMove = (e) -> | |
mx = e.offsetX || e.pageX | |
my = e.offsetY || e.pageY | |
for child in display_list | |
dx = child.x - mx | |
dy = child.y - my | |
d = Math.sqrt dx * dx + dy * dy | |
if d < min_distance | |
child.speed_x += dx * 0.05 | |
child.speed_y += dy * 0.05 | |
changeWord = (str) -> | |
# clear the canvas first | |
ctx.clearRect 0, 0, W, H | |
# draw the new text | |
ctx.fillStyle = "#000" | |
ctx.textBaseline = 'top' | |
ctx.textAlign = 'center' | |
ctx.font = "200px Arial" | |
ctx.fillText str, W/2, 0 | |
# get the text points | |
text_array = getTextArray 0, 0, W, H | |
for child in display_list | |
child.reposition() | |
animate = -> | |
animationLoop() | |
requestAnimFrame animate | |
animationLoop = -> | |
if !last_time then last_time = (new Date).getTime() | |
current_time = (new Date).getTime() | |
dt = (current_time - last_time) / 1000 | |
last_time = current_time | |
window.fps = 1 / dt | |
draw() | |
update() | |
draw = -> | |
ctx.fillStyle = "#111" | |
ctx.fillRect 0, 0, W, H | |
for child in display_list | |
continue if typeof child.draw isnt 'function' | |
ctx.save(); | |
ctx.translate child.x, child.y unless isNaN child.x or isNaN child.y | |
ctx.scale child.scale_x, child.scale_y unless isNaN child.scale_x or isNaN child.scale_y | |
ctx.globalAlpha = child.alpha unless isNaN child.alpha | |
child.draw() | |
ctx.restore(); | |
update = -> | |
# update particles | |
for child in display_list | |
child.update() if typeof child.update is 'function' | |
class Particle | |
constructor: (@radius = 1, @x = Math.random() * W, @y = Math.random() * H) -> | |
@speed_x = 10 - Math.random() * 20 | |
@speed_y = 10 - Math.random() * 20 | |
@color = "#ff0" | |
@alpha = 1 | |
@reposition() | |
@reset() | |
draw: -> | |
#ctx.drawImage canvas2, 0, 0 | |
ctx.fillStyle = @color | |
ctx.beginPath() | |
ctx.arc 4, 4, 4, 0, Math.PI*2, true | |
ctx.fill() | |
update: -> | |
acc_x = -spring_constant * (@x - @ox) - damping_constant * @speed_x | |
acc_y = -spring_constant * (@y - @oy) - damping_constant * @speed_y | |
@speed_x += acc_x | |
@speed_y += acc_y | |
@alpha -= 0.019 | |
@scale_x += 0.06 | |
@scale_y += 0.06 | |
@x += @speed_x | |
@y += @speed_y | |
if @alpha <= 0 | |
@reset() | |
reset: -> | |
@alpha = Math.random() | |
@scale_x = @scale_y = 0 | |
@color = colors[~~(Math.random() * colors.length)] | |
reposition: -> | |
point = text_array[~~(Math.random() * text_array.length)] | |
@ox = @x = point.x + ~~(3 + Math.random() * 6) | |
@oy = @y = point.y + ~~(3 + Math.random() * 6) | |
init() | |
animate() |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Sppringgy jelly! · CodePen</title> | |
<!-- | |
Copyright (c) 2012 maxw3st, http://codepen.io/maxw3st | |
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> | |
body { margin: 0; } | |
span { position: absolute; top: 15px; left: 5px; color: white; } | |
</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=c></canvas> | |
<span>Try disturbing the particles with mouse</span> | |
<script> | |
(function() { | |
/* | |
Text masked spring particles | |
Author: Kushagra Gour a.k.a. Chin Chang | |
*/ | |
(function() { | |
var H, Particle, W, animate, animationLoop, canvas, changeWord, colors, ctx, damping_constant, density, display_list, draw, getTextArray, init, last_time, min_distance, num_particles, onMouseMove, spring_constant, text_array, update; | |
window.requestAnimFrame = (function() { | |
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { | |
return window.setTimeout(callback, 1000 / 60); | |
}; | |
})(); | |
canvas = null; | |
ctx = null; | |
num_particles = 800; | |
density = 8; | |
min_distance = 70; | |
text_array = []; | |
display_list = []; | |
last_time = null; | |
spring_constant = 0.2; | |
damping_constant = 0.08; | |
colors = ['#184DF4', '#F49A03', '#E01730', '#00A415']; | |
W = window.innerWidth; | |
H = window.innerHeight; | |
init = function(e) { | |
var i, _results; | |
canvas = document.getElementById('c'); | |
ctx = canvas.getContext('2d'); | |
canvas.width = W; | |
canvas.height = H; | |
canvas.addEventListener('mousemove', onMouseMove); | |
changeWord('Koi Mundo'); | |
if (text_array.length) { | |
_results = []; | |
for (i = 0; 0 <= num_particles ? i < num_particles : i > num_particles; 0 <= num_particles ? i++ : i--) { | |
_results.push(display_list.push(new Particle)); | |
} | |
return _results; | |
} | |
}; | |
getTextArray = function(ox, oy, w, h) { | |
var data, i, j, linear_index; | |
text_array = []; | |
data = ctx.getImageData(ox, oy, w, h).data; | |
for (i = 0; 0 <= h ? i < h : i > h; i += density) { | |
for (j = 0; 0 <= w ? j < w : j > w; j += density) { | |
linear_index = (i * w + j) * 4; | |
if (data[linear_index + 3] !== 0) { | |
text_array.push({ | |
x: j, | |
y: i | |
}); | |
} | |
} | |
} | |
return text_array; | |
}; | |
onMouseMove = function(e) { | |
var child, d, dx, dy, mx, my, _i, _len, _results; | |
mx = e.offsetX || e.pageX; | |
my = e.offsetY || e.pageY; | |
_results = []; | |
for (_i = 0, _len = display_list.length; _i < _len; _i++) { | |
child = display_list[_i]; | |
dx = child.x - mx; | |
dy = child.y - my; | |
d = Math.sqrt(dx * dx + dy * dy); | |
if (d < min_distance) { | |
child.speed_x += dx * 0.05; | |
_results.push(child.speed_y += dy * 0.05); | |
} else { | |
_results.push(void 0); | |
} | |
} | |
return _results; | |
}; | |
changeWord = function(str) { | |
var child, _i, _len, _results; | |
ctx.clearRect(0, 0, W, H); | |
ctx.fillStyle = "#000"; | |
ctx.textBaseline = 'top'; | |
ctx.textAlign = 'center'; | |
ctx.font = "200px Arial"; | |
ctx.fillText(str, W / 2, 0); | |
text_array = getTextArray(0, 0, W, H); | |
_results = []; | |
for (_i = 0, _len = display_list.length; _i < _len; _i++) { | |
child = display_list[_i]; | |
_results.push(child.reposition()); | |
} | |
return _results; | |
}; | |
animate = function() { | |
animationLoop(); | |
return requestAnimFrame(animate); | |
}; | |
animationLoop = function() { | |
var current_time, dt; | |
if (!last_time) last_time = (new Date).getTime(); | |
current_time = (new Date).getTime(); | |
dt = (current_time - last_time) / 1000; | |
last_time = current_time; | |
window.fps = 1 / dt; | |
draw(); | |
return update(); | |
}; | |
draw = function() { | |
var child, _i, _len, _results; | |
ctx.fillStyle = "#111"; | |
ctx.fillRect(0, 0, W, H); | |
_results = []; | |
for (_i = 0, _len = display_list.length; _i < _len; _i++) { | |
child = display_list[_i]; | |
if (typeof child.draw !== 'function') continue; | |
ctx.save(); | |
if (!isNaN(child.x || isNaN(child.y))) ctx.translate(child.x, child.y); | |
if (!isNaN(child.scale_x || isNaN(child.scale_y))) { | |
ctx.scale(child.scale_x, child.scale_y); | |
} | |
if (!isNaN(child.alpha)) ctx.globalAlpha = child.alpha; | |
child.draw(); | |
_results.push(ctx.restore()); | |
} | |
return _results; | |
}; | |
update = function() { | |
var child, _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = display_list.length; _i < _len; _i++) { | |
child = display_list[_i]; | |
if (typeof child.update === 'function') { | |
_results.push(child.update()); | |
} else { | |
_results.push(void 0); | |
} | |
} | |
return _results; | |
}; | |
Particle = (function() { | |
function Particle(radius, x, y) { | |
this.radius = radius != null ? radius : 1; | |
this.x = x != null ? x : Math.random() * W; | |
this.y = y != null ? y : Math.random() * H; | |
this.speed_x = 10 - Math.random() * 20; | |
this.speed_y = 10 - Math.random() * 20; | |
this.color = "#ff0"; | |
this.alpha = 1; | |
this.reposition(); | |
this.reset(); | |
} | |
Particle.prototype.draw = function() { | |
ctx.fillStyle = this.color; | |
ctx.beginPath(); | |
ctx.arc(4, 4, 4, 0, Math.PI * 2, true); | |
return ctx.fill(); | |
}; | |
Particle.prototype.update = function() { | |
var acc_x, acc_y; | |
acc_x = -spring_constant * (this.x - this.ox) - damping_constant * this.speed_x; | |
acc_y = -spring_constant * (this.y - this.oy) - damping_constant * this.speed_y; | |
this.speed_x += acc_x; | |
this.speed_y += acc_y; | |
this.alpha -= 0.019; | |
this.scale_x += 0.06; | |
this.scale_y += 0.06; | |
this.x += this.speed_x; | |
this.y += this.speed_y; | |
if (this.alpha <= 0) return this.reset(); | |
}; | |
Particle.prototype.reset = function() { | |
this.alpha = Math.random(); | |
this.scale_x = this.scale_y = 0; | |
return this.color = colors[~~(Math.random() * colors.length)]; | |
}; | |
Particle.prototype.reposition = function() { | |
var point; | |
point = text_array[~~(Math.random() * text_array.length)]; | |
this.ox = this.x = point.x + ~~(3 + Math.random() * 6); | |
return this.oy = this.y = point.y + ~~(3 + Math.random() * 6); | |
}; | |
return Particle; | |
})(); | |
init(); | |
animate(); | |
}).call(this); | |
})(); | |
</script> | |
<div id="codepen-footer"> | |
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" onclick="window.open(this.href, null, 'height=517, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1'); return false">Report Abuse</a> | |
| |
<a href="/maxw3st/pen/nIJkh" target="_blank">Edit this Pen</a> | |
</div> | |
</body> | |
</html> |
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
<canvas id=c></canvas> | |
<span>Try disturbing the particles with mouse</span> |
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
body { margin: 0; } | |
span { position: absolute; top: 15px; left: 5px; color: white; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment