Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created September 19, 2015 15:02
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 zeffii/fb3cef3402ff5d2259d5 to your computer and use it in GitHub Desktop.
Save zeffii/fb3cef3402ff5d2259d5 to your computer and use it in GitHub Desktop.
timerVar = {}
dw = document.documentElement.clientWidth
dh = document.documentElement.clientHeight
generator = () ->
return {x: Math.random() * 600, y: Math.random() * 600}
dot_array = []
for v in [0..60]
dot_array.push generator()
step = 0
color = (r,g,b) -> 'rgb(' + [r, g, b].toString() + ')'
Draw = (ctx) ->
ctx.fillStyle = 'rgba(' + [255, 255, 255, 0.8].toString() + ')'
ctx.fillRect(0,0,600,600)
connected = {}
for i in dot_array
ctx.fillStyle = 'rgb(' + [5, 255, 151].toString() + ')'
ctx.lineWidth = 1
ctx.beginPath()
ctx.arc(i.x, i.y, 3, 0, 2 * Math.PI)
ctx.fill()
counter_dict = {}
counter_dict[key] = 0 for key in [0...dot_array.length]
for i in [0...dot_array.length]
for j in [0...dot_array.length]
if not (i is j)
key = if i < j then [i, j] else [j, i]
key = '(' + key.toString() + ')'
if connected.hasOwnProperty(key)
continue
x1 = dot_array[i].x
y1 = dot_array[i].y
x2 = dot_array[j].x
y2 = dot_array[j].y
d = Math.sqrt(((x1-x2)**2) + ((y1-y2)**2))
if d < 90
connected[key] = 1
f = color(d, 190, 233)
console.log(f)
ctx.strokeStyle = color((23 + Math.round(d))*2, 210, 233)
ctx.beginPath()
ctx.lineWidth = 1
ctx.moveTo(x1, y1)
ctx.lineTo(x2, y2)
ctx.stroke()
counter_dict[i] += 1
counter_dict[j] += 1
for i, index in dot_array
if counter_dict[index] > 6
ctx.strokeStyle = color(215, 155, 251)
ctx.lineWidth = 2
ctx.beginPath()
ctx.arc(i.x, i.y, 13, 0, 2 * Math.PI)
ctx.stroke()
pii = Math.random() * 2 * Math.PI
xoffset = Math.sin(pii) * 2
yoffset = Math.cos(pii) * 2
i.x += xoffset
i.y += yoffset
if not ( 0 < i.x < 600)
i.x = Math.random() * 600
if not ( 0 < i.y < 600)
i.y = Math.random() * 600
removeTimer = () ->
window.clearInterval timerVar
addTimer = (ctx) ->
removeTimer() # remove any existing timer
timerVar = setInterval((->
Draw(ctx)
return
), 50)
$(document).ready( () ->
ctx = $("canvas")[0].getContext('2d');
$('.start').click( -> addTimer(ctx) )
$('.end').click( -> removeTimer() )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment