forked from MohmedIkram/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Javascript_animation
373 lines (338 loc) · 10.7 KB
/
Javascript_animation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery"], factory);
} else if (typeof module === "object" && module.exports) {
// Node/CommonJS
module.exports = function (root, jQuery) {
if (jQuery === undefined) {
// require('jQuery') returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it's defined (how jquery works)
if (typeof window !== "undefined") {
jQuery = require("jquery");
} else {
jQuery = require("jquery")(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser globals
factory(jQuery);
}
})(function ($) {
$.fn.tilt = function (options) {
/**
* RequestAnimationFrame
*/
const requestTick = function () {
if (this.ticking) return;
requestAnimationFrame(updateTransforms.bind(this));
this.ticking = true;
};
/**
* Bind mouse movement evens on instance
*/
const bindEvents = function () {
const _this = this;
$(this).on("mousemove", mouseMove);
$(this).on("mouseenter", mouseEnter);
if (this.settings.reset) $(this).on("mouseleave", mouseLeave);
if (this.settings.glare)
$(window).on("resize", updateGlareSize.bind(_this));
};
/**
* Set transition only on mouse leave and mouse enter so it doesn't influence mouse move transforms
*/
const setTransition = function () {
if (this.timeout !== undefined) clearTimeout(this.timeout);
$(this).css({
transition: `${this.settings.speed}ms ${this.settings.easing}`,
});
if (this.settings.glare)
this.glareElement.css({
transition: `opacity ${this.settings.speed}ms ${this.settings.easing}`,
});
this.timeout = setTimeout(() => {
$(this).css({ transition: "" });
if (this.settings.glare) this.glareElement.css({ transition: "" });
}, this.settings.speed);
};
/**
* When user mouse enters tilt element
*/
const mouseEnter = function (event) {
this.ticking = false;
$(this).css({ "will-change": "transform" });
setTransition.call(this);
// Trigger change event
$(this).trigger("tilt.mouseEnter");
};
/**
* Return the x,y position of the mouse on the tilt element
* @returns {{x: *, y: *}}
*/
const getMousePositions = function (event) {
if (typeof event === "undefined") {
event = {
pageX: $(this).offset().left + $(this).outerWidth() / 2,
pageY: $(this).offset().top + $(this).outerHeight() / 2,
};
}
return { x: event.pageX, y: event.pageY };
};
/**
* When user mouse moves over the tilt element
*/
const mouseMove = function (event) {
this.mousePositions = getMousePositions(event);
requestTick.call(this);
};
/**
* When user mouse leaves tilt element
*/
const mouseLeave = function () {
setTransition.call(this);
this.reset = true;
requestTick.call(this);
// Trigger change event
$(this).trigger("tilt.mouseLeave");
};
/**
* Get tilt values
*
* @returns {{x: tilt value, y: tilt value}}
*/
const getValues = function () {
const width = $(this).outerWidth();
const height = $(this).outerHeight();
const left = $(this).offset().left;
const top = $(this).offset().top;
const percentageX = (this.mousePositions.x - left) / width;
const percentageY = (this.mousePositions.y - top) / height;
// x or y position inside instance / width of instance = percentage of position inside instance * the max tilt value
const tiltX = (
this.settings.maxTilt / 2 -
percentageX * this.settings.maxTilt
).toFixed(2);
const tiltY = (
percentageY * this.settings.maxTilt -
this.settings.maxTilt / 2
).toFixed(2);
// angle
const angle =
Math.atan2(
this.mousePositions.x - (left + width / 2),
-(this.mousePositions.y - (top + height / 2))
) *
(180 / Math.PI);
// Return x & y tilt values
return {
tiltX,
tiltY,
percentageX: percentageX * 100,
percentageY: percentageY * 100,
angle,
};
};
/**
* Update tilt transforms on mousemove
*/
const updateTransforms = function () {
this.transforms = getValues.call(this);
if (this.reset) {
this.reset = false;
$(this).css(
"transform",
`perspective(${this.settings.perspective}px) rotateX(0deg) rotateY(0deg)`
);
// Rotate glare if enabled
if (this.settings.glare) {
this.glareElement.css(
"transform",
`rotate(180deg) translate(-50%, -50%)`
);
this.glareElement.css("opacity", `0`);
}
return;
} else {
$(this).css(
"transform",
`perspective(${this.settings.perspective}px) rotateX(${
this.settings.disableAxis === "x" ? 0 : this.transforms.tiltY
}deg) rotateY(${
this.settings.disableAxis === "y" ? 0 : this.transforms.tiltX
}deg) scale3d(${this.settings.scale},${this.settings.scale},${
this.settings.scale
})`
);
// Rotate glare if enabled
if (this.settings.glare) {
this.glareElement.css(
"transform",
`rotate(${this.transforms.angle}deg) translate(-50%, -50%)`
);
this.glareElement.css(
"opacity",
`${(this.transforms.percentageY * this.settings.maxGlare) / 100}`
);
}
}
// Trigger change event
$(this).trigger("change", [this.transforms]);
this.ticking = false;
};
/**
* Prepare elements
*/
const prepareGlare = function () {
const glarePrerender = this.settings.glarePrerender;
// If option pre-render is enabled we assume all html/css is present for an optimal glare effect.
if (!glarePrerender)
// Create glare element
$(this).append(
'<div class="js-tilt-glare"><div class="js-tilt-glare-inner"></div></div>'
);
// Store glare selector if glare is enabled
this.glareElementWrapper = $(this).find(".js-tilt-glare");
this.glareElement = $(this).find(".js-tilt-glare-inner");
// Remember? We assume all css is already set, so just return
if (glarePrerender) return;
// Abstracted re-usable glare styles
const stretch = {
position: "absolute",
top: "0",
left: "0",
width: "100%",
height: "100%",
};
// Style glare wrapper
this.glareElementWrapper.css(stretch).css({
overflow: "hidden",
"pointer-events": "none",
});
// Style glare element
this.glareElement.css({
position: "absolute",
top: "50%",
left: "50%",
"background-image": `linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)`,
width: `${$(this).outerWidth() * 2}`,
height: `${$(this).outerWidth() * 2}`,
transform: "rotate(180deg) translate(-50%, -50%)",
"transform-origin": "0% 0%",
opacity: "0",
});
};
/**
* Update glare on resize
*/
const updateGlareSize = function () {
this.glareElement.css({
width: `${$(this).outerWidth() * 2}`,
height: `${$(this).outerWidth() * 2}`,
});
};
/**
* Public methods
*/
$.fn.tilt.destroy = function () {
$(this).each(function () {
$(this).find(".js-tilt-glare").remove();
$(this).css({ "will-change": "", transform: "" });
$(this).off("mousemove mouseenter mouseleave");
});
};
$.fn.tilt.getValues = function () {
const results = [];
$(this).each(function () {
this.mousePositions = getMousePositions.call(this);
results.push(getValues.call(this));
});
return results;
};
$.fn.tilt.reset = function () {
$(this).each(function () {
this.mousePositions = getMousePositions.call(this);
this.settings = $(this).data("settings");
mouseLeave.call(this);
setTimeout(() => {
this.reset = false;
}, this.settings.transition);
});
};
/**
* Loop every instance
*/
return this.each(function () {
/**
* Default settings merged with user settings
* Can be set trough data attributes or as parameter.
* @type {*}
*/
this.settings = $.extend(
{
maxTilt: $(this).is("[data-tilt-max]")
? $(this).data("tilt-max")
: 20,
perspective: $(this).is("[data-tilt-perspective]")
? $(this).data("tilt-perspective")
: 300,
easing: $(this).is("[data-tilt-easing]")
? $(this).data("tilt-easing")
: "cubic-bezier(.03,.98,.52,.99)",
scale: $(this).is("[data-tilt-scale]")
? $(this).data("tilt-scale")
: "1",
speed: $(this).is("[data-tilt-speed]")
? $(this).data("tilt-speed")
: "400",
transition: $(this).is("[data-tilt-transition]")
? $(this).data("tilt-transition")
: true,
disableAxis: $(this).is("[data-tilt-disable-axis]")
? $(this).data("tilt-disable-axis")
: null,
axis: $(this).is("[data-tilt-axis]")
? $(this).data("tilt-axis")
: null,
reset: $(this).is("[data-tilt-reset]")
? $(this).data("tilt-reset")
: true,
glare: $(this).is("[data-tilt-glare]")
? $(this).data("tilt-glare")
: false,
maxGlare: $(this).is("[data-tilt-maxglare]")
? $(this).data("tilt-maxglare")
: 1,
},
options
);
// Add deprecation warning & set disableAxis to deprecated axis setting
if (this.settings.axis !== null) {
console.warn(
"Tilt.js: the axis setting has been renamed to disableAxis. See https://github.com/gijsroge/tilt.js/pull/26 for more information"
);
this.settings.disableAxis = this.settings.axis;
}
this.init = () => {
// Store settings
$(this).data("settings", this.settings);
// Prepare element
if (this.settings.glare) prepareGlare.call(this);
// Bind events
bindEvents.call(this);
};
// Init
this.init();
});
};
/**
* Auto load
*/
$("[data-tilt]").tilt();
return true;
});