Skip to content

Instantly share code, notes, and snippets.

@pygy

pygy/index.html Secret

Last active January 23, 2016 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pygy/ec5f91df2e90f49873e1 to your computer and use it in GitHub Desktop.
Save pygy/ec5f91df2e90f49873e1 to your computer and use it in GitHub Desktop.
<!doctype html>
<title>fireflies</title>
<style>
#canvas {
width: 600px;
height: 450px;
border: 1px solid grey;
margin: 0 auto;
position:relative;
}
.firefly{
position: absolute;
width: 9px;
height: 9px;
}
</style>
<div id="canvas"></div>
<script>
// The MIT License (MIT)
// Copyright (c) 2016 Pierre-Yves Gérardy py@py.gy
// 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.
function color() {
return (
'#' +
(0|256*Math.random()).toString(16) +
(0|256*Math.random()).toString(16) +
'00'
)
}
function Firefly(parent, resolution) {
this.resolution = resolution
this.max = {
x: parent.offsetWidth - resolution,
y: parent.offsetHeight - resolution
}
this.end = {
x:Math.random() * this.max.x,
y:(1+Math.random()) / 2 * this.max.y,
time: + new Date()
}
var el = document.createElement('div')
el.className = 'firefly'
el.style.backgroundColor = color()
this.element = el
parent.appendChild(el)
this.setDestination(+new Date())
}
p = Firefly.prototype
p.setDestination = function (t) {
this.start = this.end
this.start.time = t
var maxDistance = Math.min(this.max.x, this.max.y) / 4;
do {
var d = (0.5 - Math.random()) * maxDistance
var angle = Math.asin(1) * (0.5 - Math.random()) // move mostly horizontally
this.end = {
x: this.start.x + (Math.cos(angle) * d),
y: this.start.y + (Math.sin(angle) * d),
time: t + (1 + Math.random()) * Math.abs(d) / this.resolution * 200 //100ms per tick
}
} while (!this.withinBounds())
}
p.tick = function (t) {
var soFar = t - this.start.time;
var duration = this.end.time - this.start.time;
if (soFar < duration) {
progress = soFar/duration;
this.element.style.left = this.quantize(this.start.x + (progress * (this.end.x - this.start.x)))
this.element.style.top = this.quantize(this.start.y + (progress * (this.end.y - this.start.y)))
} else {
this.setDestination(t)
}
}
p.quantize = function(n) {
return Math.floor(n/this.resolution) * this.resolution + 'px'
}
p.withinBounds = function () {
return 0 < this.end.x && 0 < this.end.y && this.end.x <= this.max.x && this.end.y <= this.max.y
}
fireflies = []
for (var i = 0; i < 10; i ++) {
fireflies.push(new Firefly(canvas, 3))
}
function update() {
var t = +new Date()
fireflies.forEach(function(frfl) {frfl.tick(t)})
}
update() // position the fireflies
setInterval(update, 200)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment