Skip to content

Instantly share code, notes, and snippets.

@geelen
Created March 27, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geelen/699b6ae68338c2fcbc5e0a347f2ab9fb to your computer and use it in GitHub Desktop.
Save geelen/699b6ae68338c2fcbc5e0a347f2ab9fb to your computer and use it in GitHub Desktop.
// Doesn't work unless the image host accepts CORS
window.download = a => {
const { opacity, country, blendMode } = current
const canvas = document.createElement('canvas')
canvas.width = 1280
canvas.height = 720
const ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, 1280, 720)
ctx.globalCompositeOperation = 'source-over'
const img = document.createElement('img')
img.crossOrigin = "use-credentials"
img.src = currentImage()
ctx.drawImage(img, 0, 0, 1280, 720)
console.log(flags(opacity)[country])
const [_, svgUrl] = /^.*url\("(.*)"\).*$/m.exec(flags(opacity)[country])
console.log(svgUrl)
if (svgUrl) {
img.src = svgUrl
ctx.globalCompositeOperation = blendMode
ctx.drawImage(img, 0, 0, 1280, 720)
}
document.body.appendChild(canvas)
a.href = canvas.toDataURL()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment