-
Notifications
You must be signed in to change notification settings - Fork 4
/
boids2d_renderer.coffee
66 lines (51 loc) · 1.55 KB
/
boids2d_renderer.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
62
63
64
65
class @Boids2DRenderer
# Instance variables
canvas = null
ctx = null
options =
showVelocityVectors: true
showAveragePosition: false
# Private methods
getContext = ->
canvas.width = window.innerWidth
canvas.height = window.innerHeight
ctx = canvas.getContext('2d')
# Public API
constructor: (el) ->
canvas = el
getContext()
clearScreen: -> ctx.clearRect(0, 0, canvas.width, canvas.height)
render: (boids, center, goal) ->
ctx.fillStyle = "black"
ctx.strokeStyle = "#333"
@clearScreen()
for b in boids
ctx.beginPath()
ctx.arc(b.position.x(), b.position.y(), 3, 0, Math.PI*2, true)
ctx.stroke()
ctx.fill()
ctx.closePath()
if options['showVelocityVectors']
ctx.beginPath()
ctx.moveTo(b.position.x(), b.position.y())
ctx.lineTo(b.position.x() - b.velocity.x() * 10, b.position.y() - b.velocity.y() * 10)
ctx.stroke()
ctx.closePath()
if options['showAveragePosition']
ctx.fillStyle = "red"
ctx.beginPath()
ctx.arc(center.x(), center.y(), 3, 0, Math.PI*2, true)
ctx.stroke()
ctx.fill()
if goal
ctx.fillStyle = "rgba(230, 230, 55, 0.5)"
ctx.strokeStyle = "#F0C44D"
ctx.beginPath()
ctx.arc(goal.x(), goal.y(), 10, 0, Math.PI*2, true)
ctx.stroke()
ctx.fill()
width: -> canvas.width
height: -> canvas.height
handleResize: -> getContext()
get: (option) -> options[option]
set: (option, value) -> console.log "#{option} = #{value}"; options[option] = value