Skip to content

Instantly share code, notes, and snippets.

@belkadan
Last active December 14, 2015 16:28
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 belkadan/359cfc81e04aa18bc615 to your computer and use it in GitHub Desktop.
Save belkadan/359cfc81e04aa18bc615 to your computer and use it in GitHub Desktop.
Implementation of Angry Multi-legged Robots' "Simple Visual Futuristic Sports Scoring System".
go = ->
canvas = document.getElementById('canvas')
context = canvas.getContext('2d')
BGCOLOR = 'rgb(255,255,255)'
LINECOLOR = 'rgb(0,0,0)'
SPEED = 700
DIM = 4
SCALE = canvas.height
WIDTH = SCALE / DIM
HEIGHT = WIDTH * (Math.sqrt(3) / 2.0)
# Data: hardcoded to DIM == 4
TRIANGLES = [
[2,2],
[3,3],
[2,3],
[2,1],
[3,5],
[3,6],
[3,4],
[2,4],
[3,1],
[3,2],
[3,0],
[2,0],
[1,1],
[1,2],
[1,0],
[0,0]
]
COLORS = [
'rgb(118, 217, 235)',
'rgb(96 , 146, 191)',
'rgb(0 , 162, 234)',
'rgb(61 , 71 , 206)',
'rgb(243, 228, 174)',
'rgb(255, 242, 0 )',
'rgb(255, 201, 0 )',
'rgb(255, 126, 13 )',
'rgb(214, 214, 214)',
'rgb(160, 160, 160)',
'rgb(103, 103, 103)',
'rgb(58 , 58 , 58 )',
'rgb(158, 231, 1 )',
'rgb(129, 192, 0 )',
'rgb(0 , 172, 5 )',
'rgb(0 , 109, 0 )'
]
getVertices = (row, col) ->
x = (DIM + col - row) * WIDTH / 2.0
y = row * HEIGHT
if col % 2 == 0
[[x,y + HEIGHT], [x + WIDTH/2.0, y], [x + WIDTH, y + HEIGHT]]
else
[[x,y], [x + WIDTH/2.0, y + HEIGHT], [x + WIDTH, y]]
VERTICES = (getVertices(t...) for t in TRIANGLES)
draw = (score) ->
context.fillStyle = BGCOLOR
context.fillRect(0, 0, canvas.width, canvas.height)
context.strokeStyle = LINECOLOR
context.lineWidth = 2
for triangle, i in VERTICES
context.beginPath()
context.moveTo(triangle[0]...)
context.lineTo(triangle[1]...)
context.lineTo(triangle[2]...)
context.closePath()
if i < score
context.fillStyle = COLORS[i]
context.fill()
context.stroke()
null # don't collect the for-loop for a return value
# Accept the arguments to window.setInterval in either order.
do ->
oldSetInterval = setInterval
window.setInterval = (fn, timeout) ->
if typeof(fn) isnt 'function'
[fn, timeout] = [timeout, fn]
oldSetInterval(fn, timeout)
score = 1
draw(0)
setInterval SPEED, ->
draw(score)
score += 1
score = 0 if score > VERTICES.length
if document.readyState is 'complete'
go()
else
document.addEventListener 'readystatechange', go
<html>
<head>
<title>Triangles</title>
<script type="text/coffeescript" src="triangles.coffee"></script>
<script type="text/javascript" src="coffee-script.js"></script>
</head>
<body>
<canvas id="canvas" width="480" height="360">
</canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment