-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Octoframes/dynamic_images
Implemented Dynamic Image Updates
- Loading branch information
Showing
10 changed files
with
253 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.imgs_wrapper { | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
gap: 10px; | ||
} | ||
|
||
.img { | ||
width: 200px; | ||
height: 200px; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
|
||
border-color: black; | ||
border-style: solid; | ||
} | ||
|
||
.dragover { | ||
border-style: dashed; | ||
border-color: red; | ||
} | ||
|
||
.image_preview { | ||
width: 100%; | ||
} |
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,97 @@ | ||
// based on https://soshace.com/the-ultimate-guide-to-drag-and-drop-image-uploading-with-pure-javascript | ||
|
||
function load_from_disk(files, callback) { | ||
if (files.length != 1) | ||
console.log("Warning: only using first image"); | ||
|
||
let reader = new FileReader(); | ||
reader.onload = (e) => { | ||
let url = e.target.result; | ||
callback(url); | ||
} | ||
reader.readAsDataURL(files[0]); | ||
} | ||
|
||
function load_from_img_element(data) { | ||
let html = data.getData("text/html"); | ||
let match = html && /\bsrc="?([^"\s]+)"?\s*/.exec(html); | ||
// TODO: use quiet fail | ||
if (!match) | ||
throw "Failed to load url from dragged object."; | ||
let url = match[1]; | ||
return url; | ||
} | ||
|
||
function get_valid_urls(image_urls) { | ||
return image_urls.filter(url => url); | ||
} | ||
|
||
function kill_all_children(parent) { | ||
while (parent.lastChild) { | ||
parent.removeChild(parent.lastChild); | ||
} | ||
} | ||
|
||
function load_new_url(target, url, image_urls, on_new_imgs, image_id) { | ||
image_urls[image_id] = url; | ||
console.log(`new image at index ${image_id}: ${url}`); | ||
on_new_imgs(get_valid_urls(image_urls)); | ||
|
||
// update image | ||
kill_all_children(target); | ||
let img = document.createElement("img"); | ||
img.src = url; | ||
img.classList.add("image_preview") | ||
target.appendChild(img); | ||
} | ||
|
||
function handle_drop(image_urls, e, on_new_imgs, image_id) { | ||
let data = e.dataTransfer; | ||
let files = data.files; | ||
// drag+drop from user file system | ||
if (files.length) | ||
load_from_disk(files, (url) => { | ||
load_new_url(e.target, url, image_urls, on_new_imgs, image_id); | ||
}); | ||
// drag+drop from HTMLImageElement (or invalid drop (e.g. text)) | ||
else { | ||
let url = load_from_img_element(data); | ||
load_new_url(e.target, url, image_urls, on_new_imgs, image_id); | ||
} | ||
} | ||
|
||
function prevent_default(e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
} | ||
|
||
function enable_style_change(image) { | ||
// don't you fucking dare touch this | ||
// this garbage took me an hour to not glitch around like a berserk all the time | ||
image.addEventListener("dragover", () => { | ||
image.classList.add("dragover"); | ||
}); | ||
image.addEventListener("dragleave", () => { | ||
image.classList.remove("dragover"); | ||
}); | ||
image.addEventListener("drop", () => { | ||
image.classList.remove("dragover"); | ||
}); | ||
} | ||
|
||
function load_img_drop(image_urls, on_new_imgs) { | ||
let images = document.getElementsByClassName("img"); | ||
for (let i = 0; i < images.length; ++i) { | ||
images[i].addEventListener("dragenter", prevent_default, false); | ||
images[i].addEventListener("dragleave", prevent_default, false); | ||
images[i].addEventListener("dragover", prevent_default, false); | ||
images[i].addEventListener("drop", prevent_default, false); | ||
|
||
images[i].addEventListener("drop", (e) => { | ||
handle_drop(image_urls, e, on_new_imgs, i); | ||
}); | ||
|
||
enable_style_change(images[i]); | ||
} | ||
} | ||
|
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,68 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<title>compare_view Dynamic Images</title> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
<meta name="description" content="" /> | ||
<link rel="stylesheet" href="./drag_drop.css"> | ||
</head> | ||
|
||
<body> | ||
<script src="../dist/browser_compare_view.js"></script> | ||
<script src="./drag_drop.js"></script> | ||
|
||
<img src="../images/banner_colour.png" width=500 /> | ||
<img src="../images/banner_grey.png" width=500 /> | ||
|
||
<div class="imgs_wrapper"> | ||
<div class="img"> | ||
<div>Drag&Drop Image Here</div> | ||
</div> | ||
<div class="img"> | ||
<div>Drag&Drop Image Here</div> | ||
</div> | ||
<div class="img"> | ||
<div>Drag&Drop Image Here</div> | ||
</div> | ||
<div class="img"> | ||
<div>Drag&Drop Image Here</div> | ||
</div> | ||
</div> | ||
|
||
<div style="display: flex; flex-direction: row; width: 100%;"> | ||
<div> | ||
<canvas id="canvas" style="width: 100%;"></canvas> | ||
</div> | ||
<div id="controls" style="width: 120px;"> | ||
</div> | ||
</div> | ||
<script> | ||
compare_view.load( | ||
[ | ||
"../images/cat.png", | ||
"../images/cat_grey.png", | ||
], | ||
"canvas", | ||
{ | ||
controls_id: "controls", | ||
}, | ||
(cvd) => { | ||
load_img_drop([ | ||
"../images/cat.png", | ||
"../images/cat_grey.png", | ||
undefined, | ||
undefined, | ||
undefined, | ||
], (image_urls) => { | ||
// TODO: remove debug | ||
console.log(image_urls); | ||
compare_view.load_new_imgs(cvd, image_urls); | ||
}); | ||
} | ||
); | ||
</script> | ||
</body> | ||
|
||
</html> |
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
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
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import { choose_cfg, Config } from "./cfg"; | ||
import { CompareViewData, Task } from "./compare_view_data"; | ||
import { CompareViewData } from "./compare_view_data"; | ||
|
||
// needs to get called every time canvas size changes | ||
function load_fractions(cvd: CompareViewData, cfg: Config): void { | ||
function load_fractions(cvd: CompareViewData): void { | ||
let max_size = Math.max(cvd.canvas.width, cvd.canvas.height); | ||
cvd.circumference_thickness = max_size * choose_cfg(cfg, "circumference_fraction"); | ||
cvd.circumference_thickness = max_size * choose_cfg(cvd.cfg, "circumference_fraction"); | ||
// use circle_size when provided, otherwise use fraction | ||
cvd.circle_size = cfg.circle_size != undefined ? cfg.circle_size : max_size * choose_cfg(cfg, "circle_fraction"); | ||
cvd.slider_thickness = max_size * choose_cfg(cfg, "slider_fraction"); | ||
cvd.circle_size = cvd.cfg.circle_size != undefined ? cvd.cfg.circle_size : max_size * choose_cfg(cvd.cfg, "circle_fraction"); | ||
cvd.slider_thickness = max_size * choose_cfg(cvd.cfg, "slider_fraction"); | ||
} | ||
|
||
export function load_canvas_scaling(cvd: CompareViewData, img_resolution: [number, number], cfg: Config): void { | ||
export function load_canvas_scaling(cvd: CompareViewData, img_resolution: [number, number]): void { | ||
cvd.canvas.width = img_resolution[0]; | ||
cvd.canvas.height = img_resolution[1]; | ||
cvd.width = cvd.canvas.width; | ||
cvd.height = cvd.canvas.height; | ||
|
||
load_fractions(cvd, cfg); | ||
load_fractions(cvd); | ||
} |
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