-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntacsImage.js
71 lines (57 loc) · 2.2 KB
/
syntacsImage.js
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
class SyntacsImage {
//changes the height of the image to display it with the correct aspect ratio
static resizeImage(image){
//getting the original width and height of the image
const originalWidth = image.naturalWidth;
const originalHeight = image.naturalHeight;
//calculating the ratio
const originalRatio = originalWidth / originalHeight;
//scaling the height to maintain original aspect ratio
image.height = image.width / originalRatio;
}
static getCursorStyle(isEnlarged) {
if (isEnlarged) {
//setting the cursor to magnifying glass with minus icon
return "zoom-out";
} else {
//setting the cursor to magnifying glass with plus icon
return "zoom-in";
}
}
//adds the event listener to the image so that it can be resized on click
static imageEventListener(image){
//boolean for storing current state of image
var isEnlarged = false;
//setting the cursor to magnifying glass with plus icon here so that it would appear on first hover
image.style.cursor = SyntacsImage.getCursorStyle(isEnlarged);
//saving the original width/height
const smallWidth = image.width;
const smallHeight = image.height;
//setting the max width for an enlarged image
//TODO: Make this relative to window size
const maxImageWidth = 400;
//adding the event listener
image.addEventListener("click",
function(){
//flipping the state bool
isEnlarged = !isEnlarged;
if(isEnlarged){
//setting size to natural size
image.width = maxImageWidth;
image.height = (maxImageWidth / (image.naturalWidth / image.naturalHeight));
//resizing the image given new sizes
SyntacsImage.resizeImage(image);
}
else{
//setting size to original display size
image.width = smallWidth;
image.height = smallHeight;
//resizing the image given new sizes
SyntacsImage.resizeImage(image);
}
image.style.cursor = SyntacsImage.getCursorStyle(isEnlarged);
//resizing the popup so the image would fit
Utility.autoResize(document.body, document.getElementById("imageBox"));
}, true);
};
};