@resvg/resvg-js
Advanced tools
Changelog
[2.1.0] - 2022-07-03
Add imagesToResolve()
and resolveImage()
APIs to load image URL. By @zimond in #102
xlink:href
starting with http://
or https://
.In order to support loading image URL, we forked the rust side of resvg and made some improvements.
Now please witness the magic moment:
Add innerBBox()
API. By @yisibl in #105
Calculate a maximum bounding box of all visible elements in this SVG. (Note: path bounding box are approx values).
Add getBBox()
API. By @yisibl in #108
We designed it to correspond to the SVGGraphicsElement.getBBox()
method in the browser.
This is different from the innerBBox()
API, by default it does apply transform calculations and gets the BBox with curves exactly. This works well in most use cases, the only drawback is that it does not calculate BBoxes with stroke correctly.
Add cropByBBox()
API. By @yisibl in #108
With this API, we can crop the generated bitmap based on the BBox(bounding box).
<img width="550" alt="cropByBBox Demo" src="https://user-images.githubusercontent.com/2784308/177039185-5c1a8014-9e44-4c18-aae2-8f509163da56.gif">const fs = require('fs')
const { Resvg } = require('@resvg/resvg-js')
const svg = '' // some SVG string or file.
const resvg = new Resvg(svg)
const innerBBox = resvg.innerBBox()
const bbox = resvg.getBBox()
// Crop the bitmap according to bbox,
// The argument to the `.cropByBBox()` method accepts `bbox` or `innerBBox`.
if (bbox) resvg.cropByBBox(bbox)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('SVG BBox: ', `${bbox.width} x ${bbox.height}`)
fs.writeFileSync('out.png', pngBuffer)
feat: upgrade svgtypes to 0.8.1 to support 4 digits and 8 digits hex colors. By @yisibl in #127
Changelog
[2.0.0] - 2022-04-30
resvg-js now supports WebAssembly. 🎉
With WebAssembly, resvg-js gains broader cross-platform compatibility, all by loading only about 1.2MB of Wasm files. And, the API is consistent with the Node.js side.
The current version of Wasm does not support loading fonts, so please submit an issue if you have a request.
Changelog
[2.0.0-beta.0] - 2022-04-02
The resvg-js API is now largely stable.
feat: render()
result as a new class
render()
now no longer returns the Buffer
directly, you need to get the buffer via the new render().asPng()
, also added render().width
and render().height
to return the size of the PNG.
Changelog
[2.0.0-alpha.6] - 2022-03-20
chore: add libc fields on linux platform packages
On Linux, it is not possible to tell exactly what kind of C library a native modules depends on just by os/cpu, so yarn 3.2 and cnpm added libc fields to further distinguish this case. This avoids downloading both gnu
and musl
packages at the same time.
Currently only yarn 3.2+ and cnpm are supported, the npm implementation is still under discussion.
Changelog
[2.0.0-alpha.5] - 2022-03-19
feat: rounding width / height, this will further improve #61.
Physical pixels cannot be decimal, and when SVG dimensions have decimals in them, the output PNG dimensions are rounded. This should be consistent when getting the PNG size through the width / height API.
Changelog
[2.0.0-alpha.4] - 2022-03-14
Changelog
[2.0.0-alpha.3] - 2022-03-01
.width
and .height
to get the original size of the SVG.toString()
to convert SVG shapes or text to path.refactor: change to Class API, now instead use new Resvg()
to create the constructor.
With the new Class API, we can avoid rendering the SVG again when getting the SVG size. It also makes it easier to extend new APIs.
const { render } = require('@resvg/resvg-js')
const svg = '' // svg buffer or string
const pngData = render(svg, opts)
const { Resvg } = require('@resvg/resvg-js')
const svg = '' // svg buffer or string
const resvg = new Resvg(svg, opts)
const pngData = resvg.render()
// New!
console.info('Simplified svg string \n', resvg.toString())
console.info('SVG original size:', `${resvg.width} x ${resvg.height}px`)
Changelog
[2.0.0-alpha.2] - 2022-02-21
feat: upgrade resvg to 0.22.0
Support svg
referenced by <use>
, this is often used in svg sprite.
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<svg id="svg1" xmlns="http://www.w3.org/2000/svg">
<rect width="50%" height="50%" fill="green" />
</svg>
<use id="use1" x="50%" y="50%" xlink:href="#svg1" />
</svg>
feat: upgrade fontdb to 0.9.0, loading system fonts will become faster.
There are many CJK fonts installed in my local OS, the test result is 2.5 times faster:
Loaded 1085 font faces in 860.447ms.
Loaded 1085 font faces in 339.665ms.
Changelog
[2.0.0-alpha.1] - 2022-02-17
feat: playground uses unpkg.com's online resources
<script src="https://unpkg.com/@resvg/resvg-wasm@2.0.0-alpha.2/index.min.js"></script>
feat: preload wasm in the playground
<link rel="preload" as="fetch" type="application/wasm" href="https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm" />
chore: upload wasm file to artifacts
Switching from local to building in CI (Linux-x64-gnu) for .wasm
files can reduce the file size a bit.
After gzip compression, the .wasm
file is only about 700kB.