Add additional log message for svg resizer

This commit is contained in:
lowlighter
2021-03-03 16:08:19 +01:00
parent 20fcbd68c5
commit ab06631d4e

View File

@@ -155,28 +155,43 @@
const padding = {width:pw, height:ph ?? pw} const padding = {width:pw, height:ph ?? pw}
console.debug(`metrics/svg/resize > padding width*${padding.width}, height*${padding.height}`) console.debug(`metrics/svg/resize > padding width*${padding.width}, height*${padding.height}`)
//Render through browser and resize height //Render through browser and resize height
console.debug(`metrics/svg/resize > loading svg`)
const page = await svg.resize.browser.newPage() const page = await svg.resize.browser.newPage()
page.on("console", ({_text:text}) => console.debug(`metrics/svg/resize > puppeteer > ${text}`))
await page.setContent(rendered, {waitUntil:["load", "domcontentloaded", "networkidle2"]}) await page.setContent(rendered, {waitUntil:["load", "domcontentloaded", "networkidle2"]})
console.debug(`metrics/svg/resize > loaded svg successfully`)
await page.addStyleTag({content:"body { margin: 0; padding: 0; }"}) await page.addStyleTag({content:"body { margin: 0; padding: 0; }"})
await wait(1)
let mime = "image/svg+xml" let mime = "image/svg+xml"
let {resized, width, height} = await page.evaluate(async padding => { console.debug(`metrics/svg/resize > resizing svg`)
//Disable animations let resized, width, height
const animated = !document.querySelector("svg").classList.contains("no-animations") try {
if (animated) ({resized, width, height} = await page.evaluate(async padding => {
document.querySelector("svg").classList.add("no-animations") //Disable animations
//Get bounds and resize const animated = !document.querySelector("svg").classList.contains("no-animations")
let {y:height, width} = document.querySelector("svg #metrics-end").getBoundingClientRect() if (animated)
height = Math.ceil(height*padding.height) document.querySelector("svg").classList.add("no-animations")
width = Math.ceil(width*padding.width) console.debug(`animations are ${animated ? "enabled" : "disabled"}`)
//Resize svg await new Promise(solve => setTimeout(solve, 2400))
document.querySelector("svg").setAttribute("height", height) //Get bounds and resize
//Enable animations let {y:height, width} = document.querySelector("svg #metrics-end").getBoundingClientRect()
if (animated) console.debug(`bounds width=${width}, height=${height}`)
document.querySelector("svg").classList.remove("no-animations") height = Math.ceil(height*padding.height)
//Result width = Math.ceil(width*padding.width)
return {resized:new XMLSerializer().serializeToString(document.querySelector("svg")), height, width} console.debug(`bounds after applying padding width=${width} (*${padding.width}), height=${height} (*${padding.height})`)
}, padding) //Resize svg
document.querySelector("svg").setAttribute("height", height)
//Enable animations
if (animated)
document.querySelector("svg").classList.remove("no-animations")
//Result
return {resized:new XMLSerializer().serializeToString(document.querySelector("svg")), height, width}
}, padding))
}
catch (error) {
console.error(error)
console.debug(`metrics/svg/resize > an error occured: ${error}`)
throw error
}
//Convert if required //Convert if required
if (convert) { if (convert) {
console.debug(`metrics/svg/resize > convert to ${convert}`) console.debug(`metrics/svg/resize > convert to ${convert}`)
@@ -185,6 +200,7 @@
} }
//Result //Result
await page.close() await page.close()
console.debug(`metrics/svg/resize > rendering complete`)
return {resized, mime} return {resized, mime}
}, },
/**Render twemojis */ /**Render twemojis */