-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement button for importing a image
Merge pull request #88 from haroldo-ok/export-image
- Loading branch information
Showing
3 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const openFileDialog = (accept) => new Promise((resolve, reject) => { | ||
// Adapted from https://stackoverflow.com/a/72664569/679240 | ||
|
||
const input = document.createElement('input'); | ||
input.type = 'file'; | ||
input.setAttribute('accept', accept); | ||
input.onchange = function(event) { | ||
resolve(this.files[0]); | ||
}; | ||
input.click(); | ||
}); | ||
|
||
export const loadImageFromFile = (file) => new Promise((resolve, reject) => { | ||
// Adapted from https://stackoverflow.com/a/33112602/679240 | ||
const reader = new FileReader(); | ||
// load to image to get it's width/height | ||
const img = new Image(); | ||
img.onload = function() { | ||
resolve(img); | ||
}; | ||
img.onerror = reject; | ||
// this is to setup loading the image | ||
reader.onloadend = () => { | ||
img.src = reader.result; | ||
}; | ||
// this is to read the file | ||
reader.readAsDataURL(file); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const createResizedCanvas = (img, width, height) => { | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = width; | ||
canvas.height = height; | ||
|
||
const ctx = canvas.getContext('2d'); | ||
ctx.drawImage(img, 0, 0, width, height); | ||
|
||
return canvas; | ||
}; |