zui53 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ <!DOCTYPE html>
2
+ <html style='width: 100%; height:100%;'>
3
+ <head>
4
+ <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' type='text/javascript'></script>
5
+ <script src="../build/zui53.js" type="text/javascript"></script>
6
+ <script>
7
+ function initZUI(){
8
+ console.log(ZUI53);
9
+ var zui = new ZUI53.Viewport( document.getElementById('zui') );
10
+ zui.addSurface( new ZUI53.Surfaces.CSS( document.getElementById('viewport') ) );
11
+
12
+ var pan_tool = new ZUI53.Tools.Pan(zui);
13
+ zui.toolset.add( pan_tool );
14
+ pan_tool.attach()
15
+
16
+ }
17
+ </script>
18
+ </head>
19
+ <body onload='initZUI()' style='width:100%; height:100%; margin: 0; padding: 0;'>
20
+
21
+ <div id='zui'>
22
+ <div id='viewport'>
23
+ <button style='position:absolute; left: 300px; top: 100px;'>Hello World</button>
24
+
25
+ <div style='position: absolute; left: 100px; top: 100px; border: 2px solid red; padding: 10px;'>Hello World</div>
26
+ </div>
27
+ </div>
28
+
29
+ </body>
30
+ </html>
@@ -0,0 +1,33 @@
1
+ <!DOCTYPE html>
2
+ <html style='width: 100%; height:100%;'>
3
+ <head>
4
+ <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' type='text/javascript'></script>
5
+ <script src="../build/zui53.js" type="text/javascript"></script>
6
+ <script>
7
+ function initZUI(){
8
+ console.log(ZUI53);
9
+ zui = new ZUI53.Viewport( document.getElementById('zui') );
10
+ zui.addSurface( new ZUI53.Surfaces.SVG( document.getElementById('svg_root_group') ) );
11
+
12
+ var pan_tool = new ZUI53.Tools.Pan(zui);
13
+ zui.toolset.add( pan_tool );
14
+ pan_tool.attach()
15
+
16
+ zui.showBounds({width: 10, height: 10, x: 45, y: 45})
17
+
18
+ }
19
+ </script>
20
+ </head>
21
+ <body onload='initZUI()' style='width:100%; height:100%; margin: 0; padding: 0;'>
22
+
23
+ <div id='zui'>
24
+ <svg height=100% width=100%>
25
+ <g id='svg_root_group'>
26
+ <path stroke=#F53F0C stroke-width=10 fill=#F5C60C stroke-linejoin=round d='M 10,90 L 90,90 L 50,14 Z'/>
27
+ <line stroke=black stroke-width=10 stroke-linecap=round x1=50 x2=50 y1=45 y2=75 />
28
+ </g>
29
+ </svg>
30
+ </div>
31
+
32
+ </body>
33
+ </html>
@@ -0,0 +1,43 @@
1
+ // # ZUI53
2
+ //
3
+ // Copyright (c) 2011 Florian Günther
4
+ //
5
+ // Permission is hereby granted, free of charge, to any person obtaining
6
+ // a copy of this software and associated documentation files (the
7
+ // "Software"), to deal in the Software without restriction, including
8
+ // without limitation the rights to use, copy, modify, merge, publish,
9
+ // distribute, sublicense, and/or sell copies of the Software, and to
10
+ // permit persons to whom the Software is furnished to do so, subject to
11
+ // the following conditions:
12
+ //
13
+ // The above copyright notice and this permission notice shall be
14
+ // included in all copies or substantial portions of the Software.
15
+ //
16
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ //
24
+
25
+ function registerNS(ns)
26
+ {
27
+ var nsParts = ns.split(".");
28
+ var root = window;
29
+
30
+ for(var i=0; i<nsParts.length; i++)
31
+ {
32
+ if(typeof root[nsParts[i]] == "undefined")
33
+ root[nsParts[i]] = new Object();
34
+
35
+ root = root[nsParts[i]];
36
+ }
37
+ }
38
+
39
+ function namespace(name, callback)
40
+ {
41
+ registerNS(name);
42
+ callback( eval(name) );
43
+ }
@@ -0,0 +1,4 @@
1
+ //= require sylvester
2
+ //= require jquery.transform
3
+ //= require jquery.mousewheel
4
+ //= require ./zui53
@@ -0,0 +1,27 @@
1
+ namespace 'ZUI53.Surfaces', (exports)->
2
+ class exports.CSS
3
+ constructor: (@node)->
4
+ $(@node).transform({origin:['0','0']})
5
+ $(@node).css({
6
+ # '-webkit-transform-origin': '0 0',
7
+ # '-moz-transform-origin': '0 0',
8
+ # '-o-transform-origin': '0 0',
9
+ # 'transform-origin': '0 0',
10
+ 'position': 'absolute'
11
+ })
12
+
13
+ limits: ()->
14
+ null
15
+
16
+ apply: (panX, panY, scale)=>
17
+ $(@node).transform({matrix: [scale,0.0,0.0,scale,panX,panY]});
18
+ # matrix = "matrix(#{scale}, 0.0, 0.0, #{scale}, #{panX}, #{panY})"
19
+ # single = "translate(#{pX}px, #{pY}px) scale(#{scale}, #{scale})"
20
+
21
+ # $(@node).css("-webkit-transform", matrix)
22
+ # $(@node).css({
23
+ # # "-moz-transform": matrix,
24
+ # # "-o-transform": matrix,
25
+ # # "transform": matrix,
26
+ # "-webkit-transform": matrix
27
+ # })
@@ -0,0 +1,16 @@
1
+ namespace 'ZUI53.Surfaces', (exports)->
2
+ class exports.SVG
3
+ constructor: (@node)->
4
+ # console.log @node
5
+ # $(@node).css({
6
+ # 'position': 'absolute',
7
+ # 'width': '100%',
8
+ # 'height': '100%'
9
+ # })
10
+
11
+ limits: ()->
12
+ [0.0001, 20000] #20000
13
+
14
+ apply: (panX, panY, scale)=>
15
+ singleSVG = "translate(#{panX}, #{panY}) scale(#{scale}, #{scale})"
16
+ $(@node).attr("transform", singleSVG)
@@ -0,0 +1,76 @@
1
+ #= require ./toolset
2
+
3
+ namespace 'ZUI53.Tools', (exports)->
4
+ class exports.Pan extends exports.Base
5
+ constructor: (zui)->
6
+ @vp = zui
7
+ @eventDispatcher = zui.viewport #window
8
+
9
+ attach: ()=>
10
+ # console.log "Attaching PAN"
11
+ $('body').addClass('pan')
12
+ $(@eventDispatcher).bind 'mousedown', @start
13
+ $(@eventDispatcher).bind 'touchstart', @touch_start
14
+
15
+ detach: ()=>
16
+ # console.log "Detach PAN.."
17
+ $('body').removeClass('pan')
18
+
19
+ @touch_stop(null)
20
+
21
+ $(@eventDispatcher).unbind 'mousedown', @start
22
+ $(@eventDispatcher).unbind 'touchstart', @touch_start
23
+
24
+ start: (e)=>
25
+ # console.log "start panning"
26
+ $('body').addClass('panning')
27
+
28
+ @_start_with(e.screenX, e.screenY)
29
+
30
+ window.addEventListener 'mousemove', @pan, true
31
+ window.addEventListener 'mouseup', @stop, true
32
+
33
+ @stopEvent(e)
34
+
35
+ pan: (e)=>
36
+ @_pan_with(e.screenX, e.screenY)
37
+
38
+ stop: (e)=>
39
+ # console.log "stop panning"
40
+ $('body').removeClass('panning')
41
+
42
+ window.removeEventListener 'mousemove', @pan, true
43
+ window.removeEventListener 'mouseup', @stop, true
44
+
45
+ window.removeEventListener 'touchmove', @pan, true
46
+ window.removeEventListener 'touchend', @stop, true
47
+
48
+ @stopEvent(e)
49
+
50
+ touch_start: (e)=>
51
+ # console.log "start panning (touch)"
52
+ @_start_with(e.originalEvent.touches[0].clientX, e.originalEvent.touches[0].clientY)
53
+ @eventDispatcher.addEventListener 'touchmove', @touch_move, true
54
+ @eventDispatcher.addEventListener 'touchend', @touch_stop, true
55
+ e.originalEvent.preventDefault()
56
+
57
+ touch_move: (e)=>
58
+ @_pan_with(e.touches[0].clientX, e.touches[0].clientY)
59
+
60
+ touch_stop: (e)=>
61
+ @eventDispatcher.removeEventListener 'touchmove', @touch_move, true
62
+ @eventDispatcher.removeEventListener 'touchend', @touch_stop, true
63
+
64
+ _pan_with: (x, y)=>
65
+ # console.log "panning PAN Tool"
66
+ dX = x - @startX
67
+ dY = y - @startY
68
+
69
+ @startX = x
70
+ @startY = y
71
+
72
+ @vp.panBy(dX, dY)
73
+
74
+ _start_with: (x, y)=>
75
+ @startX = x
76
+ @startY = y
@@ -0,0 +1,84 @@
1
+ namespace 'ZUI53.Tools', (exports)->
2
+ class exports.Base
3
+ constructor: ()->
4
+ @set = null
5
+ @group = null
6
+ @attached = false
7
+ # @exclusive = false
8
+
9
+ attach: ()=>
10
+ @group.attach(@) if @group
11
+ @attached = true
12
+
13
+ detach: ()=>
14
+ @attached = false
15
+ # if @exclusive
16
+ # @makeUnexclusive()
17
+
18
+ makeExclusive: ()=>
19
+ # if @exclusive
20
+ # return
21
+ # @exclusive = true
22
+ @set.exclusive(@) if @set
23
+ @attach()
24
+
25
+ makeUnexclusive: ()=>
26
+ # if !@exclusive
27
+ # return
28
+ # @exclusive = false
29
+ @set.unexclusive() if @set
30
+
31
+ stopEvent: (e)=>
32
+ e.preventDefault()
33
+ if e.stopImmediatePropagation?
34
+ e.stopImmediatePropagation()
35
+ false
36
+
37
+ class exports.SetGroup
38
+ constructor: ()->
39
+ @tools = []
40
+ @current = null
41
+ @beforeExclusive = null
42
+
43
+ add: (tool)=>
44
+ tool.group = @
45
+ @tools.push(tool)
46
+ tool.attach() if @tools.length == 1
47
+
48
+ attach: (tool)=>
49
+ @current = tool
50
+ for t in @tools
51
+ t.detach() if t != tool
52
+
53
+ requestExclusive: (tool)=>
54
+ @current.detach() if @current and @current != tool
55
+ @beforeExclusive = @current
56
+
57
+ requestUnexclusive: ()=>
58
+ @current = @beforeExclusive
59
+ @current.attach() if @current
60
+
61
+ class exports.Set
62
+ constructor: (@default_tool)->
63
+ @groups = [ new exports.SetGroup() ]
64
+
65
+ @default_tool.set = @
66
+ @default_tool.attach() if @default_tool
67
+
68
+ add: (tool)=>
69
+ @groups[0].add(tool)
70
+ tool.set = @
71
+
72
+ exclusive: (tool)=>
73
+ # console.log 'Make Exclusive'
74
+ for g in @groups
75
+ g.requestExclusive(tool)
76
+
77
+ @default_tool.detach() if @default_tool != tool and @default_tool
78
+
79
+ unexclusive: ()=>
80
+ for g in @groups
81
+ g.requestUnexclusive()
82
+
83
+ @default_tool.attach() if @default_tool
84
+ # console.log 'Make UN-Exclusive'
@@ -0,0 +1,167 @@
1
+ #= require ./toolset
2
+
3
+ namespace 'ZUI53.Tools', (exports)->
4
+ class exports.Zoom extends exports.Base
5
+ constructor: (zui)->
6
+ @vp = zui
7
+ @eventDispatcher = zui.viewport
8
+ @use_capture = true # on event handling
9
+
10
+ @t1 = null
11
+ @t2 = null
12
+
13
+ @touch = {
14
+ touches: [],
15
+ touch_ids: []
16
+ }
17
+
18
+ attach: ()=>
19
+ $(@eventDispatcher).mousewheel @zoom
20
+ @eventDispatcher.addEventListener 'gesturestart', @gesture_start, @use_capture
21
+
22
+ @eventDispatcher.addEventListener 'MozTouchDown', @moz_touch_down, @use_capture
23
+ @eventDispatcher.addEventListener 'MozTouchUp', @moz_touch_up, @use_capture
24
+
25
+ detach: ()=>
26
+ $(@eventDispatcher).unmousewheel @zoom
27
+ @eventDispatcher.removeEventListener 'gesturestart', @gesture_start, @use_capture
28
+
29
+ @eventDispatcher.removeEventListener 'MozTouchDown', @moz_touch_down, @use_capture
30
+ @eventDispatcher.removeEventListener 'MozTouchUp', @moz_touch_up, @use_capture
31
+
32
+
33
+
34
+ fetch_touch: (e, value)=>
35
+ if @t1 and @t1.streamId == e.streamId
36
+ @t1 = value || e
37
+ else
38
+ @t2 = value || e
39
+
40
+ @update_moz_touch()
41
+
42
+ update_moz_touch: ()=>
43
+ if @t1 and @t2
44
+ # calc midpoint
45
+ try
46
+ mp = @find_midpoint( {touches: [@t1, @t2]} )
47
+ catch e
48
+ console.log e
49
+
50
+ else if @t1 or @t2
51
+ # remove
52
+ console.log 'only one'
53
+
54
+
55
+ create_touch_index: (streamId)=>
56
+ i = @touch.touch_ids.indexOf(streamId)
57
+ if i < 0
58
+ i = @touch.touch_ids.length
59
+ @touch.touch_ids[i] = streamId
60
+
61
+ return i
62
+
63
+ moz_touch_down: (e)=>
64
+ @touch_df = null
65
+
66
+ try
67
+ i = @create_touch_index(e.streamId)
68
+ @touch.touches[i] = e
69
+
70
+ if @touch.touches.length == 2
71
+ @_internal_gesture_start()
72
+ @eventDispatcher.addEventListener 'MozTouchMove', @moz_touch_move, @use_capture
73
+ catch e
74
+ console.log e
75
+
76
+ moz_touch_move: (e)=>
77
+ i = @create_touch_index(e.streamId)
78
+ @touch.touches[i] = e
79
+
80
+ @touch_move(@touch)
81
+ # @last_touch_p = @find_midpoint(@touch)
82
+
83
+ d = @find_distance(@touch)
84
+ if @touch_df
85
+ s = @touch_df * d
86
+ @gesture_move({scale: s})
87
+ else
88
+ @touch_df = 1/d
89
+
90
+
91
+ moz_touch_up: (e)=>
92
+ i = @touch.touch_ids.indexOf(e.streamId)
93
+ if i > 0
94
+ console.log "Removed: #{i}"
95
+ if @touch.touches.length == 2
96
+ @_internal_gesture_end()
97
+ @eventDispatcher.removeEventListener 'MozTouchMove', @moz_touch_move, @use_capture
98
+ # remove
99
+ @touch.touches.splice(i, 1)
100
+ @touch.touch_ids.splice(i, 1)
101
+
102
+ zoom: (e)=>
103
+ delta = e.wheelDelta || (e.detail * -1)
104
+ f = 0.05
105
+ if delta < 0
106
+ f *= -1
107
+
108
+ @vp.zoomBy(f, e.clientX, e.clientY)
109
+
110
+ @stopEvent(e)
111
+
112
+ gesture_start: (e)=>
113
+ @_internal_gesture_start()
114
+ @eventDispatcher.addEventListener 'gesturechange', @gesture_move, @use_capture
115
+ @eventDispatcher.addEventListener 'gestureend', @gesture_end, @use_capture
116
+ @eventDispatcher.addEventListener 'touchmove', @touch_move, @use_capture
117
+ e.preventDefault()
118
+
119
+ gesture_move: (e)=>
120
+ # console.log "Gesture Move"
121
+ if @last_touch_p
122
+ @vp.zoomSet( @start_scale * e.scale, @last_touch_p.e(1), @last_touch_p.e(2))
123
+
124
+ gesture_end: (e)=>
125
+ @eventDispatcher.removeEventListener 'touchmove', @touch_move, @use_capture
126
+ @eventDispatcher.removeEventListener 'gesturechange', @gesture_move, @use_capture
127
+ @eventDispatcher.removeEventListener 'gestureend', @gesture_end, @use_capture
128
+ @_internal_gesture_end()
129
+
130
+ _internal_gesture_start: ()=>
131
+ @makeExclusive()
132
+ @last_touch_p = null
133
+ @start_scale = @vp.scale
134
+
135
+ _internal_gesture_end: ()=>
136
+ @makeUnexclusive()
137
+
138
+ touch_move: (e)=>
139
+ # console.log "Touch Move: #{e.targetTouches.length}, #{e.touches.length}"
140
+ if @last_touch_p
141
+ new_touch_p = @find_midpoint(e)
142
+ d = new_touch_p.subtract(@last_touch_p)
143
+ @last_touch_p = new_touch_p
144
+ @vp.panBy(d.e(1), d.e(2))
145
+ else
146
+ @last_touch_p = @find_midpoint(e)
147
+
148
+ # Some Helper
149
+
150
+ find_midpoint: (e)=>
151
+ t1 = e.touches[0] #e.targetTouches[0]
152
+ t2 = e.touches[1] #e.targetTouches[1]
153
+ p1 = $V([t1.clientX, t1.clientY, 1])
154
+ p2 = $V([t2.clientX, t2.clientY, 1])
155
+
156
+ d = p2.subtract(p1).multiply(0.5)
157
+ p = p1.add(d)
158
+
159
+ find_distance: (e)=>
160
+ t1 = e.touches[0] #e.targetTouches[0]
161
+ t2 = e.touches[1] #e.targetTouches[1]
162
+ p1 = $V([t1.clientX, t1.clientY, 1])
163
+ p2 = $V([t2.clientX, t2.clientY, 1])
164
+
165
+ # d = p2.subtract(p1) #.multiply(0.5)
166
+ # d.length()
167
+ p2.distanceFrom(p1)