Change padding into a percentage

This commit is contained in:
linguist
2021-01-02 13:05:07 +01:00
parent a13b903122
commit f55176f7b9
3 changed files with 10 additions and 6 deletions

View File

@@ -84,7 +84,7 @@
console.debug(`metrics/compute/${login} > render`)
let rendered = await ejs.render(image, {...data, s, style, fonts}, {async:true})
//Apply resizing
const {resized, mime} = await svgresize(rendered, {padding:"config.padding" in q ? Number(q["config.padding"]) : undefined, convert})
const {resized, mime} = await svgresize(rendered, {padding:q["config.padding"], convert})
rendered = resized
//Additional SVG transformations
@@ -180,12 +180,16 @@
}
/** Render svg */
async function svgresize(svg, {padding = 20, convert} = {}) {
async function svgresize(svg, {padding = "2%", convert} = {}) {
//Instantiate browser if needed
if (!svgresize.browser) {
svgresize.browser = await puppeteer.launch({headless:true, executablePath:process.env.PUPPETEER_BROWSER_PATH, args:["--no-sandbox", "--disable-extensions", "--disable-setuid-sandbox", "--disable-dev-shm-usage"]})
console.debug(`metrics/svgresize > started ${await svgresize.browser.version()}`)
}
//Format padding
padding = ((Number(`${padding}`.substring(0, padding.length-1))||0)/100)
console.debug(`metrics/svgresize > padding ${(100*padding).toFixed(2)}%`)
padding += 1
//Render through browser and resize height
const page = await svgresize.browser.newPage()
await page.setContent(svg, {waitUntil:"load"})
@@ -197,7 +201,7 @@
document.querySelector("svg").classList.add("no-animations")
//Get bounds and resize
let {y:height, width} = document.querySelector("svg #metrics-end").getBoundingClientRect()
height = Math.ceil(height + padding)
height = Math.ceil(height*padding)
width = Math.ceil(width)
//Resize svg
document.querySelector("svg").setAttribute("height", height)