6 # json (compiled to javascript and minified) is ~8% smaller than the resulting xml
7 json_to_svg = (json) ->
9 el = document.createElementNS 'http://www.w3.org/2000/svg', tag
13 el.appendChild json_to_svg child
18 # public vars: x, y, width, height, el
20 constructor: (args) ->
23 @width = args?.width ? 50
24 @height = args?.height ? 34
28 class RectWidget extends Widget
29 constructor: (args) ->
31 @el = json_to_svg rect:
37 style: 'filter: url(#crayon)'
40 @el.setAttribute 'x', @x + 1
41 @el.setAttribute 'y', @y + 1
43 return new RectWidget @
45 # called automatically on domcontentloaded
51 $container = $ '.crayon_mockup'
52 svg = json_to_svg svg: width: width, height: height, viewBox: "0 0 #{width} #{height}"
54 $container.append $svg
55 svg.appendChild json_to_svg filter:
56 id: 'crayon', filterUnits: 'userSpaceOnUse'
57 x: '-5%', y: '-5%', height: '110%', width: '110%'
59 { feTurbulence: baseFrequency: '.3', numOctaves: '2', type: 'fractalNoise' }
60 { feDisplacementMap: scale: '6', xChannelSelector: 'R', in: 'SourceGraphic' }
63 # create canvas border
64 svg.appendChild json_to_svg rect:
68 height: height - 2 - supply_height
69 class: 'canvas_border'
73 new RectWidget width: 12, height: 50
74 new RectWidget width: 70, height: 12
76 for widget, i in supply
78 x: 30 + i * 90 + (70 - widget.width) / 2
79 y: (supply_height - widget.height) / 2
81 svg.appendChild widget.el
84 closest_widget = (widgets, xy) ->
85 closest = [10000, null]
87 x = w.x + w.width / 2 - xy.x
88 y = w.y + w.height / 2 - xy.y
95 svg_event_to_xy = (e) ->
97 offset = $svg.offset()
99 x: Math.round(e.pageX - offset.left)
100 y: Math.round(e.pageY - offset.top)
102 $svg.mousedown (e) ->
103 xy = svg_event_to_xy e
104 if xy.y < supply_height
105 closest = closest_widget supply, xy
108 closest[1] = closest[1].clone()
109 svg.appendChild closest[1].el
111 closest = closest_widget on_canvas, xy
113 selected = [closest[1]]
121 if selected[0].y > supply_height
122 on_canvas.push selected[0]
124 svg.removeChild selected[0].el
129 xy = svg_event_to_xy e
130 xy.x -= selected[0].width / 2
131 xy.y -= selected[0].height / 2
134 $svg.mousemove mousemove
135 #($ document).keydown (e) ->