-
Notifications
You must be signed in to change notification settings - Fork 2
/
shaderballs.js
313 lines (291 loc) · 15 KB
/
shaderballs.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* // Ascii art credit https://textart.sh/topic/ball
████████████
████ ████
██ ░░▒▒▒▒▒▒░░ ░░██
██ ░░▒▒▒▒▒▒▒▒▒▒░░ ░░██
██▒▒ ░░▒▒▒▒▒▒▒▒░░ ░░░░██
██░░ ░░▒▒▒▒▒▒▒▒ ░░████
██▒▒░░░░░░░░▒▒▒▒▒▒▒▒▒▒ ████████
██▒▒░░░░▒▒▒▒▒▒▒▒▒▒▒▒░░ ░░████████
██▒▒░░░░▒▒▒▒▒▒▒▒▒▒▒▒ ██████████
████░░░░▒▒▒▒▒▒▒▒▒▒▒▒ ░░██████████
████████▒▒██████▒▒▒▒ ░░████████████
██████████░░ ░░████░░██████████████
██ ████ ██████████████░░██
██ ██░░ ░░██████████ ░░██
██░░ ██████ ░░██
██░░ ░░░░██
████░░░░░░░░░░░░████
████████████
_____ _ _
/ ___| | | |
\ `--.| |__ __ _ __| | ___ _ __
`--. \ '_ \ / _` |/ _` |/ _ \ '__|
/\__/ / | | | (_| | (_| | __/ |
\____/|_| |_|\__,_|\__,_|\___|_|
______ _ _
| ___ \ | | |
| |_/ / __ _| | |___
| ___ \/ _` | | / __|
| |_/ / (_| | | \__ \
\____/ \__,_|_|_|___/
*/
const logBalls = ["🪩","⚽️","⚾️","🏀","🏈","🎾","🎱","🏉","🏐","🔮","🥎","🧶"];
const randBall = () => {return logBalls[Math.floor(Math.random() * logBalls.length)]};
console.log(`${randBall()} : Shaderballs is ballin!`);
const injectionPt = document.getElementById("myFullScreen");
const ourButton = document.createElement("img");
ourButton.src = chrome.runtime.getURL("orb.png");
// Doesn't work lmfao => ourButton.style.backgroundImage = `url('${chrome.runtime.getURL("orb.png")}')`;
ourButton.style.width = "20px";
ourButton.style.height = "20px";
ourButton.style.cursor = "pointer";
ourButton.className="uiButton";
injectionPt.parentElement.insertBefore(ourButton, injectionPt);
const shadertoyCanvas = document.getElementById("demogl");
if(injectionPt && shadertoyCanvas) {
(async () => {
const srcTHREE = chrome.runtime.getURL("node_modules/three/build/three.module.min.js");
const srcCameraCtrls = chrome.runtime.getURL("node_modules/camera-controls/dist/camera-controls.module.min.js");
const THREE = await import(srcTHREE);
let CameraControls = await import(srcCameraCtrls);
CameraControls = CameraControls.default;
// Dev Note : Cannot use vanilla THREE.js camera-controls b/c module-maps are not possible
// Seee GH issue tradgedy https://github.com/WICG/import-maps/issues/92
CameraControls.install({THREE: THREE});
ourButton.addEventListener("mousedown", () => {
console.log(`${randBall()} : OrbToy Created.`);
const babyWindow = document.createElement("div");
babyWindow.className="shaderballs";
babyWindow.style.position = "absolute";
babyWindow.style.top = "40px";
babyWindow.style.left = "50%";
babyWindow.style.borderRadius = "16px";
babyWindow.style.backdropFilter = "blur(5px)";
babyWindow.style.border = "1px solid ivory";
babyWindow.style.display = "flex";
babyWindow.style.flexDirection = "column";
babyWindow.style.overflow = "hidden";
babyWindow.style.zIndex="69";//lol
babyWindow.style.boxShadow = "0 4px 8px rgba(0, 0, 0, 0.1), 0 6px 20px rgba(0, 0, 0, 0.1)";
babyWindow.style.width = "50vh";//`${shadertoyCanvas.clientHeight}px`;
babyWindow.style.height = "50vh";//`${shadertoyCanvas.clientHeight}px`;
babyWindow.tabIndex = 1;
const header = document.createElement("div");
header.style.display = "flex";
header.style.flexDirection = "row";
header.style.backgroundColor = "#f0f0f0";
header.style.width = "100%";
const title = document.createElement("div")
title.innerText = "Shaderballs";
title.width = "100%";
title.style.fontFamily = "Lobster,Tahoma,Arial"
title.style.fontWeight = "bold";
title.style.textAlign = "center";
title.style.padding = "5px";
title.style.fontSize="16px"
const close = document.createElement("div");
close.innerText = "X";
close.style.fontSize="18px"
close.style.fontWeight = "bold";
close.style.cursor = "pointer";
close.style.padding = "5px";
close.style.marginLeft = "auto";
close.style.marginRight = "5px";
close.fontWeight = "bold";
const canvas = document.createElement("canvas");
canvas.style.setProperty('height', '100%', 'important');
canvas.style.setProperty('width', '100%', 'important');;
header.appendChild(title);
header.appendChild(close);
babyWindow.appendChild(header);
babyWindow.appendChild(canvas);
document.body.appendChild(babyWindow);
const gl = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
preserveDrawingBuffer: true
});
gl.setClearColor(0xFFFFFF, 0);
gl.setPixelRatio(window.devicePixelRatio);
const height = canvas.clientHeight;
const width = canvas.clientWidth;
gl.setSize(width, height);
// THREE.js assigns absolute PX values that we don't want so we re-assign them.
canvas.style.setProperty('height', '100%', 'important');
canvas.style.setProperty('width', '100%', 'important');
gl.autoClear = false;
const camera = new THREE.PerspectiveCamera(
47,
width / height,
0.1,
200000
);
camera.position.set(0, 0, 1);
const ctrls = new CameraControls(camera, canvas)
const scene = new THREE.Scene();
const orb = new THREE.Mesh(
new THREE.SphereGeometry(0.3, 32, 32),
new THREE.MeshBasicMaterial({ color: 0xFFFFFF, map: new THREE.CanvasTexture(shadertoyCanvas), side: THREE.DoubleSide})
);
orb.material.map.colorSpace = THREE.SRGBColorSpace;
scene.add(orb);
let frame = null;
const clock = new THREE.Clock();
function render(){
ctrls.update(clock.getDelta());
orb.material.map.needsUpdate = true;
gl.clear();
gl.render(scene, camera);
frame = requestAnimationFrame(render);
}
frame = requestAnimationFrame(render);
close.addEventListener("mousedown", (e) => {
e.preventDefault();
e.stopPropagation();
// THIS NEEDS TO HAPPEN FIRST (Weird Chrome Bug)
babyWindow.remove();
cancelAnimationFrame(frame);
orb.material.map.dispose();
orb.material.dispose();
orb.geometry.dispose();
console.log(`${randBall()} : Removed Orb Toy`)
})
let isDragging = false;
let offsetX, offsetY;
header.addEventListener('mousedown', (e) => {
e.preventDefault();
e.stopPropagation();
isDragging = true;
// Calculate the offset (difference) between the mouse position and the element's top-left corner
offsetX = e.clientX - babyWindow.getBoundingClientRect().left;
offsetY = e.clientY - babyWindow.getBoundingClientRect().top;
header.style.cursor = 'grabbing';
focusBabyWindow();
});
document.addEventListener('mousemove', (e) => {
if (isDragging) {
// Calculate the new position of the element
const x = e.clientX - offsetX;
const y = Math.max(e.clientY - offsetY,0);
// Set the new position of the element
babyWindow.style.left = `${x}px`;
babyWindow.style.top = `${y}px`;
}
});
window.addEventListener('mouseup', () => {
isDragging = false;
header.style.cursor = 'pointer';
});
const resizableWindow = document.getElementById('resizableWindow');
let isResizing = false;
let originalWidth, originalHeight, originalX, originalY, originalMouseX, originalMouseY;
let edge;
canvas.addEventListener('resize', (e) => {})
babyWindow.addEventListener('mousedown', (e) => {
const rect = babyWindow.getBoundingClientRect();
const offset = 10; // Distance from the edge to start resizing
if (e.clientX >= rect.right - offset && e.clientY >= rect.bottom - offset) {
edge = 'bottom-right';
} else if(e.clientX <= rect.left + offset && e.clientY >= rect.bottom - offset) {
edge = 'bottom-left';
} else if (e.clientX >= rect.right - offset) {
edge = 'right';
} else if (e.clientX <= rect.left + offset) {
edge = 'left';
} else if (e.clientY >= rect.bottom - offset) {
edge = 'bottom';
console.log("bottoms")
} else {
edge = null;
}
if (edge) {
e.preventDefault();
e.stopPropagation();
isResizing = true;
originalWidth = parseFloat(getComputedStyle(babyWindow, null).getPropertyValue('width').replace('px', ''));
originalHeight = parseFloat(getComputedStyle(babyWindow, null).getPropertyValue('height').replace('px', ''));
originalX = babyWindow.getBoundingClientRect().left;
originalY = babyWindow.getBoundingClientRect().top;
originalMouseX = e.pageX;
originalMouseY = e.pageY;
window.addEventListener('mousemove', resize);
window.addEventListener('mouseup', stopResize);
}
});
function resize(e) {
if (edge === 'right') {
const width = originalWidth + (e.pageX - originalMouseX);
if (width > 100) { // Minimum width
babyWindow.style.width = width + 'px';
}
} else if (edge === 'left') {
const width = originalWidth - (e.pageX - originalMouseX);
if (width > 100) { // Minimum width
babyWindow.style.width = width + 'px';
babyWindow.style.left = originalX + (e.pageX - originalMouseX) + 'px';
}
} else if (edge === 'bottom') {
const height = originalHeight + (e.pageY - originalMouseY);
if (height > 100) { // Minimum height
babyWindow.style.height = height + 'px';
}
} else if (edge === 'bottom-right') {
const width = originalWidth + (e.pageX - originalMouseX);
const height = originalHeight + (e.pageY - originalMouseY);
if (width > 100) { // Minimum width
babyWindow.style.width = width + 'px';
}
if (height > 100) { // Minimum height
babyWindow.style.height = height + 'px';
}
} else if (edge === 'bottom-left') {
const width = originalWidth - (e.pageX - originalMouseX);
const height = originalHeight + (e.pageY - originalMouseY);
if (width > 100) { // Minimum width
babyWindow.style.width = width + 'px';
babyWindow.style.left = originalX + (e.pageX - originalMouseX) + 'px';
}
if (height > 100) { // Minimum height
babyWindow.style.height = height + 'px';
}
}
}
function stopResize() {
isResizing = false;
window.removeEventListener('mousemove', resize);
window.removeEventListener('mouseup', stopResize);
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix()
gl.setSize(canvas.clientWidth, canvas.clientHeight);
canvas.style.setProperty('height', '100%', 'important');
canvas.style.setProperty('width', '100%', 'important');
}
babyWindow.addEventListener('mousemove', (e) => {
const rect = babyWindow.getBoundingClientRect();
const offset = 10; // Distance from the edge to change cursor
if (e.clientX >= rect.right - offset && e.clientY >= rect.bottom - offset) {
babyWindow.style.cursor = 'nwse-resize';
} else if (e.clientX <= rect.left + offset && e.clientY >= rect.bottom - offset) {
babyWindow.style.cursor = 'nesw-resize';
} else if (e.clientX >= rect.right - offset) {
babyWindow.style.cursor = 'ew-resize';
} else if (e.clientX <= rect.left + offset) {
babyWindow.style.cursor = 'ew-resize';
} else if (e.clientY >= rect.bottom - offset) {
babyWindow.style.cursor = 'ns-resize';
} else {
babyWindow.style.cursor = 'default';
}
});
babyWindow.addEventListener('focus', (e) => {
focusBabyWindow();
})
function focusBabyWindow(){
//const balls = document.getElementsByClassName("shaderballs");
document.body.appendChild(babyWindow);
}
})
})();
}