Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FreshPenguin112
GitHub Repository: FreshPenguin112/bookmarklets
Path: blob/main/tri.js
5051 views
1
(function() {
2
var tri = {
3
menu: document.createElement("div"),
4
limit: document.createElement("input"),
5
gap: document.createElement("input"),
6
sag: document.createElement("input"),
7
fov: document.createElement("input"),
8
flo: document.createElement("input"),
9
off: document.createElement("input"),
10
non: document.createElement("input"),
11
end: document.createElement("input"),
12
tgl: document.createElement("input"),
13
cssStatic: document.createElement("style"),
14
cssDynamic: document.createElement("style"),
15
orientation: {"yaw": 0, "pitch": 0, "roll": 0},
16
mouseMove: function(e) {
17
tri.orientation.yaw = -Math.cos(Math.PI * e.clientX / innerWidth) * 180 * tri.limit.value;
18
tri.orientation.pitch = Math.cos(Math.PI * e.clientY / innerHeight) * 180 * tri.limit.value;
19
tri.updateBody();
20
},
21
gyroMove: function(e) {
22
var landscape = innerWidth > innerHeight;
23
if (landscape) {
24
tri.orientation.yaw = -(e.alpha + e.beta);
25
tri.orientation.pitch = e.gamma - Math.sign(90 - Math.abs(e.beta)) * 90;
26
}
27
else {
28
tri.orientation.yaw = -(e.alpha + e.gamma);
29
tri.orientation.pitch = e.beta - 90;
30
}
31
tri.updateBody();
32
},
33
updateOrigin: function(e) {
34
document.body.style.transformOrigin = (innerWidth / 2 + pageXOffset) + "px " + (innerHeight / 2 + pageYOffset) + "px";
35
},
36
updateBody: function() {
37
document.body.style.transform = "perspective(" + Math.pow(2, tri.fov.value) + "px) translateZ(-" + tri.gap.value + "px) rotateX(" + tri.orientation.pitch + "deg) rotateY(" + tri.orientation.yaw + "deg)";
38
},
39
updateCSS: function() {
40
if (tri.non.checked)
41
tri.cssDynamic.innerHTML = "";
42
else if (tri.off.checked)
43
tri.cssDynamic.innerHTML = "* { transform-style: preserve-3d; }";
44
else {
45
for (var depth = 0; document.querySelector("body" + " > *".repeat(depth)); depth++);
46
var gap = tri.gap.value / depth;
47
var sag = -Math.PI * tri.sag.value / depth;
48
tri.cssDynamic.innerHTML = `
49
* {
50
transform: translateZ(${gap}px) rotateX(${sag}rad);
51
transform-style: preserve-3d;
52
transition: transform 1s;
53
outline: 1px solid rgba(0, 0, 0, 0.0625);
54
${tri.flo.checked ? "overflow: visible !important;" : ""}
55
}
56
*:hover {
57
transform: translateZ(${gap * 2}px) rotateX(${sag * 2}rad);
58
${!tri.flo.checked ? "overflow: visible;" : ""}
59
}
60
`;
61
}
62
},
63
toggle: function() {
64
if (tri.menu.className == "active") {
65
tri.menu.removeAttribute("class");
66
}
67
else {
68
tri.menu.className = "active";
69
}
70
},
71
quit: function() {
72
window.removeEventListener("deviceorientation", tri.gyroMove);
73
window.removeEventListener("mousemove", tri.mouseMove);
74
window.removeEventListener("scroll", tri.updateOrigin);
75
window.addEventListener("resize", tri.updateOrigin);
76
tri.menu.remove();
77
tri.cssStatic.remove();
78
tri.cssDynamic.remove();
79
document.body.removeAttribute("style");
80
},
81
newRange: function(e, label, min, step, max, value, f) {
82
tri.menu.appendChild(e);
83
e.type = "range";
84
e.min = min;
85
e.max = max;
86
e.step = step;
87
e.value = value;
88
e.addEventListener("input", f);
89
tri.menu.appendChild(document.createElement("span")).innerHTML = label;
90
tri.menu.appendChild(document.createElement("br"));
91
},
92
newCheckbox: function(e, label, f) {
93
tri.menu.appendChild(e);
94
e.type = "checkbox";
95
e.addEventListener("click", f);
96
tri.menu.appendChild(document.createElement("span")).innerHTML = label;
97
tri.menu.appendChild(document.createElement("br"));
98
},
99
newButton: function(e, label, f) {
100
tri.menu.appendChild(e);
101
e.type = "button";
102
e.value = label;
103
e.addEventListener("click", f);
104
},
105
init: function() {
106
document.body.parentNode.appendChild(tri.menu).id = "tri-menu";
107
tri.newRange(tri.limit, "limit", 0, 0.03125, 1, 0.125, tri.updateBody);
108
tri.newRange(tri.gap, "gap / distance", 0, 32, 512, 128, function() {
109
tri.updateCSS();
110
tri.updateBody();
111
});
112
tri.newRange(tri.sag, "sag", -0.25, 0.03125, 0.25, 0, tri.updateCSS);
113
tri.newRange(tri.fov, "field of view", 7, 1, 13, 10, tri.updateBody);
114
tri.newCheckbox(tri.flo, "force overflow", tri.updateCSS);
115
tri.flo.setAttribute("checked", "");
116
tri.newCheckbox(tri.off, "flatten layers", tri.updateCSS);
117
tri.newCheckbox(tri.non, "flatten everything", tri.updateCSS);
118
tri.newButton(tri.end, "Quit", tri.quit);
119
tri.newButton(tri.tgl, "≡", tri.toggle);
120
tri.tgl.id = "tri-toggle";
121
tri.menu.appendChild(tri.cssStatic).innerHTML = `
122
html, body {
123
transition-property: none;
124
height: 100%;
125
width: 100%;
126
}
127
html, html:hover, #tri-menu, #tri-menu > *, #tri-menu > *:hover {
128
transform: none;
129
outline: none;
130
overflow: auto !important;
131
float: none;
132
}
133
#tri-menu {
134
position: fixed;
135
top: 0;
136
left: 0;
137
background: rgba(0, 0, 0, 0.5);
138
color: white;
139
border: 1px solid rgba(255, 255, 255, 0.5);;
140
border-radius: 0 0 16px 0;
141
padding: 8px;
142
transform: translate(-100%, -100%) translate(32px, 32px);
143
}
144
#tri-menu.active {
145
transform: none;
146
}
147
#tri-toggle {
148
position: absolute;
149
bottom: 0;
150
right: 0;
151
height: 32px;
152
width: 32px;
153
background: transparent;
154
color: white;
155
border: none;
156
cursor: pointer;
157
}
158
#tri-menu.active > #tri-toggle {
159
background: white;
160
color: black;
161
border-radius: 8px 0 0 0;
162
}
163
`;
164
tri.menu.appendChild(tri.cssDynamic);
165
tri.updateCSS();
166
window.addEventListener("deviceorientation", tri.gyroMove);
167
window.addEventListener("mousemove", tri.mouseMove);
168
window.addEventListener("scroll", tri.updateOrigin);
169
window.addEventListener("resize", tri.updateOrigin);
170
window.scrollBy(0, 1);
171
}
172
};
173
tri.init();
174
})();
175
176