Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zsajjad
Last active January 8, 2019 22:16
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 zsajjad/10f21ec6be6d22fc9bf0c6137d43ba6e to your computer and use it in GitHub Desktop.
Save zsajjad/10f21ec6be6d22fc9bf0c6137d43ba6e to your computer and use it in GitHub Desktop.
Drawing an image from URL in html canvas
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