-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b033a97
commit 59d2f3c
Showing
8 changed files
with
396 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
.DS_Store | ||
.idea | ||
node_modules/ | ||
.vscode/ | ||
**/.* | ||
node_modules | ||
bower_components | ||
test | ||
tests | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,8 @@ | |
"bower_components", | ||
"test", | ||
"tests" | ||
] | ||
], | ||
"dependencies": { | ||
"svg.js": "^2.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/*! | ||
* svg.panzoom.js - A plugin for svg.js that enables panzoom for viewport elements | ||
* @version 1.0.0 | ||
* https://github.com/svgdotjs/svg.panzoom.js#readme | ||
* | ||
* @copyright Ulrich-Matthias Schäfer | ||
* @license MIT | ||
*/; | ||
;(function() { | ||
"use strict"; | ||
|
||
var normalizeEvent = function(ev) { | ||
if(!ev.touches) { | ||
ev.touches = [{clientX: ev.clientX, clientY: ev.clientY}] | ||
} | ||
|
||
return ev.touches | ||
} | ||
|
||
SVG.extend(SVG.Doc, SVG.Nested, { | ||
|
||
panZoom: function(options) { | ||
options = options || {} | ||
var zoomFactor = options.zoomFactor || 0.03 | ||
|
||
var lastP, lastTouches, zoomInProgress = false | ||
|
||
var wheelZoom = function(ev) { | ||
ev.preventDefault() | ||
|
||
var zoomAmount = this.zoom() - zoomFactor * ev.deltaY/Math.abs(ev.deltaY) | ||
, p = this.point(ev.clientX, ev.clientY) | ||
|
||
this.zoom(zoomAmount, p) | ||
} | ||
|
||
var pinchZoomStart = function(ev) { | ||
lastTouches = normalizeEvent(ev) | ||
|
||
if(lastTouches.length < 2) return | ||
ev.preventDefault() | ||
|
||
if(this.fire('pinchZoomStart', {event: ev}).event().defaultPrevented) | ||
return | ||
|
||
this.off('touchstart', pinchZoomStart) | ||
|
||
zoomInProgress = true | ||
SVG.on(document, 'touchmove', pinchZoom, this, {passive:false}) | ||
SVG.on(document, 'touchend', pinchZoomStop, this, {passive:false}) | ||
} | ||
|
||
var pinchZoomStop = function(ev) { | ||
ev.preventDefault() | ||
zoomInProgress = false | ||
|
||
this.fire('pinchZoomEnd', {event: ev}) | ||
|
||
SVG.off(document,'touchmove', pinchZoom) | ||
SVG.off(document,'touchend', pinchZoomStop) | ||
this.on('touchstart', pinchZoomStart) | ||
} | ||
|
||
var pinchZoom = function(ev) { | ||
ev.preventDefault() | ||
|
||
var currentTouches = normalizeEvent(ev) | ||
|
||
// Distance Formula | ||
var lastDelta = Math.sqrt( | ||
Math.pow(lastTouches[0].clientX - lastTouches[1].clientX, 2) + | ||
Math.pow(lastTouches[0].clientY - lastTouches[1].clientY, 2) | ||
) | ||
|
||
var currentDelta = Math.sqrt( | ||
Math.pow(currentTouches[0].clientX - currentTouches[1].clientX, 2) + | ||
Math.pow(currentTouches[0].clientY - currentTouches[1].clientY, 2) | ||
) | ||
|
||
var zoomAmount = lastDelta/currentDelta | ||
|
||
var currentFocus = { | ||
x: currentTouches[0].clientX + 0.5 * (currentTouches[1].clientX - currentTouches[0].clientX), | ||
y: currentTouches[0].clientY + 0.5 * (currentTouches[1].clientY - currentTouches[0].clientY) | ||
} | ||
|
||
var lastFocus = { | ||
x: lastTouches[0].clientX + 0.5 * (lastTouches[1].clientX - lastTouches[0].clientX), | ||
y: lastTouches[0].clientY + 0.5 * (lastTouches[1].clientY - lastTouches[0].clientY) | ||
} | ||
|
||
var p = this.point(currentFocus.x, currentFocus.y) | ||
var focusP = this.point(2*currentFocus.x-lastFocus.x, 2*currentFocus.y-lastFocus.y) | ||
var box = new SVG.Box(this.viewbox()).transform( | ||
new SVG.Matrix() | ||
.translate(p.x, p.y) | ||
.scale(zoomAmount, 0, 0) | ||
.translate(-focusP.x, -focusP.y) | ||
) | ||
|
||
this.viewbox(box) | ||
|
||
lastTouches = currentTouches | ||
|
||
this.fire('zoom', {box: box, focus: focusP}) | ||
} | ||
|
||
var panStart = function(ev) { | ||
ev.preventDefault() | ||
|
||
this.off('mousedown', panStart) | ||
|
||
lastTouches = normalizeEvent(ev) | ||
|
||
if(zoomInProgress) return | ||
|
||
this.fire('panStart', {event: ev}) | ||
|
||
lastP = {x: lastTouches[0].clientX, y: lastTouches[0].clientY } | ||
|
||
SVG.on(document, 'mousemove', panning, this, {passive:false}) | ||
SVG.on(document, 'mouseup', panStop, this, {passive:false}) | ||
} | ||
|
||
var panStop = function(ev) { | ||
ev.preventDefault() | ||
|
||
this.fire('panEnd', {event: ev}) | ||
|
||
SVG.off(document,'mousemove', panning) | ||
SVG.off(document,'mouseup', panStop) | ||
this.on('mousedown', panStart) | ||
} | ||
|
||
var panning = function(ev) { | ||
ev.preventDefault() | ||
|
||
var currentTouches = normalizeEvent(ev) | ||
|
||
var currentP = {x: currentTouches[0].clientX, y: currentTouches[0].clientY } | ||
, p1 = this.point(currentP.x, currentP.y) | ||
, p2 = this.point(lastP.x, lastP.y) | ||
, deltaP = [p2.x - p1.x, p2.y - p1.y] | ||
, box = new SVG.Box(this.viewbox()).transform(new SVG.Matrix().translate(deltaP[0], deltaP[1])) | ||
|
||
this.viewbox(box) | ||
lastP = currentP | ||
} | ||
|
||
this.on('wheel', wheelZoom) | ||
this.on('touchstart', pinchZoomStart, this, {passive:false}) | ||
this.on('mousedown', panStart, this) | ||
|
||
return this | ||
|
||
}, | ||
|
||
zoom: function(level, point) { | ||
var style = window.getComputedStyle(this.node) | ||
, width = parseFloat(style.getPropertyValue('width')) | ||
, height = parseFloat(style.getPropertyValue('height')) | ||
, v = this.viewbox() | ||
, zoomX = width / v.width | ||
, zoomY = height / v.height | ||
, zoom = Math.min(zoomX, zoomY) | ||
|
||
if(level == null) { | ||
return zoom | ||
} | ||
|
||
var zoomAmount = (zoom / level) | ||
|
||
point = point || new SVG.Point(width/2 / zoomX + v.x, height/2 / zoomY + v.y) | ||
|
||
var box = new SVG.Box(v) | ||
.transform(new SVG.Matrix() | ||
.scale(zoomAmount, point.x, point.y) | ||
) | ||
|
||
if(this.fire('zoom', {box: box, focus: point}).event().defaultPrevented) | ||
return this | ||
|
||
return this.viewbox(box) | ||
} | ||
}) | ||
|
||
SVG.extend(SVG.FX, { | ||
zoom: function(level, point) { | ||
return this.add('zoom', new SVG.Number(level), point) | ||
} | ||
}) | ||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var del = require('del') | ||
, gulp = require('gulp') | ||
, header = require('gulp-header') | ||
, iife = require('gulp-iife') | ||
, rename = require('gulp-rename') | ||
, trim = require('gulp-trimlines') | ||
, uglify = require('gulp-uglify') | ||
, pkg = require('./package.json') | ||
|
||
|
||
var headerLong = ['/*!' | ||
, '* <%= pkg.name %> - <%= pkg.description %>' | ||
, '* @version <%= pkg.version %>' | ||
, '* <%= pkg.homepage %>' | ||
, '*' | ||
, '* @copyright <%= pkg.author %>' | ||
, '* @license <%= pkg.license %>' | ||
, '*/;' | ||
, ''].join('\n') | ||
|
||
var headerShort = '/*! <%= pkg.name %> v<%= pkg.version %> <%= pkg.license %>*/;' | ||
|
||
gulp.task('clean', function() { | ||
return del([ 'dist/*' ]) | ||
}) | ||
|
||
gulp.task('copy', ['clean'], function() { | ||
return gulp.src('src/svg.panzoom.js') | ||
.pipe(iife()) | ||
.pipe(header(headerLong, { pkg: pkg })) | ||
.pipe(trim({ leading: false })) | ||
.pipe(gulp.dest('dist')) | ||
}) | ||
|
||
/** | ||
* uglify the file | ||
* add the license info | ||
*/ | ||
gulp.task('minify', ['copy'], function() { | ||
return gulp.src('dist/svg.panzoom.js') | ||
.pipe(uglify()) | ||
.pipe(rename({ suffix:'.min' })) | ||
.pipe(header(headerShort, { pkg: pkg })) | ||
.pipe(gulp.dest('dist')) | ||
}) | ||
|
||
|
||
gulp.task('default', ['clean', 'copy', 'minify']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
zoom is available on all viewport elements and not only elements you called panzoom on