6 $svg = null # jquery object for svg element
7 svg = null # dom object for svg element
9 svg_ns = 'http://www.w3.org/2000/svg'
12 update_path = (path, data, flags) ->
19 d += "#{loc[0]} #{loc[1]}"
21 d += "L #{mouse[0]} #{mouse[1]}"
24 path.setAttribute "d", d
28 update_path selection.element, selection.data
31 stop_close_drawing = ->
33 update_path selection.element, selection.data, close: true
38 path = document.createElementNS svg_ns, "path"
39 selection = data: [], element: path
41 selection.data.push [x, y]
42 update_path selection.element, selection.data
47 update_path selection.element, selection.data, to_mouse: true
49 keydown = (keycode) ->
52 return stop_close_drawing()
53 when (' '.charCodeAt 0), 13, 10, 27
56 # called automatically on domcontentloaded
58 $container = $ '.crayon_mockup'
59 $stop_button = $ '<span class="button">stop drawing</span>'
60 $stop_close_button = $ '<span class="button">stop drawing, close loop</span>'
61 $tools = $ '<div class="toolbar"></div>'
62 $tools.append $stop_button
63 $tools.append $stop_close_button
64 $stop_button.click stop_drawing
65 $stop_close_button.click stop_close_drawing
66 $container.append $tools
67 svg = document.createElementNS svg_ns, 'svg'
68 svg.setAttribute 'width', width
69 svg.setAttribute 'height', height
70 svg.setAttribute 'viewBox', "0 0 #{width} #{height}"
72 $container.append $svg
74 offset = $svg.offset()
75 click e.pageX - offset.left, e.pageY - offset.top
78 offset = $svg.offset()
79 mousemove e.pageX - offset.left, e.pageY - offset.top
80 ($ document).keydown (e) ->
81 return keydown e.keyCode