Skip to content

Instantly share code, notes, and snippets.

@sh0rtwave
Last active September 5, 2021 17:54
Show Gist options
  • Save sh0rtwave/293a933530cd1ff1a1543970892d614f to your computer and use it in GitHub Desktop.
Save sh0rtwave/293a933530cd1ff1a1543970892d614f to your computer and use it in GitHub Desktop.
Generate png + transparent background from HTML Snippet
const style = `font-family: ${data.fontFamily ? data.fontFamily : 'sans-serif'};` +
// `fontSize: ${data.fontSize ? data.fontSize : '24'}px;` +
`font-size: 48px;` +
`font-color: ${data.fontColor ? data.fontColor : 'inherit;'}` +
`align: center;` +
`vertical-align: middle`
const contentToSerialize = `<div style="${style}">${content}</div>`
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = canvas.height = 64
const tempImg = document.createElement('img')
tempImg.addEventListener('load', (ev) => {
ctx.drawImage(ev.target as HTMLVideoElement, 0, 0)
const dataURL = canvas.toDataURL()
this.contentCache[content] = dataURL;
this.setAttribute('data-url',dataURL)
})
tempImg.src = 'data:image/svg+xml,' +
encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<foreignObject width="100%" height="100%">
<div xmlns="http://www.w3.org/1999/xhtml">
${contentToSerialize}
</div>
</foreignObject></svg>`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment