Skip to content

Commit

Permalink
Fix inverted contianment for when the panzoom element is larger than …
Browse files Browse the repository at this point in the history
…its container. Supplements gh-11.
  • Loading branch information
timmywil committed Aug 12, 2013
1 parent 2167cca commit 029b6ba
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.panzoom",
"version": "1.4.0",
"version": "1.4.1",
"main": "dist/jquery.panzoom.js",
"ignore": [
"**/.*",
Expand Down
10 changes: 4 additions & 6 deletions dist/jquery.panzoom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license jquery.panzoom.js v1.4.0
* Updated: Thu Jul 25 2013
* @license jquery.panzoom.js v1.4.1
* Updated: Mon Aug 12 2013
* Add pan and zoom functionality to any element
* Copyright (c) 2013 timmy willison
* Released under the MIT license
Expand Down Expand Up @@ -141,8 +141,6 @@

// Save the instance
$.data( elem, datakey, this );

return this;
};

// Attach regex for possible use (immutable)
Expand Down Expand Up @@ -324,12 +322,12 @@
isInvert = contain === 'invert';
container = this.container;
dims = this.dimensions;
margin = ((dims.width * scale) - container.width) / 2;
margin = ((dims.width * scale) - container.width) / 2 + ((container.width - dims.width) / 2);
matrix[4] = Math[ isInvert ? 'max' : 'min' ](
Math[ isInvert ? 'min' : 'max' ]( matrix[4], margin - dims.left ),
-margin - dims.left
);
margin = ((dims.height * scale) - container.height) / 2;
margin = ((dims.height * scale) - container.height) / 2 + ((container.height - dims.height) / 2);
matrix[5] = Math[ isInvert ? 'max' : 'min' ](
Math[ isInvert ? 'min' : 'max' ]( matrix[5], margin - dims.top ),
-margin - dims.top
Expand Down
6 changes: 3 additions & 3 deletions dist/jquery.panzoom.min.js

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions jquery.panzoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@

// Save the instance
$.data( elem, datakey, this );

return this;
};

// Attach regex for possible use (immutable)
Expand Down Expand Up @@ -324,12 +322,12 @@
isInvert = contain === 'invert';
container = this.container;
dims = this.dimensions;
margin = ((dims.width * scale) - container.width) / 2;
margin = ((dims.width * scale) - container.width) / 2 + ((container.width - dims.width) / 2);
matrix[4] = Math[ isInvert ? 'max' : 'min' ](
Math[ isInvert ? 'min' : 'max' ]( matrix[4], margin - dims.left ),
-margin - dims.left
);
margin = ((dims.height * scale) - container.height) / 2;
margin = ((dims.height * scale) - container.height) / 2 + ((container.height - dims.height) / 2);
matrix[5] = Math[ isInvert ? 'max' : 'min' ](
Math[ isInvert ? 'min' : 'max' ]( matrix[5], margin - dims.top ),
-margin - dims.top
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.panzoom",
"version": "1.4.0",
"version": "1.4.1",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.1",
Expand Down
2 changes: 1 addition & 1 deletion panzoom.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"zoom",
"panzoom"
],
"version": "1.4.0",
"version": "1.4.1",
"author": {
"name": "Timmy Willison",
"email": "[email protected]",
Expand Down
51 changes: 32 additions & 19 deletions test/bdd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ describe('Panzoom', function() {
panzoom.setMatrix( origMatrix );
}

/**
* Test the invert containment option
*/
function testInvert() {
var panzoom = $elem.panzoom('instance');
// Zoom in for moving
$elem.panzoom('zoom', { animate: false });
// Set contain to 'invert'
$elem.panzoom('option', 'contain', 'invert');
fauxMove( -2, -2 );
var matrix = panzoom.getMatrix();
expect( +matrix[4] ).to.equal( -2 );
expect( +matrix[5] ).to.equal( -2 );
fauxMove( 2, 2 );
matrix = panzoom.getMatrix();
// Should normalize to 0
expect( +matrix[4] ).to.equal( 0 );
expect( +matrix[5] ).to.equal( 0 );
// Clean up
$elem.panzoom('option', 'contain', false).panzoom( 'reset', false );
matrix = panzoom.getMatrix();
expect( +matrix[4] ).to.equal( 0 );
expect( +matrix[5] ).to.equal( 0 );
}

/* panzoom creation and options
---------------------------------------------------------------------- */
it('should have elements available', function() {
Expand Down Expand Up @@ -185,25 +210,13 @@ describe('Panzoom', function() {
$elem.panzoom('option', 'contain', false).panzoom( 'reset', false );
});
it('should invert-contain the panzoom element outside its parent when the contain option is set to "invert"', function() {
var panzoom = $elem.panzoom('instance');
// Zoom in for moving
$elem.panzoom('zoom', { animate: false });
// Set contain to 'invert'
$elem.panzoom('option', 'contain', 'invert');
fauxMove( -2, -2 );
var matrix = panzoom.getMatrix();
expect( +matrix[4] ).to.equal( -2 );
expect( +matrix[5] ).to.equal( -2 );
fauxMove( 2, 2 );
matrix = panzoom.getMatrix();
// Should normalize to 0
expect( +matrix[4] ).to.equal( 0 );
expect( +matrix[5] ).to.equal( 0 );
// Clean up
$elem.panzoom('option', 'contain', false).panzoom( 'reset', false );
matrix = panzoom.getMatrix();
expect( +matrix[4] ).to.equal( 0 );
expect( +matrix[5] ).to.equal( 0 );
testInvert();
});
it('should invert-contain the panzoom element when the panzoom element is larger than the container', function() {
var width = $elem.width();
$elem.width( width * 2 );
testInvert();
$elem.width( width );
});


Expand Down

0 comments on commit 029b6ba

Please sign in to comment.