-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
187 lines (168 loc) · 5.35 KB
/
script.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// the jquery code
$(document).ready(function () {
// we initially hide the button to close the capture
$(".button-container").hide();
// dragging is initially false
let isDragging = false;
let offset = { x: 0, y: 0 };
// important to distinguish what is draggable
let $currentDraggable = null;
// used so that the element doesnt fidget when selected
$(".draggable-element").css({
border: "2px solid transparent",
});
// just for hovering and selecting
$(".draggable-element").hover(
function () {
if ($(this).css("opacity") === "1") {
$(this).css("border", "2px dotted red");
}
},
function () {
if ($(this).css("opacity") === "1") {
$(this).css("border", "2px solid transparent");
}
}
);
$(".draggable-element").mousedown(function (event) {
// we first see if its visible (meaning if it is in use)
if ($(this).css("opacity") === "1") {
// we are dragging, and this is the current draggable element
isDragging = true;
$currentDraggable = $(this);
// find the offset and position
offset.x = event.pageX - $currentDraggable.offset().left;
offset.y = event.pageY - $currentDraggable.offset().top;
event.preventDefault();
}
});
$(".draggable-element").mousemove(function (event) {
if (isDragging && $currentDraggable) {
// move to new position only when its draggable and its the current draggable element
const newX = event.pageX - offset.x;
const newY = event.pageY - offset.y;
$currentDraggable.css({ left: newX, top: newY });
}
});
$(".draggable-element").mouseup(function () {
// once done dragging, we set drag to false, and unknown current draggable element
isDragging = false;
$currentDraggable = null;
});
// animation code for capture
// used to prevent any issues
let isAnimating = false;
// when we hover over our capture, we do our animation
$("#capture").on("mouseenter", function () {
// check first if we're in animation
if (!isAnimating) {
$("#capture").animate(
// we animate to the full screen width
{
width: "100%",
height: "auto",
right: 0,
bottom: 0,
padding: 0,
},
{
duration: 300,
// once done, we end animation, and show a closing button
complete: function () {
$(".button-container").show();
isAnimating = false;
},
}
);
isAnimating = true; // animation is in progress
}
});
// a close button for our capture
$(".close-button").click(function () {
// first check if we're already animating
if (!isAnimating) {
// otherwise, we animate the capture to go to original dimensions
// first hide the button
$(".button-container").hide();
// then animate
$("#capture").animate(
{
right: "0px",
bottom: "0px",
width: "300px",
height: "auto",
padding: "10px",
},
{
duration: 300,
// once done, we end the animation
complete: function () {
isAnimating = false;
},
}
);
isAnimating = true; // animation is in progress
}
});
});
// function to toggle each body part from said button
function togglePart(part, button) {
// get the element of said part
let element = document.getElementById(part);
// first check if its not visible using opacity
if (element.style.opacity === "0") {
// if not, we set it visible, enable pointer events
element.style.opacity = "1";
element.style.pointerEvents = "auto";
// change up the look of button using bootstrap
button.classList.add("bg-primary", "text-white");
} else {
// do the opposite of previous if statement
element.style.opacity = "0";
element.style.pointerEvents = "none";
button.classList.remove("bg-primary", "text-white");
}
}
// funciton for getting the screenshot
function getScreenShot(selector) {
// use html to canvas
html2canvas(document.querySelector("#canvas"), { useCORS: true }).then(
(canvas) => {
// get the image
const img = canvas.toDataURL("image/png");
// either we use the screenshot for downloading it
if (selector == 1) downloadScreenShot(img);
// or we capture it (part of problem 2)
else if (selector == 2) captureScreenShot(img);
}
);
}
// downloading our screenshot
function downloadScreenShot(src) {
// prompt the user to enter the image name
const imgName = prompt("Enter Image Name:");
// we create a link to add our image to it
const link = document.createElement("a");
link.href = src;
link.download = imgName;
// we then initialize a click, and download the image
link.click();
}
// get a capture of our screenshot
function captureScreenShot(src) {
// add it to a div as an img tag
let capture = document.getElementById("capture");
capture.innerHTML = `<img id="capture-img" src=${src}>`;
// set the div to be visible
capture.style.display = "inline";
}
// just an issue on my code, where i want all the parts to be hidden
function startup() {
const buttons = document.querySelectorAll("#partButton");
// so i simply trigger each button to toggle it
buttons.forEach((button) => {
button.click();
});
}
// then call the function on load
startup();