Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Created January 25, 2014 07:28
Show Gist options
  • Select an option

  • Save jarek-foksa/8613003 to your computer and use it in GitHub Desktop.

Select an option

Save jarek-foksa/8613003 to your computer and use it in GitHub Desktop.
let extractBitmapSize = (bitmap, callback) => {
let imageElement = new Image();
imageElement.onload = () => {
callback(imageElement.naturalWidth, imageElement.naturalHeight);
};
imageElement.src = bitmap;
};
document.body.addEventListener('drop', (event) => {
event.stopPropagation();
event.preventDefault();
let files = event.dataTransfer.files;
for (let file of files) {
if (file.type === 'image/png') {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.addEventListener('load', (event) => {
let bitmap = event.target.result;
extractBitmapSize(bitmap, (width, height) => {
let image = SVG`<image></image>`;
image.setAttribute('width', width);
image.setAttribute('height', height);
image.setAttributeNS(xlinkNS, 'xlink:href', `${bitmap}`);
image.appendTo(this.graphics);
});
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment