-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathztext.js
More file actions
255 lines (213 loc) · 7.5 KB
/
ztext.js
File metadata and controls
255 lines (213 loc) · 7.5 KB
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
/*!
* ztext.js v0.0.2
* https://bennettfeely.com/ztext
* Licensed MIT | (c) 2020 Bennett Feely
*/
if (
CSS.supports("-moz-transform-style", "preserve-3d") ||
CSS.supports("-ms-transform-style", "preserve-3d") ||
CSS.supports("-webkit-transform-style", "preserve-3d") ||
CSS.supports("transform-style", "preserve-3d")
) {
// Default values
z_default = {
depth: "1rem",
direction: "both",
event: "none",
eventRotation: "30deg",
eventDirection: "default",
fade: false,
layers: 10,
perspective: "500px",
z: true,
};
// Get all elements with the [data-z] attribute
var zs = document.querySelectorAll("[data-z]");
zs.forEach((z) => {
// Make uniform option keys
options = {
depth: z.dataset.zDepth || z_default.depth,
direction: z.dataset.zDirection || z_default.direction,
event: z.dataset.zEvent || z_default.event,
eventRotation: z.dataset.zEventrotation || z_default.eventRotation,
eventDirection: z.dataset.zEventdirection || z_default.eventDirection,
fade: z.dataset.zFade || z_default.fade,
layers: parseFloat(z.dataset.zLayers) || z_default.layers,
perspective: z.dataset.zPerspective || z_default.perspective,
zEngaged: z.dataset.z || z_default.z,
};
zDraw(z, options);
});
// JS constructor
function Ztextify(selector, options) {
var zs = document.querySelectorAll(selector);
zs.forEach((z) => {
zDraw(z, options);
});
}
function zDraw(z, options) {
var z_engaged = options.zEngaged || z_default.zEngaged;
if (z_engaged !== "false") {
var depth = options.depth || z_default.depth;
var depth_unit = depth.match(/[a-z]+/)[0];
var depth_numeral = parseFloat(depth.replace(depth_unit, ""));
var direction = options.direction || z_default.direction;
var event = options.event || z_default.event;
var event_rotation = options.eventRotation || z_default.eventRotation;
var event_rotation_unit = event_rotation.match(/[a-z]+/)[0];
var event_rotation_numeral = parseFloat(
event_rotation.replace(event_rotation_unit, "")
);
var event_direction = options.eventDirection || z_default.eventDirection;
var fade = options.fade || z_default.fade;
var layers = options.layers || z_default.layers;
var perspective = options.perspective || z_default.perspective;
var transform = options.transform || z_default.transform;
// Grab the text and replace it with a new structure
var text = z.innerHTML;
z.innerHTML = "";
z.style.display = "inline-block";
z.style.position = "relative";
z.style.webkitPerspective = perspective;
z.style.perspective = perspective;
// Create a wrapper span that will hold all the layers
var zText = document.createElement("span");
zText.setAttribute("class", "z-text");
zText.style.display = "inline-block";
zText.style.webkitTransformStyle = "preserve-3d";
zText.style.transformStyle = "preserve-3d";
// Create a layer for transforms from JS to be applied
// CSS is stupid that transforms cannot be applied individually
var zLayers = document.createElement("span");
zLayers.setAttribute("class", "z-layers");
zLayers.style.display = "inline-block";
zLayers.style.webkitTransformStyle = "preserve-3d";
zLayers.style.transformStyle = "preserve-3d";
zText.append(zLayers);
for (i = 0; i < layers; i++) {
let pct = i / layers;
// Create a layer
var zLayer = document.createElement("span");
zLayer.setAttribute("class", "z-layer");
zLayer.innerHTML = text;
zLayer.style.display = "inline-block";
// Shift the layer on the z axis
if (direction === "backwards") {
var zTranslation = -pct * depth_numeral;
}
if (direction === "both") {
var zTranslation = -(pct * depth_numeral) + depth_numeral / 2;
}
if (direction === "forwards") {
var zTranslation = -(pct * depth_numeral) + depth_numeral;
}
var transform = "translateZ(" + zTranslation + depth_unit + ")";
zLayer.style.webkitTransform = transform;
zLayer.style.transform = transform;
// Manipulate duplicate layers
if (i >= 1) {
// Overlay duplicate layers on top of each other
zLayer.style.position = "absolute";
zLayer.style.top = 0;
zLayer.style.left = 0;
// Hide duplicate layres from screen readers and user interation
zLayer.setAttribute("aria-hidden", "true");
zLayer.style.pointerEvents = "none";
zLayer.style.mozUserSelect = "none";
zLayer.style.msUserSelect = "none";
zLayer.style.webkitUserSelect = "none";
zLayer.style.userSelect = "none";
// Incrementally fade layers if option is enabled
if (fade === true || fade === "true") {
zLayer.style.opacity = (1 - pct) / 2;
}
}
// Add layer to wrapper span
zLayers.append(zLayer);
}
// Finish adding everything to the original element
z.append(zText);
function tilt(x_pct, y_pct) {
// Switch neg/pos values if eventDirection is reversed
if (event_direction == "reverse") {
var event_direction_adj = -1;
} else {
var event_direction_adj = 1;
}
// Multiply pct rotation by eventRotation and eventDirection
var x_tilt = x_pct * event_rotation_numeral * event_direction_adj;
var y_tilt = -y_pct * event_rotation_numeral * event_direction_adj;
// Keep values in bounds [-1, 1]
var x_clamped = Math.min(Math.max(x_tilt, -1), 1);
var y_clamped = Math.min(Math.max(y_tilt, -1), 1);
// Add unit to transform value
var unit = event_rotation_unit;
// Rotate .z-layers as a function of x and y coordinates
var transform =
"rotateX(" + y_tilt + unit + ") rotateY(" + x_tilt + unit + ")";
zLayers.style.webkitTransform = transform;
zLayers.style.transform = transform;
}
// Capture mousemove and touchmove events and rotate .z-layers
if (event === "pointer") {
window.addEventListener(
"mousemove",
(e) => {
var x_pct = (e.clientX / window.innerWidth - 0.5) * 2;
var y_pct = (e.clientY / window.innerHeight - 0.5) * 2;
tilt(x_pct, y_pct);
},
false
);
window.addEventListener(
"touchmove",
(e) => {
var x_pct = (e.touches[0].clientX / window.innerWidth - 0.5) * 2;
var y_pct = (e.touches[0].clientY / window.innerHeight - 0.5) * 2;
tilt(x_pct, y_pct);
},
false
);
}
// Capture scroll event and rotate .z-layers
if (event == "scroll") {
function zScroll() {
var bounds = z.getBoundingClientRect();
var center_x = bounds.left + bounds.width / 2 - window.innerWidth / 2;
var center_y =
bounds.top + bounds.height / 2 - window.innerHeight / 2;
var x_pct = (center_x / window.innerWidth) * -2;
var y_pct = (center_y / window.innerHeight) * -2;
tilt(x_pct, y_pct);
}
zScroll();
window.addEventListener("scroll", zScroll, false);
}
if (event == "scrollY") {
function zScrollY() {
var bounds = z.getBoundingClientRect();
var center_y =
bounds.top + bounds.height / 2 - window.innerHeight / 2;
var y_pct = (center_y / window.innerHeight) * -2;
tilt(0, y_pct);
}
zScrollY();
window.addEventListener("scroll", zScrollY, false);
}
if (event == "scrollX") {
function zScrollX() {
var bounds = z.getBoundingClientRect();
var center_x = bounds.left + bounds.width / 2 - window.innerWidth / 2;
var x_pct = (center_x / window.innerWidth) * -2;
tilt(x_pct, 0);
}
zScrollX();
window.addEventListener("scroll", zScrollX, false);
}
}
}
} else {
console.error(
"ztext is disabled because transform-style: preserve-3d; is unsupported"
);
}