Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rotate options in javascript #169

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,12 @@ When you call `svgPanZoom` method it returns an object with following methods:
* zoomIn
* zoomOut
* getZoom
* rotate
* rotateRelative
* getRotate
* resetZoom
* resetPan
* resetRotate
* reset
* fit
* contain
Expand Down Expand Up @@ -427,6 +431,7 @@ If you need more data about SVG you can call `getSizes`. It will return an objec
* `height`
* `x` - x offset
* `y` - y offset
* `rotate` - rotation angle in degrees

Destroy SvgPanZoom instance:

Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svg-pan-zoom",
"version": "3.2.5",
"version": "3.2.6",
"homepage": "https://github.com/ariutta/svg-pan-zoom",
"authors": [
"Andrea Leofreddi <[email protected]>",
Expand Down Expand Up @@ -28,7 +28,7 @@
"pan",
"zoom"
],
"license": "BSD",
"license": "BSD-2-Clause",
"ignore": [
"**/.*",
"node_modules",
Expand Down
95 changes: 84 additions & 11 deletions dist/svg-pan-zoom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// svg-pan-zoom v3.2.5
// https://github.com/ariutta/svg-pan-zoom
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var svgPanZoom = require('./svg-pan-zoom.js');

Expand Down Expand Up @@ -165,8 +163,8 @@ ShadowViewport.prototype.init = function(viewport, options) {
this.options = options

// State cache
this.originalState = {zoom: 1, x: 0, y: 0}
this.activeState = {zoom: 1, x: 0, y: 0}
this.originalState = {zoom: 1, x: 0, y: 0, rotate: 0}
this.activeState = {zoom: 1, x: 0, y: 0, rotate: 0}

this.updateCTMCached = Utils.proxy(this.updateCTM, this)

Expand Down Expand Up @@ -338,6 +336,38 @@ ShadowViewport.prototype.getPan = function() {
return {x: this.activeState.x, y: this.activeState.y}
}

/**
* Get rotate
*
* @return {Float} angle
*/
ShadowViewport.prototype.getRotate = function() {
return this.activeState.rotate
}

/**
* Get rotate transformation
*
* @return {Object} angle and point of rotation
*/
ShadowViewport.prototype.getRotateTransform = function() {
return {
angle: this.getRotate(),
x: this.getViewBox().width / 2,
y: this.getViewBox().height / 2
}
}

/**
* Set rotate
*
* @param {Float} angle
*/
ShadowViewport.prototype.rotate = function(angle) {
this.activeState.rotate = angle;
this.updateCTMOnNextFrame()
}

/**
* Return cached viewport CTM value that can be safely modified
*
Expand All @@ -347,12 +377,12 @@ ShadowViewport.prototype.getCTM = function() {
var safeCTM = this.options.svg.createSVGMatrix()

// Copy values manually as in FF they are not itterable
safeCTM.a = this.activeState.zoom
safeCTM.a = this.activeState.zoom.toFixed(6)
safeCTM.b = 0
safeCTM.c = 0
safeCTM.d = this.activeState.zoom
safeCTM.e = this.activeState.x
safeCTM.f = this.activeState.y
safeCTM.d = this.activeState.zoom.toFixed(6)
safeCTM.e = this.activeState.x.toFixed(6)
safeCTM.f = this.activeState.y.toFixed(6)

return safeCTM
}
Expand Down Expand Up @@ -471,7 +501,7 @@ ShadowViewport.prototype.updateCTMOnNextFrame = function() {
*/
ShadowViewport.prototype.updateCTM = function() {
// Updates SVG element
SvgUtils.setCTM(this.viewport, this.getCTM(), this.defs)
SvgUtils.setCTM(this.viewport, this.getCTM(), this.defs, this.getRotateTransform())

// Free the lock
this.pendingUpdate = false
Expand Down Expand Up @@ -807,6 +837,32 @@ SvgPanZoom.prototype.publicZoomAtPoint = function(scale, point, absolute) {
this.zoomAtPoint(scale, point, absolute)
}

/**
* Rotate
*
* @param {Float} angle
*/
SvgPanZoom.prototype.rotate = function(angle) {
this.viewport.rotate(angle)
}

/**
* Rotate relative
*
* @param {Float} relative angle
*/
SvgPanZoom.prototype.rotateRelative = function(angle) {
this.rotate(this.getRotate() + angle)
}

/**
* Get rotate for public usage
*
* @return {Float} rotate
*/
SvgPanZoom.prototype.getRotate = function() {
return this.viewport.getRotate()
}
/**
* Get zoom scale
*
Expand Down Expand Up @@ -851,12 +907,20 @@ SvgPanZoom.prototype.resetPan = function() {
this.pan(this.viewport.getOriginalState());
}

/**
* Set pan to initial state
*/
SvgPanZoom.prototype.resetRotate = function() {
this.rotate(this.viewport.getOriginalState().rotate);
}

/**
* Set pan and zoom to initial state
*/
SvgPanZoom.prototype.reset = function() {
this.resetZoom()
this.resetPan()
this.resetRotate()
}

/**
Expand Down Expand Up @@ -1172,9 +1236,14 @@ SvgPanZoom.prototype.getPublicInstance = function() {
, zoomIn: function() {this.zoomBy(1 + that.options.zoomScaleSensitivity); return that.pi}
, zoomOut: function() {this.zoomBy(1 / (1 + that.options.zoomScaleSensitivity)); return that.pi}
, getZoom: function() {return that.getRelativeZoom()}
// Rotate
, rotate: function(angle) {that.rotate(angle); return that.pi}
, rotateRelative: function(angle) {that.rotateRelative(angle); return that.pi}
, getRotate: function() {return that.getRotate()}
// Reset
, resetZoom: function() {that.resetZoom(); return that.pi}
, resetPan: function() {that.resetPan(); return that.pi}
, resetRotate: function() {that.resetPan(); return that.pi}
, reset: function() {that.reset(); return that.pi}
// Fit, Contain and Center
, fit: function() {that.fit(); return that.pi}
Expand All @@ -1189,6 +1258,7 @@ SvgPanZoom.prototype.getPublicInstance = function() {
, height: that.height
, realZoom: that.getZoom()
, viewBox: that.viewport.getViewBox()
, rotate: that.getRotate()
}
}
// Destroy
Expand Down Expand Up @@ -1379,11 +1449,14 @@ module.exports = {
* @param {SVGElement} element
* @param {SVGMatrix} matrix CTM
* @param {SVGElement} defs
* @param {Object} rotate
*/
, setCTM: function(element, matrix, defs) {
, setCTM: function(element, matrix, defs, rotate) {
var that = this
, s = 'matrix(' + matrix.a + ',' + matrix.b + ',' + matrix.c + ',' + matrix.d + ',' + matrix.e + ',' + matrix.f + ')';

if (rotate.angle !== 0) {
s += ' rotate(' + rotate.angle + ',' + rotate.x + ',' + rotate.y + ')';
}
element.setAttributeNS(null, 'transform', s);

// IE has a bug that makes markers disappear on zoom (when the matrix "a" and/or "d" elements change)
Expand Down
4 changes: 1 addition & 3 deletions dist/svg-pan-zoom.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var gulp = require('gulp')
, sync = require('gulp-config-sync')
, header = require('gulp-header')
, pkg = require('./package.json')
, banner = "// svg-pan-zoom v<%= pkg.version %>" + "\n" + "// https://github.com/ariutta/svg-pan-zoom" + "\n"
, banner = "svg-pan-zoom v<%= pkg.version %>" + "\n" + "// https://github.com/ariutta/svg-pan-zoom" + "\n"
;

/**
Expand All @@ -28,11 +28,11 @@ gulp.task('browserify', function() {
this.emit("end")
})
.pipe(source('svg-pan-zoom.js'))
.pipe(header(banner, {pkg: pkg}))
//.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('./dist/'))
.pipe(streamify(rename('svg-pan-zoom.min.js')))
.pipe(streamify(uglify()))
.pipe(header(banner, {pkg: pkg}))
//.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('./dist/'))
});

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "svg-pan-zoom",
"version": "3.2.5",
"version": "3.2.6",
"main": "dist/svg-pan-zoom.js",
"browser": "src/browserify.js",
"license": "BSD",
"license": "BSD-2-Clause",
"description": "JavaScript library for panning and zooming an SVG image from the mouse, touches and programmatically.",
"repository": {
"type": "git",
Expand Down
46 changes: 39 additions & 7 deletions src/shadow-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ ShadowViewport.prototype.init = function(viewport, options) {
this.options = options

// State cache
this.originalState = {zoom: 1, x: 0, y: 0}
this.activeState = {zoom: 1, x: 0, y: 0}
this.originalState = {zoom: 1, x: 0, y: 0, rotate: 0}
this.activeState = {zoom: 1, x: 0, y: 0, rotate: 0}

this.updateCTMCached = Utils.proxy(this.updateCTM, this)

Expand Down Expand Up @@ -191,6 +191,38 @@ ShadowViewport.prototype.getPan = function() {
return {x: this.activeState.x, y: this.activeState.y}
}

/**
* Get rotate
*
* @return {Float} angle
*/
ShadowViewport.prototype.getRotate = function() {
return this.activeState.rotate
}

/**
* Get rotate transformation
*
* @return {Object} angle and point of rotation
*/
ShadowViewport.prototype.getRotateTransform = function() {
return {
angle: this.getRotate(),
x: this.getViewBox().width / 2,
y: this.getViewBox().height / 2
}
}

/**
* Set rotate
*
* @param {Float} angle
*/
ShadowViewport.prototype.rotate = function(angle) {
this.activeState.rotate = angle;
this.updateCTMOnNextFrame()
}

/**
* Return cached viewport CTM value that can be safely modified
*
Expand All @@ -200,12 +232,12 @@ ShadowViewport.prototype.getCTM = function() {
var safeCTM = this.options.svg.createSVGMatrix()

// Copy values manually as in FF they are not itterable
safeCTM.a = this.activeState.zoom
safeCTM.a = this.activeState.zoom.toFixed(6)
safeCTM.b = 0
safeCTM.c = 0
safeCTM.d = this.activeState.zoom
safeCTM.e = this.activeState.x
safeCTM.f = this.activeState.y
safeCTM.d = this.activeState.zoom.toFixed(6)
safeCTM.e = this.activeState.x.toFixed(6)
safeCTM.f = this.activeState.y.toFixed(6)

return safeCTM
}
Expand Down Expand Up @@ -324,7 +356,7 @@ ShadowViewport.prototype.updateCTMOnNextFrame = function() {
*/
ShadowViewport.prototype.updateCTM = function() {
// Updates SVG element
SvgUtils.setCTM(this.viewport, this.getCTM(), this.defs)
SvgUtils.setCTM(this.viewport, this.getCTM(), this.defs, this.getRotateTransform())

// Free the lock
this.pendingUpdate = false
Expand Down
Loading