Created
May 23, 2019 18:07
-
-
Save vbarbarosh/4d0f089d6580d98db0110c662703802a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| const path = require('path'); | |
| const puppeteer = require('puppeteer'); | |
| // Get bounding box of an svg | |
| // | |
| // $ node app_svgbbox.js a.svg | |
| // 2.74 -61.85 180.86 62.43 | |
| main().catch(panic); | |
| async function main() | |
| { | |
| const browser = await puppeteer.launch(); | |
| try { | |
| const page = await browser.newPage(); | |
| await page.goto(`file://${path.resolve(__dirname, process.argv[2])}`); | |
| const bbox = await page.evaluate(function () { | |
| const r = document.querySelector('svg').getBBox(); | |
| return [r.x, r.y, r.width, r.height].map(format).join(' '); | |
| function format(num) { | |
| return num.toFixed(4).replace(/0+$/, '').replace(/\.$/, ''); | |
| } | |
| }); | |
| console.log(bbox); | |
| } | |
| finally { | |
| await browser.close(); | |
| } | |
| } | |
| function panic(error) | |
| { | |
| console.error(error); | |
| process.exit(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment