-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (84 loc) · 2.87 KB
/
script.js
File metadata and controls
99 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var defaultImgPath, imageHeight, imageWidth;
document.addEventListener("DOMContentLoaded", function () {
showLoadPictureTextIfEmpty();
loadHistory();
document.getElementById("load_pic_text").addEventListener("click", openFilePickerDialog);
console.log("screen hegit", window.screen.height, "x", window.screen.width)
});
//Drag drop
document.addEventListener('drop', (event) => {
event.preventDefault();
event.stopPropagation();
console.log(event.dataTransfer.files)
if (event.dataTransfer.files?.length > 0) {
var f = event.dataTransfer.files[0];
console.log('File Path of dragged files: ', f.path)
var path = f.path;
setImage(path);
}
});
document.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
});
function loadHistory() {
var data = window.api.getHistory();
if (data.lastPath) defaultImgPath = data.lastPath;
}
function openFilePickerDialog() {
var imagePath = window.api.showOpenDialogSync({
properties: ['openFile'],
message: "Choose picture location",
filters: [{ name: 'Images', extensions: ['jpg', 'png', 'gif'] }],
defaultPath: defaultImgPath ? defaultImgPath : app.getPath("pictures")
});
if (imagePath)
setImage(imagePath[0]);
}
function setImage(imgPath) {
document.getElementById("img1").setAttribute("src", imgPath);
document.getElementById("load_pic_text").classList.add("hidden");
var myImage = new Image();
myImage.name = imgPath;
myImage.onload = resizeWindowToImage;
myImage.src = imgPath;
console.log(imgPath)
var history = {};
history.lastPath = imgPath.substring(0, imgPath.lastIndexOf("\\"));
defaultImgPath = history.lastPath;
document.querySelector(".container").style.backgroundColor = "transparent";
}
function showLoadPictureTextIfEmpty() {
var imgEle = document.getElementById("img1");
var image = imgEle.getAttribute("src");
if (image == null)
document.getElementById("load_pic_text").classList.remove("hidden");
}
function resizeWindowToImage() {
imageHeight = this.height;
imageWidth = this.width;
var img = document.getElementById("img1");
console.log("Image dimension ", imageHeight, "x", imageWidth)
var screenHeight = window.screen.height;
var screenWidth = window.screen.width;
var ratio = 1;
var tooBig = false;
if (imageHeight > screenHeight) {
ratio = screenHeight / imageHeight;
tooBig = true;
}
if (imageWidth > screenWidth) {
if (screenWidth / imageWidth < ratio) ratio = screenWidth / imageWidth;
tooBig = true;
}
console.log(ratio, " ratio")
ratio *= 0.95;
imageHeight *= ratio;
imageWidth *= ratio;
if (tooBig) {
img.height = imageHeight;
img.width = imageWidth;
}
window.resizeTo(imageWidth, imageHeight)
return true;
}