| 1 |
/** |
| 2 |
* SVGPan library 1.2.2 |
| 3 |
* ====================== |
| 4 |
* |
| 5 |
* Given an unique existing element with a given id (or by default, the first |
| 6 |
* g-element), including the library into any SVG adds the following |
| 7 |
* capabilities: |
| 8 |
* |
| 9 |
* - Mouse panning |
| 10 |
* - Mouse zooming (using the wheel) |
| 11 |
* - Object dragging |
| 12 |
* |
| 13 |
* You can configure the behaviour of the pan/zoom/drag via setOptions(). |
| 14 |
* |
| 15 |
* Known issues: |
| 16 |
* |
| 17 |
* - Zooming (while panning) on Safari has still some issues |
| 18 |
* |
| 19 |
* Releases: |
| 20 |
* |
| 21 |
* 1.2.2, Tue Aug 30 17:21:56 CEST 2011, Andrea Leofreddi |
| 22 |
* - Fixed viewBox on root tag (#7) |
| 23 |
* - Improved zoom speed (#2) |
| 24 |
* |
| 25 |
* 1.2.1, Mon Jul 4 00:33:18 CEST 2011, Andrea Leofreddi |
| 26 |
* - Fixed a regression with mouse wheel (now working on Firefox 5) |
| 27 |
* - Working with viewBox attribute (#4) |
| 28 |
* - Added "use strict;" and fixed resulting warnings (#5) |
| 29 |
* - Added configuration variables, dragging is disabled by default (#3) |
| 30 |
* |
| 31 |
* 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui |
| 32 |
* Fixed a bug with browser mouse handler interaction |
| 33 |
* |
| 34 |
* 1.1, Wed Feb 3 17:39:33 GMT 2010, Zeng Xiaohui |
| 35 |
* Updated the zoom code to support the mouse wheel on Safari/Chrome |
| 36 |
* |
| 37 |
* 1.0, Andrea Leofreddi |
| 38 |
* First release |
| 39 |
*/ |
| 40 |
|
| 41 |
/** |
| 42 |
* @license |
| 43 |
* This code is licensed under the following BSD license: |
| 44 |
* Copyright 2009-2010 Andrea Leofreddi <[email protected]>. All rights |
| 45 |
* reserved. |
| 46 |
* |
| 47 |
* Redistribution and use in source and binary forms, with or without |
| 48 |
* modification, are permitted provided that the following conditions are met: |
| 49 |
* |
| 50 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 51 |
* this list of conditions and the following disclaimer. |
| 52 |
* |
| 53 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 54 |
* notice, this list of conditions and the following disclaimer in the |
| 55 |
* documentation and/or other materials provided with the distribution. |
| 56 |
* |
| 57 |
* THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR |
| 58 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 59 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 60 |
* EVENT SHALL Andrea Leofreddi OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 61 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 62 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 63 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 64 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 65 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 66 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 67 |
* |
| 68 |
* The views and conclusions contained in the software and documentation are |
| 69 |
* those of the authors and should not be interpreted as representing official |
| 70 |
* policies, either expressed or implied, of Andrea Leofreddi. |
| 71 |
* |
| 72 |
*/ |
| 73 |
|
| 74 |
goog.provide('svgpan.SvgPan'); |
| 75 |
|
| 76 |
goog.require('goog.Disposable'); |
| 77 |
goog.require('goog.events'); |
| 78 |
goog.require('goog.events.EventType'); |
| 79 |
goog.require('goog.events.MouseWheelHandler'); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Instantiates an SvgPan object. |
| 85 |
* @param {string=} opt_graphElementId The id of the graph element. |
| 86 |
* @param {Element=} opt_root An optional document root. |
| 87 |
* @constructor |
| 88 |
* @extends {goog.Disposable} |
| 89 |
*/ |
| 90 |
svgpan.SvgPan = function(opt_graphElementId, opt_root) { |
| 91 |
svgpan.SvgPan.base(this, 'constructor'); |
| 92 |
|
| 93 |
/** @private {Element} */ |
| 94 |
this.root_ = opt_root || document.documentElement; |
| 95 |
|
| 96 |
/** @private {?string} */ |
| 97 |
this.graphElementId_ = opt_graphElementId || null; |
| 98 |
|
| 99 |
/** @private {boolean} */ |
| 100 |
this.cancelNextClick_ = false; |
| 101 |
|
| 102 |
/** @private {boolean} */ |
| 103 |
this.enablePan_ = true; |
| 104 |
|
| 105 |
/** @private {boolean} */ |
| 106 |
this.enableZoom_ = true; |
| 107 |
|
| 108 |
/** @private {boolean} */ |
| 109 |
this.enableDrag_ = false; |
| 110 |
|
| 111 |
/** @private {number} */ |
| 112 |
this.zoomScale_ = 0.4; |
| 113 |
|
| 114 |
/** @private {svgpan.SvgPan.State} */ |
| 115 |
this.state_ = svgpan.SvgPan.State.NONE; |
| 116 |
|
| 117 |
/** @private {Element} */ |
| 118 |
this.svgRoot_ = null; |
| 119 |
|
| 120 |
/** @private {Element} */ |
| 121 |
this.stateTarget_ = null; |
| 122 |
|
| 123 |
/** @private {SVGPoint} */ |
| 124 |
this.stateOrigin_ = null; |
| 125 |
|
| 126 |
/** @private {SVGMatrix} */ |
| 127 |
this.stateTf_ = null; |
| 128 |
|
| 129 |
/** @private {goog.events.MouseWheelHandler} */ |
| 130 |
this.mouseWheelHandler_ = null; |
| 131 |
|
| 132 |
this.setupHandlers_(); |
| 133 |
}; |
| 134 |
goog.inherits(svgpan.SvgPan, goog.Disposable); |
| 135 |
|
| 136 |
|
| 137 |
/** @override */ |
| 138 |
svgpan.SvgPan.prototype.disposeInternal = function() { |
| 139 |
svgpan.SvgPan.base(this, 'disposeInternal'); |
| 140 |
goog.events.removeAll(this.root_); |
| 141 |
this.mouseWheelHandler_.dispose(); |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* @enum {string} |
| 147 |
*/ |
| 148 |
svgpan.SvgPan.State = { |
| 149 |
NONE: 'none', |
| 150 |
PAN: 'pan', |
| 151 |
DRAG: 'drag' |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Enables/disables panning the entire SVG (default = true). |
| 157 |
* @param {boolean} enabled Whether or not to allow panning. |
| 158 |
*/ |
| 159 |
svgpan.SvgPan.prototype.setPanEnabled = function(enabled) { |
| 160 |
this.enablePan_ = enabled; |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Enables/disables zooming (default = true). |
| 166 |
* @param {boolean} enabled Whether or not to allow zooming (default = true). |
| 167 |
*/ |
| 168 |
svgpan.SvgPan.prototype.setZoomEnabled = function(enabled) { |
| 169 |
this.enableZoom_ = enabled; |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Enables/disables dragging individual SVG objects (default = false). |
| 175 |
* @param {boolean} enabled Whether or not to allow dragging of objects. |
| 176 |
*/ |
| 177 |
svgpan.SvgPan.prototype.setDragEnabled = function(enabled) { |
| 178 |
this.enableDrag_ = enabled; |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Sets the sensitivity of mousewheel zooming (default = 0.4). |
| 184 |
* @param {number} scale The new zoom scale. |
| 185 |
*/ |
| 186 |
svgpan.SvgPan.prototype.setZoomScale = function(scale) { |
| 187 |
this.zoomScale_ = scale; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Registers mouse event handlers. |
| 193 |
* @private |
| 194 |
*/ |
| 195 |
svgpan.SvgPan.prototype.setupHandlers_ = function() { |
| 196 |
goog.events.listen(this.root_, goog.events.EventType.CLICK, |
| 197 |
goog.bind(this.handleMouseClick_, this)); |
| 198 |
goog.events.listen(this.root_, goog.events.EventType.MOUSEUP, |
| 199 |
goog.bind(this.handleMouseUp_, this)); |
| 200 |
goog.events.listen(this.root_, goog.events.EventType.MOUSEDOWN, |
| 201 |
goog.bind(this.handleMouseDown_, this)); |
| 202 |
goog.events.listen(this.root_, goog.events.EventType.MOUSEMOVE, |
| 203 |
goog.bind(this.handleMouseMove_, this)); |
| 204 |
this.mouseWheelHandler_ = new goog.events.MouseWheelHandler(this.root_); |
| 205 |
goog.events.listen(this.mouseWheelHandler_, |
| 206 |
goog.events.MouseWheelHandler.EventType.MOUSEWHEEL, |
| 207 |
goog.bind(this.handleMouseWheel_, this)); |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Retrieves the root element for SVG manipulation. The element is then cached. |
| 213 |
* @param {Document} svgDoc The document. |
| 214 |
* @return {Element} The svg root. |
| 215 |
* @private |
| 216 |
*/ |
| 217 |
svgpan.SvgPan.prototype.getRoot_ = function(svgDoc) { |
| 218 |
if (!this.svgRoot_) { |
| 219 |
var r = this.graphElementId_ ? |
| 220 |
svgDoc.getElementById(this.graphElementId_) : svgDoc.documentElement; |
| 221 |
var t = r; |
| 222 |
while (t != svgDoc) { |
| 223 |
if (t.getAttribute('viewBox')) { |
| 224 |
this.setCtm_(r, r.getCTM()); |
| 225 |
t.removeAttribute('viewBox'); |
| 226 |
} |
| 227 |
t = t.parentNode; |
| 228 |
} |
| 229 |
this.svgRoot_ = r; |
| 230 |
} |
| 231 |
return this.svgRoot_; |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Instantiates an SVGPoint object with given event coordinates. |
| 237 |
* @param {!goog.events.Event} evt The event with coordinates. |
| 238 |
* @return {SVGPoint} The created point. |
| 239 |
* @private |
| 240 |
*/ |
| 241 |
svgpan.SvgPan.prototype.getEventPoint_ = function(evt) { |
| 242 |
return this.newPoint_(evt.clientX, evt.clientY); |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Instantiates an SVGPoint object with given coordinates. |
| 248 |
* @param {number} x The x coordinate. |
| 249 |
* @param {number} y The y coordinate. |
| 250 |
* @return {SVGPoint} The created point. |
| 251 |
* @private |
| 252 |
*/ |
| 253 |
svgpan.SvgPan.prototype.newPoint_ = function(x, y) { |
| 254 |
var p = this.root_.createSVGPoint(); |
| 255 |
p.x = x; |
| 256 |
p.y = y; |
| 257 |
return p; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
/** |
| 262 |
* Sets the current transform matrix of an element. |
| 263 |
* @param {Element} element The element. |
| 264 |
* @param {SVGMatrix} matrix The transform matrix. |
| 265 |
* @private |
| 266 |
*/ |
| 267 |
svgpan.SvgPan.prototype.setCtm_ = function(element, matrix) { |
| 268 |
var s = 'matrix(' + matrix.a + ',' + matrix.b + ',' + matrix.c + ',' + |
| 269 |
matrix.d + ',' + matrix.e + ',' + matrix.f + ')'; |
| 270 |
element.setAttribute('transform', s); |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Handle mouse wheel event. |
| 276 |
* @param {!goog.events.Event} evt The event. |
| 277 |
* @private |
| 278 |
*/ |
| 279 |
svgpan.SvgPan.prototype.handleMouseWheel_ = function(evt) { |
| 280 |
if (!this.enableZoom_) |
| 281 |
return; |
| 282 |
|
| 283 |
// Prevents scrolling. |
| 284 |
evt.preventDefault(); |
| 285 |
|
| 286 |
var svgDoc = evt.target.ownerDocument; |
| 287 |
|
| 288 |
var delta = evt.deltaY / -9; |
| 289 |
var z = Math.pow(1 + this.zoomScale_, delta); |
| 290 |
var g = this.getRoot_(svgDoc); |
| 291 |
var p = this.getEventPoint_(evt); |
| 292 |
p = p.matrixTransform(g.getCTM().inverse()); |
| 293 |
|
| 294 |
// Compute new scale matrix in current mouse position |
| 295 |
var k = this.root_.createSVGMatrix().translate( |
| 296 |
p.x, p.y).scale(z).translate(-p.x, -p.y); |
| 297 |
this.setCtm_(g, g.getCTM().multiply(k)); |
| 298 |
|
| 299 |
if (typeof(this.stateTf_) == 'undefined') { |
| 300 |
this.stateTf_ = g.getCTM().inverse(); |
| 301 |
} |
| 302 |
this.stateTf_ = |
| 303 |
this.stateTf_ ? this.stateTf_.multiply(k.inverse()) : this.stateTf_; |
| 304 |
}; |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Handle mouse move event. |
| 309 |
* @param {!goog.events.Event} evt The event. |
| 310 |
* @private |
| 311 |
*/ |
| 312 |
svgpan.SvgPan.prototype.handleMouseMove_ = function(evt) { |
| 313 |
if (evt.button != 0) { |
| 314 |
return; |
| 315 |
} |
| 316 |
this.handleMove(evt.clientX, evt.clientY, evt.target.ownerDocument); |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* Handles mouse motion for the given coordinates. |
| 322 |
* @param {number} x The x coordinate. |
| 323 |
* @param {number} y The y coordinate. |
| 324 |
* @param {Document} svgDoc The svg document. |
| 325 |
*/ |
| 326 |
svgpan.SvgPan.prototype.handleMove = function(x, y, svgDoc) { |
| 327 |
var g = this.getRoot_(svgDoc); |
| 328 |
if (this.state_ == svgpan.SvgPan.State.PAN && this.enablePan_) { |
| 329 |
// Pan mode |
| 330 |
var p = this.newPoint_(x, y).matrixTransform( |
| 331 |
/** @type {!SVGMatrix} */ (this.stateTf_)); |
| 332 |
this.setCtm_(g, this.stateTf_.inverse().translate( |
| 333 |
p.x - this.stateOrigin_.x, p.y - this.stateOrigin_.y)); |
| 334 |
this.cancelNextClick_ = true; |
| 335 |
} else if (this.state_ == svgpan.SvgPan.State.DRAG && this.enableDrag_) { |
| 336 |
// Drag mode |
| 337 |
var p = this.newPoint_(x, y).matrixTransform(g.getCTM().inverse()); |
| 338 |
this.setCtm_(this.stateTarget_, this.root_.createSVGMatrix().translate( |
| 339 |
p.x - this.stateOrigin_.x, p.y - this.stateOrigin_.y).multiply( |
| 340 |
g.getCTM().inverse()).multiply(this.stateTarget_.getCTM())); |
| 341 |
this.stateOrigin_ = p; |
| 342 |
} |
| 343 |
}; |
| 344 |
|
| 345 |
|
| 346 |
/** |
| 347 |
* Handle click event. |
| 348 |
* @param {!goog.events.Event} evt The event. |
| 349 |
* @private |
| 350 |
*/ |
| 351 |
svgpan.SvgPan.prototype.handleMouseDown_ = function(evt) { |
| 352 |
if (evt.button != 0) { |
| 353 |
return; |
| 354 |
} |
| 355 |
// Prevent selection while dragging. |
| 356 |
evt.preventDefault(); |
| 357 |
var svgDoc = evt.target.ownerDocument; |
| 358 |
|
| 359 |
var g = this.getRoot_(svgDoc); |
| 360 |
|
| 361 |
if (evt.target.tagName == 'svg' || !this.enableDrag_) { |
| 362 |
// Pan mode |
| 363 |
this.state_ = svgpan.SvgPan.State.PAN; |
| 364 |
this.stateTf_ = g.getCTM().inverse(); |
| 365 |
this.stateOrigin_ = this.getEventPoint_(evt).matrixTransform( |
| 366 |
/** @type {!SVGMatrix} */ (this.stateTf_)); |
| 367 |
} else { |
| 368 |
// Drag mode |
| 369 |
this.state_ = svgpan.SvgPan.State.DRAG; |
| 370 |
this.stateTarget_ = /** @type {Element} */ (evt.target); |
| 371 |
this.stateTf_ = g.getCTM().inverse(); |
| 372 |
this.stateOrigin_ = this.getEventPoint_(evt).matrixTransform( |
| 373 |
/** @type {!SVGMatrix} */ (this.stateTf_)); |
| 374 |
} |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
/** |
| 379 |
* Handle mouse button release event. |
| 380 |
* @param {!goog.events.Event} evt The event. |
| 381 |
* @private |
| 382 |
*/ |
| 383 |
svgpan.SvgPan.prototype.handleMouseUp_ = function(evt) { |
| 384 |
if (this.state_ != svgpan.SvgPan.State.NONE) { |
| 385 |
this.endPanOrDrag(); |
| 386 |
} |
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* Ends pan/drag mode. |
| 392 |
*/ |
| 393 |
svgpan.SvgPan.prototype.endPanOrDrag = function() { |
| 394 |
if (this.state_ != svgpan.SvgPan.State.NONE) { |
| 395 |
this.state_ = svgpan.SvgPan.State.NONE; |
| 396 |
} |
| 397 |
}; |
| 398 |
|
| 399 |
|
| 400 |
/** |
| 401 |
* Handle mouse clicks. |
| 402 |
* @param {!goog.events.Event} evt The event. |
| 403 |
* @private |
| 404 |
*/ |
| 405 |
svgpan.SvgPan.prototype.handleMouseClick_ = function(evt) { |
| 406 |
// We only set cancelNextClick_ after panning occurred, and use it to prevent |
| 407 |
// the default action that would otherwise take place when clicking on the |
| 408 |
// element (for instance, navigation on clickable links, but also any click |
| 409 |
// handler that may be set on an SVG element, in the case of active SVG |
| 410 |
// content) |
| 411 |
if (this.cancelNextClick_) { |
| 412 |
// Cancel potential click handler on active SVG content. |
| 413 |
evt.stopPropagation(); |
| 414 |
// Cancel navigation when panning on clickable links. |
| 415 |
evt.preventDefault(); |
| 416 |
} |
| 417 |
this.cancelNextClick_ = false; |
| 418 |
}; |
| 419 |
|
| 420 |
|
| 421 |
/** |
| 422 |
* Returns the current state. |
| 423 |
* @return {!svgpan.SvgPan.State} |
| 424 |
*/ |
| 425 |
svgpan.SvgPan.prototype.getState = function() { |
| 426 |
return this.state_; |
| 427 |
}; |