Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Finally got to work bounded panning and zooming. Credits to Rehno Lin…

…deque for helping with the math. http://bit.ly/9VTVr0
  • Loading branch information...
commit ddcf23bdb03876ed7a35985609860e74f2cbf227 1 parent c3d952f
Michael Aufreiter authored September 02, 2010

Showing 1 changed file with 45 additions and 50 deletions. Show diff stats Hide diff stats

  1. 95  src/scene/behaviors.js
95  src/scene/behaviors.js
... ...
@@ -1,59 +1,54 @@
1  
-uv.ZoomBehavior = function(display) {
2  
-  function zoom(zoom, rx, ry) {
3  
-    display.tView.translate(rx, ry);
4  
-    display.tView.scale(zoom, zoom);
5  
-    display.tView.translate(-rx, -ry);
  1
+uv.behaviors = {};
  2
+
  3
+uv.behaviors.adjust = function(display, m) {
  4
+  var b = display.bounds();
  5
+  
  6
+  // clamp to scene boundaries
  7
+  if (display.bounded) {
  8
+    m.a = m.d = Math.max(1, m.a);
  9
+    m.tx = Math.max(b.x, Math.min(0, m.tx));
  10
+    m.ty = Math.max(b.y, Math.min(0, m.ty));
6 11
   }
  12
+  return m;
  13
+};
  14
+
  15
+uv.behaviors.Zoom = function(display) {
7 16
   display.$canvas.bind('mousewheel', function(event, delta) {
8  
-    display.zoom += 0.02 * delta;
9  
-    zoom(1+0.02 * delta, display.scene.mouseX, display.scene.mouseY);
  17
+    var m = display.tView.scale(
  18
+          1+0.005 * delta,
  19
+          1+0.005 * delta,
  20
+          uv.Point(display.scene.mouseX, display.scene.mouseY)
  21
+        );
  22
+    display.tView = uv.behaviors.adjust(display, m);
10 23
     display.callbacks.viewChange.call(display);
11 24
   });
12 25
 };
13 26
 
14  
-uv.PanBehavior = function(display) {
15  
-  var paning = false,
16  
-      mouseX, mouseY,
17  
-      startX, startY,
18  
-      offsetX = 0,
19  
-      offsetY = 0,
20  
-      prevOffsetX = 0,
21  
-      prevOffsetY = 0;
  27
+uv.behaviors.Pan = function(display) {
  28
+  var pos, // initial mouse position
  29
+      view, // cached view matrix
  30
+      panning = false;
22 31
   
23  
-  display.$canvas.bind('mousedown', function(event) {
24  
-    if (display.mouseX) {
25  
-      paning = true;
26  
-      startX = display.mouseX;
27  
-      startY = display.mouseY;
28  
-      prevOffsetX = 0;
29  
-      prevOffsetY = 0;      
30  
-    }
31  
-  });
  32
+  function mouseDown() {
  33
+    p = uv.Point(display.mouseX, display.mouseY);
  34
+    view = display.tView;
  35
+    panning = true;
  36
+  }
32 37
   
33  
-  display.$canvas.bind('mouseup', function(event) {
34  
-    paning = false;
35  
-  });
  38
+  function mouseMove() {
  39
+    if (!panning) return;
  40
+    var x = (display.mouseX - p.x),
  41
+        y = (display.mouseY - p.y),
  42
+        m = uv.Matrix.translation(x, y).concat(view);
  43
+    display.tView = uv.behaviors.adjust(display, m);
  44
+  }
36 45
   
37  
-  display.$canvas.bind('mousemove', function(event) {
38  
-    var cache;
39  
-    if (paning) {
40  
-      offsetX = display.mouseX-startX;
41  
-      offsetY = display.mouseY -startY;
42  
-      
43  
-      deltaX = offsetX - prevOffsetX;
44  
-      deltaY = offsetY - prevOffsetY;
45  
-      
46  
-      prevOffsetX = offsetX;
47  
-      prevOffsetY = offsetY;
48  
-      
49  
-      // The new translate is performed first, to prevent
50  
-      // it from being scaled by the current view matrix
51  
-      cache = new uv.Matrix2D(display.tView);
52  
-      display.tView.reset();
53  
-      display.tView.translate(deltaX, deltaY);
54  
-      display.tView.apply(cache);
55  
-      display.callbacks.viewChange.call(display);
56  
-    }
57  
-  });
58  
-};
59  
-
  46
+  function release() {
  47
+    panning = false;
  48
+  }
  49
+  
  50
+  display.$canvas.bind('mousedown', mouseDown);
  51
+  display.$canvas.bind('mousemove', mouseMove);
  52
+  display.$canvas.bind('mouseup', release);
  53
+  display.$canvas.bind('mouseout', release);
  54
+};

1 note on commit ddcf23b

Rehno Lindeque

Lol, thanks for the credit :D

Please sign in to comment.
Something went wrong with that request. Please try again.