Skip to content

Commit

Permalink
feat(panzoom): add pan option - roundToPixels (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
avgeeklucky authored Dec 20, 2021
1 parent 71f3b06 commit 41498a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export function setTransform(
{ x, y, scale, isSVG }: CurrentValues,
_options?: PanzoomOptions
) {
if (_options !== undefined && _options.roundToPixels) {
x = Math.round(x)
y = Math.round(y)
}
setStyle(elem, 'transform', `scale(${scale}) translate(${x}px, ${y}px)`)
if (isSVG && isIE) {
const matrixValue = window.getComputedStyle(elem).getPropertyValue('transform')
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export interface PanOnlyOptions {
relative?: boolean
/** Disable panning while the scale is equal to the starting value */
panOnlyWhenZoomed?: boolean
/** Round transform-translate x and values to whole pixels. (Prevents images getting blurry) */
roundToPixels?: boolean
}

export interface ZoomOnlyOptions {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,15 @@ describe('css', () => {
setTransform(elem, { x: 1, y: 1, scale: 1 })
assertStyle(elem, 'transform', 'scale(1) translate(1px, 1px)')
})
it('sets the default transform-origin property with roundToPixels = true for HTML', () => {
const elem = document.createElement('div')
setTransform(elem, { x: 1.25, y: 1.75, scale: 1 }, { roundToPixels: true })
assertStyle(elem, 'transform', 'scale(1) translate(1px, 2px)')
})
it('sets the default transform-origin property with roundToPixels = true for SCG', () => {
const elem = document.createElementNS('http://www.w3.org/2000/svg', 'g')
setTransform(elem, { x: 1.25, y: 1.75, scale: 1 }, { roundToPixels: true })
assertStyle(elem, 'transform', 'scale(1) translate(1px, 2px)')
})
})
})

0 comments on commit 41498a7

Please sign in to comment.