Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Coffeescript minimal drawing experiment used for testing emacs skewer-mode extensions for coffeescript: http://vimeo.com/67465309

View sketch.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# lightweight processing-esque canvas wrapper
class Sketch
constructor: (width, height) ->
@running = false
@canvas = (document.getElementsByTagName 'canvas')[0]
if @canvas
@canvas.parentNode.removeChild @canvas
@canvas = document.createElement 'canvas'
@canvas.width = width
@canvas.height = height
@ctx = @canvas.getContext '2d'
@ctx.translate width / 2, height / 2
@ctx.save()
document.body.appendChild @canvas
@framerate = 30
@running = false
 
drawLoop: ->
@draw()
setTimeout(@drawLoop.bind(this), (1000/@framerate)) if @running
 
start: ->
if @setup
@setup
@running = true
@drawLoop()
 
stop: ->
@running = false
 
clear: ->
@ctx.restore()
@ctx.clearRect(@canvas.width/-2, @canvas.height/-2, @canvas.width, @canvas.height)
 
window.sketch = new Sketch(800,800)
 
window.sketch.draw = ->
@ctx.strokeStyle = ['#ec5d20','#b23267','#2d212c'][(~~(Math.random()*3))]
boxWidth = 400
boxHeight = 400
@ctx.rotate(Math.PI / (Math.random()*12))
@ctx.strokeRect(boxWidth/-2, boxHeight/-2, boxWidth, boxHeight)
 
window.sketch.start()
 
# redefine the draw function while it's running
window.sketch.drawLine = (x1,y1,x2,y2) ->
@ctx.beginPath()
@ctx.moveTo(x1,x2)
@ctx.lineTo(x2,y2)
@ctx.stroke()
 
window.sketch.draw = ->
@ctx.rotate(0)
@ctx.strokeStyle = ['#ec5d20','#b23267','#2d212c'][(~~(Math.random()*3))]
lines = ([0...3].map (n) -> (Math.random() * 360)).map (a) -> [0,0,(400 * Math.sin(a)),(400 * Math.cos(a))]
@drawLine(l[0],l[1],l[2],l[3]) for l in lines
 
window.sketch.stop()
 
window.sketch.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.