Skip to content

Commit

Permalink
Use zoom while calculating border limit
Browse files Browse the repository at this point in the history
I guess it would break backwards-compatibility if user has already fixed zoom himself somehow :) resolves michaschwab#16, resolves michaschwab#12.
  • Loading branch information
sliterok authored Oct 29, 2020
1 parent b147097 commit e7f2255
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions easypz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,29 +455,35 @@ class EasyPZ

private ensureTransformWithinBounds(transformBeforeScale)
{
if(this.options.bounds)
{
let scale = transformBeforeScale ? this.totalTransform.scale - 1 : 1 - 1 / this.totalTransform.scale;
let scaleTopLeft = -1 * Math.max(scale, 0);
let scaleBotRight = -1 * Math.min(scale, 0);

if(this.totalTransform.translateX < scaleTopLeft * this.width + this.options.bounds.left)
{
this.totalTransform.translateX = scaleTopLeft * this.width + this.options.bounds.left;
}
if(this.totalTransform.translateX > scaleBotRight * this.width + this.options.bounds.right)
{
this.totalTransform.translateX = scaleBotRight * this.width + this.options.bounds.right;
}
if(this.totalTransform.translateY < scaleTopLeft * this.height + this.options.bounds.top)
{
this.totalTransform.translateY = scaleTopLeft * this.height + this.options.bounds.top;
}
if(this.totalTransform.translateY > scaleBotRight * this.height + this.options.bounds.bottom)
{
this.totalTransform.translateY = scaleBotRight * this.height + this.options.bounds.bottom;
}
}
if(!this.options.bounds)
return;

let x = this.totalTransform.translateX,
y = this.totalTransform.translateY

const boundLeft = this.options.bounds.left * this.totalTransform.scale
const boundRight = this.options.bounds.right * this.totalTransform.scale
const boundTop = this.options.bounds.top * this.totalTransform.scale
const boundBottom = this.options.bounds.bottom * this.totalTransform.scale

const borderX = [boundLeft, this.width - boundRight]
const borderLeft = Math.min(...borderX)
const borderRight = Math.max(...borderX)
if(x < borderLeft)
x = borderLeft
else if(x > borderRight)
x = borderRight

const borderY = [boundTop, this.height - boundBottom]
const borderTop = Math.min(...borderY)
const borderBottom = Math.max(...borderY)
if(y < borderTop)
y = borderTop
else if(y > borderBottom)
y = borderBottom

this.totalTransform.translateX = x
this.totalTransform.translateY = y
}

private lastAppliedTransform = { translateX: 0, translateY: 0, scale: 1 };
Expand Down

0 comments on commit e7f2255

Please sign in to comment.