forked from semiaddict/raphael-zpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raphael-zpd.js
352 lines (263 loc) · 9.52 KB
/
raphael-zpd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Raphaël-ZPD: A zoom/pan/drag plugin for Raphaël.
* ==================================================
*
* This code is licensed under the following BSD license:
*
* Copyright 2010 Gabriel Zabusek <[email protected]> (Interface and feature extensions and modifications). All rights reserved.
* Copyright 2010 Daniel Assange <[email protected]> (Raphaël integration and extensions). All rights reserved.
* Copyright 2009-2010 Andrea Leofreddi <[email protected]> (original author). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Andrea Leofreddi.
*/
var raphaelZPDId = 0;
RaphaelZPD = function(raphaelPaper, o) {
function supportsSVG() {
return document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1");
}
if (!supportsSVG()) {
return null;
}
var me = this;
me.initialized = false;
me.opts = {
zoom: true, pan: true, drag: true, // Enable/disable core functionalities.
zoomThreshold: null, // Zoom [out, in] boundaries. E.g [-100, 10].
};
me.id = ++raphaelZPDId;
me.root = raphaelPaper.canvas;
me.gelem = document.createElementNS('http://www.w3.org/2000/svg', 'g');
me.gelem.id = 'viewport'+me.id;
me.root.appendChild(me.gelem);
function overrideElements(paper) {
var elementTypes = ['circle', 'rect', 'ellipse', 'image', 'text', 'path'];
for(var i = 0; i < elementTypes.length; i++) {
overrideElementFunc(paper, elementTypes[i]);
}
}
function overrideElementFunc(paper, elementType) {
paper[elementType] = function(oldFunc) {
return function() {
var element = oldFunc.apply(paper, arguments);
element.gelem = me.gelem;
me.gelem.appendChild(element.node);
return element;
};
}(paper[elementType]);
}
overrideElements(raphaelPaper);
function transformEvent(evt) {
if (typeof evt.clientX != "number") return evt;
svgDoc = evt.target.ownerDocument;
var g = svgDoc.getElementById("viewport"+me.id);
var p = me.getEventPoint(evt);
p = p.matrixTransform(g.getCTM().inverse());
evt.zoomedX = p.x;
evt.zoomedY = p.y;
return evt;
}
var events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'touchstart', 'touchmove', 'touchend', 'orientationchange', 'touchcancel', 'gesturestart', 'gesturechange', 'gestureend'];
events.forEach(function(eventName) {
var oldFunc = Raphael.el[eventName];
Raphael.el[eventName] = function(fn, scope) {
if (fn === undefined) return;
var wrap = function(evt) {
return fn.apply(this, [transformEvent(evt)]);
}
return oldFunc.apply(this, [wrap, scope]);
}
});
//raphaelPaper.canvas = me.gelem;
me.state = 'none';
me.stateTarget = null;
me.stateOrigin = null;
me.stateTf = null;
me.zoomCurrent = 0;
if (o) {
for (key in o) {
if (me.opts[key] !== undefined) {
me.opts[key] = o[key];
}
}
}
/**
* Handler registration
*/
me.setupHandlers = function(root) {
me.root.onmousedown = me.handleMouseDown;
me.root.onmousemove = me.handleMouseMove;
me.root.onmouseup = me.handleMouseUp;
//me.root.onmouseout = me.handleMouseUp; // Decomment me to stop the pan functionality when dragging out of the SVG element
if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0)
me.root.addEventListener('mousewheel', me.handleMouseWheel, false); // Chrome/Safari
else
me.root.addEventListener('DOMMouseScroll', me.handleMouseWheel, false); // Others
}
/**
* Instance an SVGPoint object with given event coordinates.
*/
me.getEventPoint = function(evt) {
var p = me.root.createSVGPoint();
p.x = evt.clientX;
p.y = evt.clientY;
return p;
}
/**
* Sets the current transform matrix of an element.
*/
me.setCTM = function(element, matrix) {
var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
element.setAttribute("transform", s);
}
/**
* Dumps a matrix to a string (useful for debug).
*/
me.dumpMatrix = function(matrix) {
var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
return s;
}
/**
* Sets attributes of an element.
*/
me.setAttributes = function(element, attributes) {
for (i in attributes)
element.setAttributeNS(null, i, attributes[i]);
}
/**
* Handle mouse move event.
*/
me.handleMouseWheel = function(evt) {
if (!me.opts.zoom) return;
if (evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
var delta;
if (evt.wheelDelta)
delta = evt.wheelDelta / 3600; // Chrome/Safari
else
delta = evt.detail / -90; // Mozilla
if (delta > 0) {
if (me.opts.zoomThreshold)
if (me.opts.zoomThreshold[1] <= me.zoomCurrent) return;
me.zoomCurrent++;
} else {
if (me.opts.zoomThreshold)
if (me.opts.zoomThreshold[0] >= me.zoomCurrent) return;
me.zoomCurrent--;
}
var z = 1 + delta; // Zoom factor: 0.9/1.1
var g = svgDoc.getElementById("viewport"+me.id);
var p = me.getEventPoint(evt);
p = p.matrixTransform(g.getCTM().inverse());
// Compute new scale matrix in current mouse position
var k = me.root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
me.setCTM(g, g.getCTM().multiply(k));
if (!me.stateTf)
me.stateTf = g.getCTM().inverse();
me.stateTf = me.stateTf.multiply(k.inverse());
}
/**
* Handle mouse move event.
*/
me.handleMouseMove = function(evt) {
if (evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
var g = svgDoc.getElementById("viewport"+me.id);
if (me.state == 'pan') {
// Pan mode
if (!me.opts.pan) return;
var p = me.getEventPoint(evt).matrixTransform(me.stateTf);
me.setCTM(g, me.stateTf.inverse().translate(p.x - me.stateOrigin.x, p.y - me.stateOrigin.y));
} else if (me.state == 'move') {
// Move mode
if (!me.opts.drag) return;
var p = me.getEventPoint(evt).matrixTransform(g.getCTM().inverse());
me.setCTM(me.stateTarget, me.root.createSVGMatrix().translate(p.x - me.stateOrigin.x, p.y - me.stateOrigin.y).multiply(g.getCTM().inverse()).multiply(me.stateTarget.getCTM()));
me.stateOrigin = p;
}
}
/**
* Handle click event.
*/
me.handleMouseDown = function(evt) {
if (evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
var g = svgDoc.getElementById("viewport"+me.id);
if (evt.target.tagName == "svg" || !me.opts.drag || (me.opts.drag && evt.target.draggable == false)) {
// Pan mode
if (!me.opts.pan) return;
me.state = 'pan';
me.stateTf = g.getCTM().inverse();
me.stateOrigin = me.getEventPoint(evt).matrixTransform(me.stateTf);
} else {
// Move mode
if (!me.opts.drag || evt.target.draggable == false) return;
me.state = 'move';
me.stateTarget = evt.target;
me.stateTf = g.getCTM().inverse();
me.stateOrigin = me.getEventPoint(evt).matrixTransform(me.stateTf);
}
}
/**
* Handle mouse button release event.
*/
me.handleMouseUp = function(evt) {
if (evt.preventDefault)
evt.preventDefault();
evt.returnValue = false;
var svgDoc = evt.target.ownerDocument;
if ((me.state == 'pan' && me.opts.pan) || (me.state == 'move' && me.opts.drag)) {
// Quit pan mode
me.state = '';
}
}
// end of constructor
me.setupHandlers(me.root);
me.initialized = true;
}
Raphael.fn.ZPDPanTo = function(x, y) {
var me = this;
if (me.gelem.getCTM() == null) {
alert('failed');
return null;
}
var stateTf = me.gelem.getCTM().inverse();
var svg = document.getElementsByTagName("svg")[0];
if (!svg.createSVGPoint) alert("no svg");
var p = svg.createSVGPoint();
p.x = x;
p.y = y;
p = p.matrixTransform(stateTf);
var element = me.gelem;
var matrix = stateTf.inverse().translate(p.x, p.y);
var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
element.setAttribute("transform", s);
return me;
}