Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@marp-team/marpit-svg-polyfill

marp-team32kMIT2.1.0TypeScript support: included

Polyfill for inline SVG slides rendered by Marpit

readme

@marp-team/marpit-svg-polyfill

CircleCI Codecov npm LICENSE

The polyfill for the inline SVG slide rendered by Marpit.

Supported browser

  • WebKit based browser: Safari and iOS browsers (including iOS Chrome, iOS Firefox, iOS Edge, and so on...)

Usage

<!-- Generated HTML by Marpit's inline SVG mode -->
<div class="marpit">
  <svg viewBox="0 0 1280 720" data-marpit-svg="">
    <foreignObject width="1280" height="720">
      <section>...</section>
    </foreignObject>
  </svg>
</div>

<!-- After than, use polyfill through jsDelivr CDN -->
<script src="https://cdn.jsdelivr.net/npm/@marp-team/marpit-svg-polyfill/lib/polyfill.browser.js"></script>

Why need?

WebKit

Marpit's inline SVG slide has a lot of advantages: No requires JavaScript, gives better performance for scaling, and has predicatable DOM structure.

But unfortunately, WebKit browser has not scaled the wrapped HTML correctly. It is caused from a long standing bug 23113, and it does not resolved in the last 15 years.

Note A brand-new SVG engine for WebKit called as "Layer-based SVG engine (LBSE)" is currently under development, and it will finally bring glitch-free scaling without JS. (See also: Status of the new SVG engine in WebKit)

You can test LBSE in Safari Technology Preview by following these steps:

  1. Install Safari Technology Preview
  2. Run defaults write com.apple.SafariTechnologyPreview IncludeInternalDebugMenu 1 in terminal
  3. Open Safari Technology Preview
  4. Turn on "Layer-based SVG engine (LBSE)" from "Debug" menu → "WebKit Internal Features"
  5. Restart app

marpit-svg-polyfill v2.1.0 and later will try to detect whether or not enabled LBSE, and does not apply polyfill if LBSE was available.

Solutions

For Webkit

Scaling

We try to simulate scaling and centering by applying transform / transform-origin style to Marpit <section> elements.

<svg viewBox="0 0 1280 960">
  <foreignObject width="1280" height="960">
    <section style="transform-origin:0 0;transform:matrix(......);">
      ...
    </section>
  </foreignObject>
</svg>

marpit-svg-polyfill uses the result of getScreenCTM() method, so the polyfill will sacrifice "zero-JS slide", the key feature of inline SVG.

Repainting

WebKit browser would not trigger repainting even if modified the contents of slide. It becomes a problem when supporting the live preview feature in Marp tools.

Fortunately, a genius already resolved this problem only in CSS! transform:translateZ(0) would trigger re-painting immidiately when modified contents.

Animation GIF

People like to put GIF animation in the slide. However, GIF in polyfilled slides have glitches. GIF updates only a cropped part somewhere.

Applying transform:translateZ(0.0001px) to each <section> elements within SVG is a magic to resolve that. 🪄

Warning This style brings slightly blurred contents too. Our polyfill prefers to render animated contents correctly.

Contributing

We are following the contributing guideline of marp-team projects. Please read these guidelines this before starting work in this repository.

Author

Managed by @marp-team.

License

This module releases under the MIT License.

changelog

Change Log

[Unreleased]

v2.1.0 - 2023-02-10

Added

Changed

  • The polyfills() function now returns a thenable array, for getting the correct result of feature detection when calling together with await expression (#50)
  • Upgrade development Node and dependent packages to the latest version (#51)

v2.0.0 - 2022-04-12

Breaking

  • Required Node 10 and later to install (but recommend to use Node 14 and later) (#45)

Fixed

  • Make robust from element injection to <foreignObject> (#46)

Changed

  • Upgrade development Node and dependent packages to the latest version (#45)

v1.7.1 - 2021-02-07

Fixed

  • Fix Marpit's <section> detection to use firstElementChild instead of firstChild (#33)

Changed

  • Upgrade development Node LTS and dependent packages to the latest version (#34)
  • Rename master branch to main (#35)

v1.7.0 - 2020-08-18

Changed

  • Use transformation matrix generated from getScreenCTM() (#30)

v1.6.0 - 2020-08-16

Added

  • Allow to change the target of observer (#28)
  • Return clean-up function from observe() (#28)

Changed

  • Upgrade dependent packages to the latest version (#29)

v1.5.0 - 2020-07-16

Changed

  • Propagate zoom factor in the top window into <iframe> through postMessage (#24)
  • Migrate from TSLint to ESlint (#25)
  • Upgrade dependent packages to the latest version (#26)

v1.4.0 - 2020-07-09

Fixed

  • Follow zoom factor of the parent window in <iframe> (#21)

Changed

  • Upgrade development Node LTS and dependent packages to the latest version (#22)
  • Test against Node 14 (#23)

v1.3.0 - 2020-06-07

Fixed

  • Fix visually shift depending on zoom level (#18, #19)

Changed

  • Simplify transformation (#19)
  • Upgrade dependent packages to the latest version (#20)

v1.2.1 - 2020-04-18

Changed

  • Upgrade dependent packages to the latest version (#17)

v1.2.0 - 2020-01-12

Changed

  • Upgrade dependent packages to the latest version (#14)

Removed

  • CI test against EOL Node 8 (#14)

v1.1.1 - 2019-07-13

Changed

  • Upgrade dependent packages to the latest version (#12)

v1.1.0 - 2019-06-20

Changed

  • Simplify WebKit detection (JS build for browser become about 5x smaller) (#9)
  • Upgrade dependent packages to the latest version (#10)

v1.0.0 - 2019-06-05

Breaking

  • Drop support of Node 6.

Changed

  • Upgrade dependent packages to the latest version (#7)
  • Automate GitHub release (#8)

v0.3.0 - 2019-03-20

Fixed

  • Incorrect scaling with custom zoom level (#3, #4)

Changed

  • Upgrade dependent packages to the latest (#5)

v0.2.0 - 2018-12-31

Fixed

  • Fix incorrect scaling in split background (#2)

v0.1.0 - 2018-12-29

Changed

  • Split picking out logics into polyfills() function (#1)

v0.0.1 - 2018-12-29

  • Initial release.