-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
212 lines (195 loc) · 6.84 KB
/
index.html
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
width: 100%;
background-image: linear-gradient(
rgba(0, 127, 239, 0.3) 0.1em,
transparent 0.1em
),
linear-gradient(
90deg,
rgba(0, 127, 239, 0.3) 0.1em,
transparent 0.1em
);
background-size: 6em 6em;
}
.resize-drag {
margin: 1rem;
font-size: 20px;
font-family: sans-serif;
background-color: rgb(0, 127, 239, 0.2);
border: 2px solid rgb(0, 127, 239, 0.2);
display: inline-block;
box-sizing: content-box;
position: relative;
}
#cloudComputerDiv {
width: 100%;
height: 100%;
}
.bar {
position: absolute;
top: 100%;
left: -2px;
background-color: rgb(0, 127, 239);
border-bottom: 2px solid rgb(0, 127, 239, 0.2);
border-left: 2px solid rgb(0, 127, 239, 0.2);
border-right: 2px solid rgb(0, 127, 239, 0.2);
height: 36px;
width: 100%;
text-align: center;
}
.lock.hidden {
display: none;
}
.lock {
border: none;
background: none;
cursor: pointer;
display: inline-block;
margin: 4px auto;
}
path {
fill: #fff;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div class="resize-drag hidden">
<div id="cloudComputerDiv"></div>
<div class="bar">
<button id="unlock" class="lock">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
>
<path
d="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"
/>
</svg>
</button>
<button id="lock" class="lock hidden">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
>
<path
d="M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6C4.89,22 4,21.1 4,20V10A2,2 0 0,1 6,8H15V6A3,3 0 0,0 12,3A3,3 0 0,0 9,6H7A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,17A2,2 0 0,0 14,15A2,2 0 0,0 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17Z"
/>
</svg>
</button>
</div>
</div>
<script src="https://unpkg.com/interactjs/dist/interact.min.js"></script>
<script type="module">
import Hyperbeam from "https://unpkg.com/@hyperbeam/web@latest/dist/index.js";
async function main() {
const container = cloudComputerDiv.parentElement;
const initX = localStorage.getItem("x");
const initY = localStorage.getItem("y");
if (initX && initY) {
container.style.transform = `translate(${initX}px, ${initY}px)`;
container.setAttribute("data-x", ~~initX);
container.setAttribute("data-y", ~~initY);
}
const initWidth = localStorage.getItem("width");
const initHeight = localStorage.getItem("height");
let baseUrl = "/computer";
if (initWidth && initHeight) {
baseUrl += `?width=${initWidth}&height=${initHeight}`;
}
const resp = await fetch(baseUrl);
const data = await resp.json();
const hb = await Hyperbeam(cloudComputerDiv, data.embed_url);
container.style.width = `${hb.width}px`;
container.style.height = `${hb.height}px`;
container.classList.remove("hidden");
lock.addEventListener("click", disableInteractives);
unlock.addEventListener("click", () => enableInteractives(hb));
enableInteractives(hb);
}
main();
function dragMoveListener(event) {
const target = event.target;
const x = (parseFloat(target.getAttribute("data-x")) || 0) + event.dx;
const y = (parseFloat(target.getAttribute("data-y")) || 0) + event.dy;
target.style.transform = `translate(${x}px, ${y}px)`;
target.setAttribute("data-x", x);
target.setAttribute("data-y", y);
localStorage.setItem("x", x.toString());
localStorage.setItem("y", y.toString());
}
function enableInteractives(hb) {
lock.classList.remove("hidden");
unlock.classList.add("hidden");
interact(".resize-drag")
.resizable({
edges: { left: true, right: true, bottom: true, top: true },
listeners: {
move(event) {
const target = event.target;
const width = Math.floor(event.rect.width);
const height = Math.floor(event.rect.height);
let x = parseFloat(target.getAttribute("data-x")) || 0;
let y = parseFloat(target.getAttribute("data-y")) || 0;
x += event.deltaRect.left;
y += event.deltaRect.top;
target.style.width = `${width}px`;
target.style.height = `${height}px`;
target.style.transform = `translate(${x}px, ${y}px)`;
target.setAttribute("data-x", x);
target.setAttribute("data-y", y);
localStorage.setItem("x", x.toString());
localStorage.setItem("y", y.toString());
},
end(event) {
const target = event.target;
const desiredWidth = event.rect.width;
const desiredHeight = event.rect.height;
const pixels = Math.min(
desiredWidth * desiredHeight,
hb.maxArea
);
const aspectRatio = desiredWidth / desiredHeight;
const height = Math.floor(Math.sqrt(pixels / aspectRatio));
const width = Math.floor(height * aspectRatio);
try {
hb.resize(width, height);
} catch {}
localStorage.setItem("width", width.toString());
localStorage.setItem("height", height.toString());
target.style.width = desiredWidth;
target.style.height = desiredHeight;
},
},
modifiers: [
interact.modifiers.restrictSize({
min: { width: 100, height: 50 },
}),
],
inertia: true,
})
.draggable({
listeners: { move: dragMoveListener },
inertia: true,
});
}
function disableInteractives() {
lock.classList.add("hidden");
unlock.classList.remove("hidden");
interact(".resize-drag").unset();
}
</script>
</body>
</html>