This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
free-draw-tool.coffee
72 lines (55 loc) · 2.02 KB
/
free-draw-tool.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
66
67
68
69
70
71
72
{Tool} = require "marking-surface"
class FreeDrawTool extends Tool
stroke: "rgba(255, 255, 255, 1)"
strokeWidth: 1
fill: "rgba(255, 255, 255, 0.3)"
constructor: ->
super
@mark.relPath = []
@path = @addShape 'path', stroke: @stroke, fill: "transparent", strokeWidth: 1
@path.addEvent "start", @startMove
@path.addEvent "move", @moveMark
onInitialStart: (e) ->
super
{x, y} = @coords e
@mark.startingPoint = [x, y]
@mark.relPath.push [0,0]
onInitialMove: (e) ->
super
{x, y} = @coords e
@mark.relPath.push [@xDiff(x), @yDiff(y)]
@mark.set {startingPoint: @mark.startingPoint, relPath: @mark.relPath}
@render()
onInitialRelease: (e) ->
super
@path.attr {fill: @fill}
@render()
@destroy() if @mark.relPath.length is 1 #don't record single clicks
xDiff: (x) -> x - @xPathCoord()
yDiff: (y) -> y - @yPathCoord()
startMove: (e) =>
{x, y} = @coords e
@xOffset = x - @mark.startingPoint[0]
@yOffset = y - @mark.startingPoint[1]
moveMark: (e) =>
{x, y} = @coords e
@mark.startingPoint = [x - @xOffset, y - @yOffset]
@mark.set {startingPoint: @mark.startingPoint}
render: ->
super
if @mark.relPath.length > 1
@path.attr {d: @pathDescription()}
@controls?.moveTo {x: @maxX(), y: @maxY()}
pathDescription: ->
"m #{@mark.startingPoint}, #{@mark.relPath.join(',')} #{if @isComplete() then ' Z' else ''}"
xPathCoord: (index = @mark.relPath.length-1) ->
# x coord at index or last point in relPath
@mark.startingPoint[0] + (p[0] for p in @mark.relPath[0..index]).reduce (acc, x) -> acc + x
yPathCoord: (index = @mark.relPath.length-1) ->
# y coord at index or last point in relPath
@mark.startingPoint[1] + (p[1] for p in @mark.relPath[0..index]).reduce (acc, y) -> acc + y
maxX: -> Math.max (p[0] for p in @_coordsOfPath())...
maxY: -> Math.max (p[1] for p in @_coordsOfPath())...
_coordsOfPath: ->
([@xPathCoord(i), @yPathCoord(i)] for point, i in @mark.relPath)
module?.exports = FreeDrawTool