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

Simplify transformation and fix visually shift depending on zoom level #19

Merged
merged 4 commits into from
Jun 7, 2020
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

## [Unreleased]

### Fixed

- Fix visually shift depending on zoom level ([#18](https://github.com/marp-team/marpit-svg-polyfill/issues/18), [#19](https://github.com/marp-team/marpit-svg-polyfill/pull/19))

### Changed

- Simplify transformation ([#19](https://github.com/marp-team/marpit-svg-polyfill/pull/19))
- Upgrade dependent packages to the latest version ([#20](https://github.com/marp-team/marpit-svg-polyfill/pull/20))

## v1.2.1 - 2020-04-18
Expand Down
44 changes: 16 additions & 28 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,29 @@ export const polyfills = () =>
navigator.vendor === 'Apple Computer, Inc.' ? [webkit] : []

export function webkit(zoom?: number) {
Array.from(document.getElementsByTagName('svg'), (svg) => {
if (svg.hasAttribute('data-marpit-svg')) {
const { clientHeight, clientWidth } = svg
Array.from(
document.querySelectorAll<SVGSVGElement>('svg[data-marpit-svg]'),
(svg) => {
const { children, clientHeight, clientWidth, viewBox } = svg
if (!svg.style.transform) svg.style.transform = 'translateZ(0)'

// NOTE: Safari reflects a zoom level to SVG's currentScale property, but
// the other browsers will always return 1. You have to specify the zoom
// factor manually if it is used in Blink engine. (e.g. Electron)
// factor manually if it is used in outdated Blink engine. (e.g. Electron)
const zoomFactor = zoom || svg.currentScale || 1

const width = svg.viewBox.baseVal.width / zoomFactor
const height = svg.viewBox.baseVal.height / zoomFactor
const width = viewBox.baseVal.width / zoomFactor
const height = viewBox.baseVal.height / zoomFactor
const scale = Math.min(clientHeight / height, clientWidth / width)

Array.from(
svg.querySelectorAll<SVGForeignObjectElement>(':scope > foreignObject'),
(foreignObject) => {
const x = foreignObject.x.baseVal.value
const y = foreignObject.y.baseVal.value

Array.from(
foreignObject.querySelectorAll<HTMLElement>(':scope > section'),
(section) => {
if (!section.style.transformOrigin) {
section.style.transformOrigin = '0 0'
}

const tx = (clientWidth - scale * width) / 2 - x
const ty = (clientHeight - scale * height) / 2 - y

section.style.transform = `translate3d(${tx}px,${ty}px,0) scale(${scale}) translate(${x}px,${y}px)`
}
)
}
)
for (let i = 0; i < children.length; i += 1) {
const { style } = children[i].firstChild as HTMLElement

if (!style.position) style.position = 'fixed'
if (!style.transformOrigin) style.transformOrigin = '0 0'

style.transform = `scale(${scale}) translateZ(0)`
}
}
})
)
}
2 changes: 0 additions & 2 deletions test/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('Marpit SVG polyfill', () => {
const { transform, transformOrigin } = section.style

expect(transformOrigin).toBeFalsy()
expect(transform).not.toContain('translate3d')
expect(transform).not.toContain('scale')
})

Expand All @@ -88,7 +87,6 @@ describe('Marpit SVG polyfill', () => {
const { transform, transformOrigin } = section.style

expect(transformOrigin).toBe('0 0')
expect(transform).toContain('translate3d')
expect(transform).toContain('scale')
})
})
Expand Down