feat: load and display image with correct scaling
This commit is contained in:
parent
037f30fe8b
commit
015ff77123
16
index.html
16
index.html
|
@ -15,20 +15,20 @@
|
||||||
<div class="hero is-fullheight">
|
<div class="hero is-fullheight">
|
||||||
<div class="hero-body">
|
<div class="hero-body">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="columns is-mobile">
|
<canvas id="myCanvas" class="is-hidden" width="300" height="300"></canvas>
|
||||||
|
<div id="imports" class="columns is-mobile">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<button
|
<button id="take-picture"
|
||||||
class="button is-large is-fullwidth"
|
class="button is-large is-responsive is-fullwidth">
|
||||||
style="padding: 100% 0 100% 0">
|
|
||||||
Take a Picture
|
Take a Picture
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<button
|
<label for="upload-image"
|
||||||
class="button is-large is-fullwidth"
|
class="button is-large is-responsive is-fullwidth">
|
||||||
style="padding: 100% 0 100% 0">
|
|
||||||
Upload an Image
|
Upload an Image
|
||||||
</button>
|
</label>
|
||||||
|
<input type="file" id="upload-image" accept="image/*" style="display: none">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div><br>
|
</div><br>
|
||||||
|
|
34
main.js
34
main.js
|
@ -1,6 +1,34 @@
|
||||||
document.addEventListener("DOMContentLoaded", main)
|
let canvas, imports;
|
||||||
|
|
||||||
function main() {
|
// wait for site to be parsed so element can be found
|
||||||
|
document.addEventListener("DOMContentLoaded", function () {
|
||||||
|
// bind listeners
|
||||||
|
document.getElementById("take-picture").addEventListener("click", take_picture);
|
||||||
|
document.getElementById("upload-image").addEventListener("change", upload_image)
|
||||||
|
|
||||||
|
canvas = document.getElementById("myCanvas");
|
||||||
|
imports = document.getElementById("imports");
|
||||||
|
})
|
||||||
|
|
||||||
|
function take_picture() {
|
||||||
|
canvas.classList.remove("is-hidden");
|
||||||
|
imports.classList.add("is-hidden");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function upload_image(event) {
|
||||||
|
canvas.classList.remove("is-hidden");
|
||||||
|
imports.classList.add("is-hidden");
|
||||||
|
console.log(this.files[0]);
|
||||||
|
|
||||||
|
const img = new Image();
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
img.src = URL.createObjectURL(this.files[0]);
|
||||||
|
img.onload = function() {
|
||||||
|
canvas.width = img.naturalWidth;
|
||||||
|
canvas.height = img.naturalHeight;
|
||||||
|
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in a new issue