forked from thx/resvg-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbox.js
37 lines (29 loc) · 973 Bytes
/
bbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { promises } = require('fs')
const { join } = require('path')
const { performance } = require('perf_hooks')
const { Resvg } = require('../index')
async function main() {
const svg = await promises.readFile(join(__dirname, './bbox.svg'))
const opts = {
fitTo: {
mode: 'width',
value: 500,
},
font: {
loadSystemFonts: false,
},
}
const t = performance.now()
const resvg = new Resvg(svg, opts)
console.info('Original SVG Size:', `${resvg.width} x ${resvg.height}`)
// const bbox = resvg.innerBBox()
const bbox = resvg.getBBox()
if (bbox) resvg.cropByBBox(bbox)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('SVG BBox:', `${bbox.width} x ${bbox.height}`)
console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`)
console.info('✨ Done in', performance.now() - t, 'ms')
await promises.writeFile(join(__dirname, './bbox-out.png'), pngBuffer)
}
main()