Last active
January 8, 2019 22:16
-
-
Save zsajjad/10f21ec6be6d22fc9bf0c6137d43ba6e to your computer and use it in GitHub Desktop.
Drawing an image from URL in html canvas
This file contains 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
import loadImage from 'blueimp-load-image'; | |
const imageLoaderConfigs = { | |
maxWidth: 224, | |
maxHeight: 224, | |
cover: true, | |
crop: true, | |
canvas: true, | |
crossOrigin: 'Anonymous', | |
} | |
function renderImage(img: Event | HTMLImageElement) { | |
if ((img as Event).type === 'error') { | |
return | |
} | |
try { | |
const element = document.getElementById('input-canvas') as HTMLCanvasElement; | |
const ctx = element.getContext('2d'); | |
ctx.drawImage(img as HTMLImageElement, 0, 0); | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
export default function loadImageToCanvas(url: string) { | |
if (!url) { | |
return; | |
} | |
loadImage(url, renderImage, imageLoaderConfigs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment