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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/**
* SVGPan library 1.2.2
* ======================
*
* Given an unique existing element with a given id (or by default, the first
* g-element), including the library into any SVG adds the following
* capabilities:
*
* - Mouse panning
* - Mouse zooming (using the wheel)
* - Object dragging
*
* You can configure the behaviour of the pan/zoom/drag via setOptions().
*
* Known issues:
*
* - Zooming (while panning) on Safari has still some issues
*
* Releases:
*
* 1.2.2, Tue Aug 30 17:21:56 CEST 2011, Andrea Leofreddi
* - Fixed viewBox on root tag (#7)
* - Improved zoom speed (#2)
*
* 1.2.1, Mon Jul 4 00:33:18 CEST 2011, Andrea Leofreddi
* - Fixed a regression with mouse wheel (now working on Firefox 5)
* - Working with viewBox attribute (#4)
* - Added "use strict;" and fixed resulting warnings (#5)
* - Added configuration variables, dragging is disabled by default (#3)
*
* 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui
* Fixed a bug with browser mouse handler interaction
*
* 1.1, Wed Feb 3 17:39:33 GMT 2010, Zeng Xiaohui
* Updated the zoom code to support the mouse wheel on Safari/Chrome
*
* 1.0, Andrea Leofreddi
* First release
*/
/**
* @license
* This code is licensed under the following BSD license:
* Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. 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.
*
*/
goog.provide('svgpan.SvgPan');
goog.require('goog.Disposable');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.events.MouseWheelHandler');
/**
* Instantiates an SvgPan object.
* @param {string=} opt_graphElementId The id of the graph element.
* @param {Element=} opt_root An optional document root.
* @constructor
* @extends {goog.Disposable}
*/
svgpan.SvgPan = function(opt_graphElementId, opt_root) {
svgpan.SvgPan.base(this, 'constructor');
/** @private {Element} */
this.root_ = opt_root || document.documentElement;
/** @private {?string} */
this.graphElementId_ = opt_graphElementId || null;
/** @private {boolean} */
this.cancelNextClick_ = false;
/** @private {boolean} */
this.enablePan_ = true;
/** @private {boolean} */
this.enableZoom_ = true;
/** @private {boolean} */
this.enableDrag_ = false;
/** @private {number} */
this.zoomScale_ = 0.4;
/** @private {svgpan.SvgPan.State} */
this.state_ = svgpan.SvgPan.State.NONE;
/** @private {Element} */
this.svgRoot_ = null;
/** @private {Element} */
this.stateTarget_ = null;
/** @private {SVGPoint} */
this.stateOrigin_ = null;
/** @private {SVGMatrix} */
this.stateTf_ = null;
/** @private {goog.events.MouseWheelHandler} */
this.mouseWheelHandler_ = null;
this.setupHandlers_();
};
goog.inherits(svgpan.SvgPan, goog.Disposable);
/** @override */
svgpan.SvgPan.prototype.disposeInternal = function() {
svgpan.SvgPan.base(this, 'disposeInternal');
goog.events.removeAll(this.root_);
this.mouseWheelHandler_.dispose();
};
/**
* @enum {string}
*/
svgpan.SvgPan.State = {
NONE: 'none',
PAN: 'pan',
DRAG: 'drag'
};
/**
* Enables/disables panning the entire SVG (default = true).
* @param {boolean} enabled Whether or not to allow panning.
*/
svgpan.SvgPan.prototype.setPanEnabled = function(enabled) {
this.enablePan_ = enabled;
};
/**
* Enables/disables zooming (default = true).
* @param {boolean} enabled Whether or not to allow zooming (default = true).
*/
svgpan.SvgPan.prototype.setZoomEnabled = function(enabled) {
this.enableZoom_ = enabled;
};
/**
* Enables/disables dragging individual SVG objects (default = false).
* @param {boolean} enabled Whether or not to allow dragging of objects.
*/
svgpan.SvgPan.prototype.setDragEnabled = function(enabled) {
this.enableDrag_ = enabled;
};
/**
* Sets the sensitivity of mousewheel zooming (default = 0.4).
* @param {number} scale The new zoom scale.
*/
svgpan.SvgPan.prototype.setZoomScale = function(scale) {
this.zoomScale_ = scale;
};
/**
* Registers mouse event handlers.
* @private
*/
svgpan.SvgPan.prototype.setupHandlers_ = function() {
goog.events.listen(this.root_, goog.events.EventType.CLICK,
goog.bind(this.handleMouseClick_, this));
goog.events.listen(this.root_, goog.events.EventType.MOUSEUP,
goog.bind(this.handleMouseUp_, this));
goog.events.listen(this.root_, goog.events.EventType.MOUSEDOWN,
goog.bind(this.handleMouseDown_, this));
goog.events.listen(this.root_, goog.events.EventType.MOUSEMOVE,
goog.bind(this.handleMouseMove_, this));
this.mouseWheelHandler_ = new goog.events.MouseWheelHandler(this.root_);
goog.events.listen(this.mouseWheelHandler_,
goog.events.MouseWheelHandler.EventType.MOUSEWHEEL,
goog.bind(this.handleMouseWheel_, this));
};
/**
* Retrieves the root element for SVG manipulation. The element is then cached.
* @param {Document} svgDoc The document.
* @return {Element} The svg root.
* @private
*/
svgpan.SvgPan.prototype.getRoot_ = function(svgDoc) {
if (!this.svgRoot_) {
var r = this.graphElementId_ ?
svgDoc.getElementById(this.graphElementId_) : svgDoc.documentElement;
var t = r;
while (t != svgDoc) {
if (t.getAttribute('viewBox')) {
this.setCtm_(r, r.getCTM());
t.removeAttribute('viewBox');
}
t = t.parentNode;
}
this.svgRoot_ = r;
}
return this.svgRoot_;
};
/**
* Instantiates an SVGPoint object with given event coordinates.
* @param {!goog.events.Event} evt The event with coordinates.
* @return {SVGPoint} The created point.
* @private
*/
svgpan.SvgPan.prototype.getEventPoint_ = function(evt) {
return this.newPoint_(evt.clientX, evt.clientY);
};
/**
* Instantiates an SVGPoint object with given coordinates.
* @param {number} x The x coordinate.
* @param {number} y The y coordinate.
* @return {SVGPoint} The created point.
* @private
*/
svgpan.SvgPan.prototype.newPoint_ = function(x, y) {
var p = this.root_.createSVGPoint();
p.x = x;
p.y = y;
return p;
};
/**
* Sets the current transform matrix of an element.
* @param {Element} element The element.
* @param {SVGMatrix} matrix The transform matrix.
* @private
*/
svgpan.SvgPan.prototype.setCtm_ = function(element, matrix) {
var s = 'matrix(' + matrix.a + ',' + matrix.b + ',' + matrix.c + ',' +
matrix.d + ',' + matrix.e + ',' + matrix.f + ')';
element.setAttribute('transform', s);
};
/**
* Handle mouse wheel event.
* @param {!goog.events.Event} evt The event.
* @private
*/
svgpan.SvgPan.prototype.handleMouseWheel_ = function(evt) {
if (!this.enableZoom_)
return;
// Prevents scrolling.
evt.preventDefault();
var svgDoc = evt.target.ownerDocument;
var delta = evt.deltaY / -9;
var z = Math.pow(1 + this.zoomScale_, delta);
var g = this.getRoot_(svgDoc);
var p = this.getEventPoint_(evt);
p = p.matrixTransform(g.getCTM().inverse());
// Compute new scale matrix in current mouse position
var k = this.root_.createSVGMatrix().translate(
p.x, p.y).scale(z).translate(-p.x, -p.y);
this.setCtm_(g, g.getCTM().multiply(k));
if (typeof(this.stateTf_) == 'undefined') {
this.stateTf_ = g.getCTM().inverse();
}
this.stateTf_ =
this.stateTf_ ? this.stateTf_.multiply(k.inverse()) : this.stateTf_;
};
/**
* Handle mouse move event.
* @param {!goog.events.Event} evt The event.
* @private
*/
svgpan.SvgPan.prototype.handleMouseMove_ = function(evt) {
if (evt.button != 0) {
return;
}
this.handleMove(evt.clientX, evt.clientY, evt.target.ownerDocument);
};
/**
* Handles mouse motion for the given coordinates.
* @param {number} x The x coordinate.
* @param {number} y The y coordinate.
* @param {Document} svgDoc The svg document.
*/
svgpan.SvgPan.prototype.handleMove = function(x, y, svgDoc) {
var g = this.getRoot_(svgDoc);
if (this.state_ == svgpan.SvgPan.State.PAN && this.enablePan_) {
// Pan mode
var p = this.newPoint_(x, y).matrixTransform(
/** @type {!SVGMatrix} */ (this.stateTf_));
this.setCtm_(g, this.stateTf_.inverse().translate(
p.x - this.stateOrigin_.x, p.y - this.stateOrigin_.y));
this.cancelNextClick_ = true;
} else if (this.state_ == svgpan.SvgPan.State.DRAG && this.enableDrag_) {
// Drag mode
var p = this.newPoint_(x, y).matrixTransform(g.getCTM().inverse());
this.setCtm_(this.stateTarget_, this.root_.createSVGMatrix().translate(
p.x - this.stateOrigin_.x, p.y - this.stateOrigin_.y).multiply(
g.getCTM().inverse()).multiply(this.stateTarget_.getCTM()));
this.stateOrigin_ = p;
}
};
/**
* Handle click event.
* @param {!goog.events.Event} evt The event.
* @private
*/
svgpan.SvgPan.prototype.handleMouseDown_ = function(evt) {
if (evt.button != 0) {
return;
}
// Prevent selection while dragging.
evt.preventDefault();
var svgDoc = evt.target.ownerDocument;
var g = this.getRoot_(svgDoc);
if (evt.target.tagName == 'svg' || !this.enableDrag_) {
// Pan mode
this.state_ = svgpan.SvgPan.State.PAN;
this.stateTf_ = g.getCTM().inverse();
this.stateOrigin_ = this.getEventPoint_(evt).matrixTransform(
/** @type {!SVGMatrix} */ (this.stateTf_));
} else {
// Drag mode
this.state_ = svgpan.SvgPan.State.DRAG;
this.stateTarget_ = /** @type {Element} */ (evt.target);
this.stateTf_ = g.getCTM().inverse();
this.stateOrigin_ = this.getEventPoint_(evt).matrixTransform(
/** @type {!SVGMatrix} */ (this.stateTf_));
}
};
/**
* Handle mouse button release event.
* @param {!goog.events.Event} evt The event.
* @private
*/
svgpan.SvgPan.prototype.handleMouseUp_ = function(evt) {
if (this.state_ != svgpan.SvgPan.State.NONE) {
this.endPanOrDrag();
}
};
/**
* Ends pan/drag mode.
*/
svgpan.SvgPan.prototype.endPanOrDrag = function() {
if (this.state_ != svgpan.SvgPan.State.NONE) {
this.state_ = svgpan.SvgPan.State.NONE;
}
};
/**
* Handle mouse clicks.
* @param {!goog.events.Event} evt The event.
* @private
*/
svgpan.SvgPan.prototype.handleMouseClick_ = function(evt) {
// We only set cancelNextClick_ after panning occurred, and use it to prevent
// the default action that would otherwise take place when clicking on the
// element (for instance, navigation on clickable links, but also any click
// handler that may be set on an SVG element, in the case of active SVG
// content)
if (this.cancelNextClick_) {
// Cancel potential click handler on active SVG content.
evt.stopPropagation();
// Cancel navigation when panning on clickable links.
evt.preventDefault();
}
this.cancelNextClick_ = false;
};
/**
* Returns the current state.
* @return {!svgpan.SvgPan.State}
*/
svgpan.SvgPan.prototype.getState = function() {
return this.state_;
};
|