backspaces/agentscript — pull request #69 by bennlich (2014-11-03 18:48:38+00:00)
Title: Rendering module

**Not for merging! Currently just for sparking discussion.**

This is the beginning of an attempt to isolate canvas/rendering logic in a single class, `CanvasView`. The goal is to easily be able to swap out renderers (Canvas, CanvasTile, WebGL) of an agentscript model.

So far, the changes have all been pretty straightforward cutting/pasting `Model` functions over to `CanvasView`.

I left `setWorld()` in `Model` for the time being because it seems model-related, although a lot of the calculations it performs are specifically for rendering.

Still need to figure out how to deal with lingering references to `model.contexts`:

* In `AgentSet`, `show()`, `hide()`
```
show: -> o.hidden = false for o in @; @draw(@model.contexts[@name])
hide: -> o.hidden = true for o in @; @draw(@model.contexts[@name])
```
Should AgentSets emit 'changed' events to alert the renderer to redraw them?

And:

* In `Patches`, `usePixels()`, `setPixels()`, `installDrawing()`

```
usePixels: (@drawWithPixels=true) ->
ctx = @model.contexts.patches
u.setCtxSmoothing ctx, not @drawWithPixels

setPixels: ->
if @size is 1
then @usePixels(); @pixelsCtx = @model.contexts.patches
else @pixelsCtx = u.createCtx @numX, @numY
@pixelsImageData = @pixelsCtx.getImageData(0, 0, @numX, @numY)
@pixelsData = @pixelsImageData.data
if @pixelsData instanceof Uint8Array # Check for typed arrays
@pixelsData32 = new Uint32Array @pixelsData.buffer
@pixelsAreLittleEndian = u.isLittleEndian()

installDrawing: (img, ctx=@model.contexts.drawing) ->
u.setIdentity ctx
ctx.drawImage img, 0, 0, ctx.canvas.width, ctx.canvas.height
ctx.restore()
```

Can these be made into aliases for functionality in CanvasView?