|
@@ -1,17 +1,19 @@
|
1
|
1
|
// SVG pan and zoom library.
|
2
|
2
|
// See copyright notice in string constant below.
|
3
|
3
|
|
4
|
|
-package svg
|
|
4
|
+package svgpan
|
5
|
5
|
|
6
|
|
-// https://www.cyberz.org/projects/SVGPan/SVGPan.js
|
|
6
|
+// https://github.com/aleofreddi/svgpan
|
7
|
7
|
|
8
|
|
-const svgPanJS = `
|
9
|
|
-/**
|
10
|
|
- * SVGPan library 1.2.1
|
|
8
|
+// JSSource returns the svgpan.js file
|
|
9
|
+const JSSource = `
|
|
10
|
+/**
|
|
11
|
+ * SVGPan library 1.2.2
|
11
|
12
|
* ======================
|
12
|
13
|
*
|
13
|
|
- * Given an unique existing element with id "viewport" (or when missing, the first g
|
14
|
|
- * element), including the the library into any SVG adds the following capabilities:
|
|
14
|
+ * Given an unique existing element with id "viewport" (or when missing, the
|
|
15
|
+ * first g-element), including the the library into any SVG adds the following
|
|
16
|
+ * capabilities:
|
15
|
17
|
*
|
16
|
18
|
* - Mouse panning
|
17
|
19
|
* - Mouse zooming (using the wheel)
|
|
@@ -26,6 +28,10 @@ const svgPanJS = `
|
26
|
28
|
*
|
27
|
29
|
* Releases:
|
28
|
30
|
*
|
|
31
|
+ * 1.2.2, Tue Aug 30 17:21:56 CEST 2011, Andrea Leofreddi
|
|
32
|
+ * - Fixed viewBox on root tag (#7)
|
|
33
|
+ * - Improved zoom speed (#2)
|
|
34
|
+ *
|
29
|
35
|
* 1.2.1, Mon Jul 4 00:33:18 CEST 2011, Andrea Leofreddi
|
30
|
36
|
* - Fixed a regression with mouse wheel (now working on Firefox 5)
|
31
|
37
|
* - Working with viewBox attribute (#4)
|
|
@@ -43,28 +49,30 @@ const svgPanJS = `
|
43
|
49
|
*
|
44
|
50
|
* This code is licensed under the following BSD license:
|
45
|
51
|
*
|
46
|
|
- * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.
|
47
|
|
- *
|
|
52
|
+ * Copyright 2009-2017 Andrea Leofreddi <a.leofreddi@vleo.net>. All rights reserved.
|
|
53
|
+ *
|
48
|
54
|
* Redistribution and use in source and binary forms, with or without modification, are
|
49
|
55
|
* permitted provided that the following conditions are met:
|
50
|
|
- *
|
51
|
|
- * 1. Redistributions of source code must retain the above copyright notice, this list of
|
52
|
|
- * conditions and the following disclaimer.
|
53
|
|
- *
|
54
|
|
- * 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
55
|
|
- * of conditions and the following disclaimer in the documentation and/or other materials
|
56
|
|
- * provided with the distribution.
|
57
|
|
- *
|
58
|
|
- * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ` + "``AS IS''" + ` AND ANY EXPRESS OR IMPLIED
|
59
|
|
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
60
|
|
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
|
|
56
|
+ *
|
|
57
|
+ * 1. Redistributions of source code must retain the above copyright
|
|
58
|
+ * notice, this list of conditions and the following disclaimer.
|
|
59
|
+ * 2. Redistributions in binary form must reproduce the above copyright
|
|
60
|
+ * notice, this list of conditions and the following disclaimer in the
|
|
61
|
+ * documentation and/or other materials provided with the distribution.
|
|
62
|
+ * 3. Neither the name of the copyright holder nor the names of its
|
|
63
|
+ * contributors may be used to endorse or promote products derived from
|
|
64
|
+ * this software without specific prior written permission.
|
|
65
|
+ *
|
|
66
|
+ * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS
|
|
67
|
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
68
|
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR
|
61
|
69
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
62
|
70
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
63
|
71
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
64
|
72
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
65
|
73
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
66
|
74
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
67
|
|
- *
|
|
75
|
+ *
|
68
|
76
|
* The views and conclusions contained in the software and documentation are those of the
|
69
|
77
|
* authors and should not be interpreted as representing official policies, either expressed
|
70
|
78
|
* or implied, of Andrea Leofreddi.
|
|
@@ -72,19 +80,20 @@ const svgPanJS = `
|
72
|
80
|
|
73
|
81
|
"use strict";
|
74
|
82
|
|
75
|
|
-/// CONFIGURATION
|
|
83
|
+/// CONFIGURATION
|
76
|
84
|
/// ====>
|
77
|
85
|
|
78
|
86
|
var enablePan = 1; // 1 or 0: enable or disable panning (default enabled)
|
79
|
87
|
var enableZoom = 1; // 1 or 0: enable or disable zooming (default enabled)
|
80
|
88
|
var enableDrag = 0; // 1 or 0: enable or disable dragging (default disabled)
|
|
89
|
+var zoomScale = 0.2; // Zoom sensitivity
|
81
|
90
|
|
82
|
91
|
/// <====
|
83
|
|
-/// END OF CONFIGURATION
|
|
92
|
+/// END OF CONFIGURATION
|
84
|
93
|
|
85
|
94
|
var root = document.documentElement;
|
86
|
95
|
|
87
|
|
-var state = 'none', svgRoot, stateTarget, stateOrigin, stateTf;
|
|
96
|
+var state = 'none', svgRoot = null, stateTarget, stateOrigin, stateTf;
|
88
|
97
|
|
89
|
98
|
setupHandlers(root);
|
90
|
99
|
|
|
@@ -109,22 +118,20 @@ function setupHandlers(root){
|
109
|
118
|
* Retrieves the root element for SVG manipulation. The element is then cached into the svgRoot global variable.
|
110
|
119
|
*/
|
111
|
120
|
function getRoot(root) {
|
112
|
|
- if(typeof(svgRoot) == "undefined") {
|
113
|
|
- var g = null;
|
114
|
|
-
|
115
|
|
- g = root.getElementById("viewport");
|
|
121
|
+ if(svgRoot == null) {
|
|
122
|
+ var r = root.getElementById("viewport") ? root.getElementById("viewport") : root.documentElement, t = r;
|
116
|
123
|
|
117
|
|
- if(g == null)
|
118
|
|
- g = root.getElementsByTagName('g')[0];
|
|
124
|
+ while(t != root) {
|
|
125
|
+ if(t.getAttribute("viewBox")) {
|
|
126
|
+ setCTM(r, t.getCTM());
|
119
|
127
|
|
120
|
|
- if(g == null)
|
121
|
|
- alert('Unable to obtain SVG root element');
|
|
128
|
+ t.removeAttribute("viewBox");
|
|
129
|
+ }
|
122
|
130
|
|
123
|
|
- setCTM(g, g.getCTM());
|
|
131
|
+ t = t.parentNode;
|
|
132
|
+ }
|
124
|
133
|
|
125
|
|
- g.removeAttribute("viewBox");
|
126
|
|
-
|
127
|
|
- svgRoot = g;
|
|
134
|
+ svgRoot = r;
|
128
|
135
|
}
|
129
|
136
|
|
130
|
137
|
return svgRoot;
|
|
@@ -185,11 +192,11 @@ function handleMouseWheel(evt) {
|
185
|
192
|
var delta;
|
186
|
193
|
|
187
|
194
|
if(evt.wheelDelta)
|
188
|
|
- delta = evt.wheelDelta / 3600; // Chrome/Safari
|
|
195
|
+ delta = evt.wheelDelta / 360; // Chrome/Safari
|
189
|
196
|
else
|
190
|
|
- delta = evt.detail / -90; // Mozilla
|
|
197
|
+ delta = evt.detail / -9; // Mozilla
|
191
|
198
|
|
192
|
|
- var z = 1 + delta; // Zoom factor: 0.9/1.1
|
|
199
|
+ var z = Math.pow(1 + zoomScale, delta);
|
193
|
200
|
|
194
|
201
|
var g = getRoot(svgDoc);
|
195
|
202
|
|
|
@@ -250,8 +257,8 @@ function handleMouseDown(evt) {
|
250
|
257
|
var g = getRoot(svgDoc);
|
251
|
258
|
|
252
|
259
|
if(
|
253
|
|
- evt.target.tagName == "svg"
|
254
|
|
- || !enableDrag // Pan anyway when drag is disabled and the user clicked on an element
|
|
260
|
+ evt.target.tagName == "svg"
|
|
261
|
+ || !enableDrag // Pan anyway when drag is disabled and the user clicked on an element
|
255
|
262
|
) {
|
256
|
263
|
// Pan mode
|
257
|
264
|
state = 'pan';
|
|
@@ -287,5 +294,4 @@ function handleMouseUp(evt) {
|
287
|
294
|
state = '';
|
288
|
295
|
}
|
289
|
296
|
}
|
290
|
|
-
|
291
|
297
|
`
|