Permalink
Browse files

Run simple canvas tests in Node too.

use: npm test
  • Loading branch information...
1 parent 2b272fa commit 640d2357757c39a13085bf7bfced934315641b8e Esa-Matti Suuronen committed Oct 17, 2011
Showing with 14 additions and 174 deletions.
  1. +0 −164 client/drawers.tools.coffee
  2. +1 −0 package.json
  3. +1 −1 server.js
  4. +7 −3 spec/tools.spec.coffee
  5. +0 −1 views/spec.jade
  6. +5 −5 {lib → }/whiteboard.coffee
View
@@ -1,164 +0,0 @@
-
-tools = NS "PWB.drawers.tools"
-{notImplemented} = NS "PWB.helpers"
-
-
-class BaseTool
- _.extend @::, Backbone.Events
-
- name: "BaseTool" # Must match the class name
-
- constructor: (@opts) ->
- @model = @opts.model
-
- @sketchCanvas = @opts.sketch
- @mainCanvas = @opts.main
-
-
- @sketch = @sketchCanvas.getContext "2d"
- @main = @mainCanvas.getContext "2d"
-
-
-
-
- setColor: (color) ->
- @sketch.strokeStyle = color
- @sketch.fillStyle = color
-
- getColor: -> @sketch.strokeStyle
-
- setSize: (width) ->
- @sketch.lineWidth = width
-
- getSize: -> @sketch.lineWidth
-
-
- draw: ->
- stats.update()
- @main.drawImage @sketchCanvas, 0, 0
- @clear()
-
-
- clear: ->
- @sketch.clearRect 0, 0, @mainCanvas.width, @mainCanvas.height
-
- begin: ->
- @moves = []
-
- end: ->
- @trigger "draw", @toJSON()
-
- down: notImplemented "down"
- up: notImplemented "up"
- move: notImplemented "move"
-
-
- drawLine: (from, to) ->
- @sketch.lineCap = "round"
- @sketch.beginPath()
- @sketch.moveTo from.x, from.y
- @sketch.lineTo to.x, to.y
-
- @sketch.stroke()
- @sketch.closePath()
-
-
- replay: (shape) ->
- @begin()
- @setColor shape.color
- @setSize shape.size
-
- # TODO: Sanitize op
- for point in shape.moves
- @[point.op] point
- @draw()
-
- toJSON: ->
- color: @getColor()
- tool: @name
- size: @getSize()
- moves: @moves
-
-class tools.Pencil extends BaseTool
-
- name: "Pencil"
-
-
- down: (point) ->
- # Start drawing
- point = _.clone point
- point.op = "down"
- @moves.push point
- @lastPoint = point
-
- # Draw a dot at the begining of the path. This is not required for Firefox,
- # but Webkits (Chrome & Android) won't draw anything if user just clicks
- # the canvas.
- @sketch.beginPath()
- @sketch.arc(point.x, point.y, @getSize() / 2, 0, (Math.PI/180)*360, true);
- @sketch.fill()
- @sketch.closePath()
-
-
- move: (to) ->
- to = _.clone to
- to.op = "move"
- @moves.push to
- from = @lastPoint
-
- @drawLine from, to
-
- @lastPoint = to
-
- up: (point) ->
- @move point
- @draw()
-
-
-
-
-# Eraser is basically just a pencil where compositing is turned inside out
-class tools.Eraser extends tools.Pencil
- name: "Eraser"
-
- draw: ->
- @main.globalCompositeOperation = "destination-out"
- super
- @main.globalCompositeOperation = "source-over"
-
-
- # Draw always on movement so that we can immediately see what we erase
- move: ->
- super
- @draw()
-
-
-class tools.Line extends BaseTool
-
- name: "Line"
-
- down: (point) ->
- # Start drawing
- point = _.clone point
- point.op = "down"
- @moves.push @startPoint = point
- @lastPoint = point
-
-
-
- move: (to) ->
- from = @startPoint
- @clear()
- @drawLine from, to
-
- to = _.clone to
- to.op = "move"
- @lastPoint = to
-
- up: ->
- # @drawLine @startPoint, @lastPoint
- @moves[1] = @lastPoint
- @draw()
-
-
-
View
@@ -4,6 +4,7 @@
, "private": true
, "scripts": {
"start": "node server.js"
+ , "test": "node_modules/.bin/jasmine-node --coffee spec/"
}
, "devDependencies": {
"supervisor": ""
View
@@ -1,2 +1,2 @@
require("coffee-script");
-require("./lib/whiteboard");
+require("./whiteboard");
View
@@ -1,8 +1,12 @@
-tools = NS "PWB.drawers.tools"
+if NS?
+ tools = NS "PWB.drawers.tools"
+ createCanvas = -> document.createElement "canvas"
+else
+ tools = require "../shared/drawtools"
+ Canvas = require "canvas"
+ createCanvas = -> new Canvas
-createCanvas = ->
- document.createElement "canvas"
describe "Pencil", ->
beforeEach ->
View
@@ -7,4 +7,3 @@ html
body
h1 Specs
-
@@ -5,18 +5,18 @@ _ = require 'underscore'
_.mixin require 'underscore.string'
-logClients = require "./clientlogger"
+logClients = require "./lib/clientlogger"
css = piler.createCSSManager()
js = piler.createJSManager()
app = express.createServer()
io = require('socket.io').listen app
-clientFiles = __dirname + "/../client"
+clientFiles = __dirname + "/client"
app.configure ->
- app.use express.static __dirname + "/../public"
+ app.use express.static __dirname + "/public"
js.bind app
css.bind app
@@ -42,7 +42,7 @@ app.configure ->
js.addFile clientFiles + "/helpers.coffee"
js.addFile clientFiles + "/drawers.coffee"
- js.addFile clientFiles + "/drawers.tools.coffee"
+ js.addFile __dirname + "/shared/drawers.tools.coffee"
js.addFile clientFiles + "/drawers.models.coffee"
js.addFile clientFiles + "/drawers.views.coffee"
@@ -55,7 +55,7 @@ app.configure "development", ->
js.addFile "spec", clientFiles + "/vendor/jasmine/jasmine.js"
js.addFile "spec", clientFiles + "/vendor/jasmine/jasmine-html.js"
- js.addFile "spec", __dirname + "/../spec/tools.spec.coffee"
+ js.addFile "spec", __dirname + "/spec/tools.spec.coffee"
js.addFile "spec", clientFiles + "/specrunner.js"
app.get "/spec", (req, res) ->

0 comments on commit 640d235

Please sign in to comment.