vendor: 15,132 bytes, lines 1-487
1var VanillaTilt = (function () { 2'use strict'; 3 4/** 5 * Created by Sergiu Èandor (micku7zu) on 1/27/2017. 6 * Original idea: https://github.com/gijsroge/tilt.js 7 * MIT License. 8 * Version 1.7.0 9 */ 10 11class VanillaTilt { 12 constructor(element, settings = {}) { 13 if (!(element instanceof Node)) { 14 throw ("Can't initialize VanillaTilt because " + element + " is not a Node."); 15 } 16 17 this.width = null; 18 this.height = null; 19 this.clientWidth = null; 20 this.clientHeight = null; 21 this.left = null; 22 this.top = null; 23 24 // for Gyroscope sampling 25 this.gammazero = null; 26 this.betazero = null; 27 this.lastgammazero = null; 28 this.lastbetazero = null; 29 30 this.transitionTimeout = null; 31 this.updateCall = null; 32 this.event = null; 33 34 this.updateBind = this.update.bind(this); 35 this.resetBind = this.reset.bind(this); 36 37 this.element = element; 38 this.settings = this.extendSettings(settings); 39 40 this.reverse = this.settings.reverse ? -1 : 1; 41 this.glare = VanillaTilt.isSettingTrue(this.settings.glare); 42 this.glarePrerender = VanillaTilt.isSettingTrue(this.settings["glare-prerender"]); 43 this.fullPageListening = VanillaTilt.isSettingTrue(this.settings["full-page-listening"]); 44 this.gyroscope = VanillaTilt.isSettingTrue(this.settings.gyroscope); 45 this.gyroscopeSamples = this.settings.gyroscopeSamples; 46 47 this.elementListener = this.getElementListener(); 48 49 if (this.glare) { 50 this.prepareGlare(); 51 } 52 53 if (this.fullPageListening) { 54 this.updateClientSize(); 55 } 56 57 this.addEventListeners(); 58 this.updateInitialPosition(); 59 } 60 61 static isSettingTrue(setting) { 62 return setting === "" || setting === true || setting === 1; 63 } 64 65 /** 66 * Method returns element what will be listen mouse events 67 * @return {Node} 68 */ 69 getElementListener() { 70 if (this.fullPageListening) { 71 return window.document; 72 } 73 74 if (typeof this.settings["mouse-event-element"] === "string") { 75 const mouseEventElement = document.querySelector(this.settings["mouse-event-element"]); 76 77 if (mouseEventElement) { 78 return mouseEventElement; 79 } 80 } 81 82 if (this.settings["mouse-event-element"] instanceof Node) { 83 return this.settings["mouse-event-element"]; 84 } 85 86 return this.element; 87 } 88 89 /** 90 * Method set listen methods for this.elementListener 91 * @return {Node} 92 */ 93 addEventListeners() { 94 this.onMouseEnterBind = this.onMouseEnter.bind(this); 95 this.onMouseMoveBind = this.onMouseMove.bind(this); 96 this.onMouseLeaveBind = this.onMouseLeave.bind(this); 97 this.onWindowResizeBind = this.onWindowResize.bind(this); 98 this.onDeviceOrientationBind = this.onDeviceOrientation.bind(this); 99 100 this.elementListener.addEventListener("mouseenter", this.onMouseEnterBind); 101 this.elementListener.addEventListener("mouseleave", this.onMouseLeaveBind); 102 this.elementListener.addEventListener("mousemove", this.onMouseMoveBind); 103 104 if (this.glare || this.fullPageListening) { 105 window.addEventListener("resize", this.onWindowResizeBind); 106 } 107 108 if (this.gyroscope) { 109 window.addEventListener("deviceorientation", this.onDeviceOrientationBind); 110 } 111 } 112 113 /** 114 * Method remove event listeners from current this.elementListener 115 */ 116 removeEventListeners() { 117 this.elementListener.removeEventListener("mouseenter", this.onMouseEnterBind); 118 this.elementListener.removeEventListener("mouseleave", this.onMouseLeaveBind); 119 this.elementListener.removeEventListener("mousemove", this.onMouseMoveBind); 120 121 if (this.gyroscope) { 122 window.removeEventListener("deviceorientation", this.onDeviceOrientationBind); 123 } 124 125 if (this.glare || this.fullPageListening) { 126 window.removeEventListener("resize", this.onWindowResizeBind); 127 } 128 } 129 130 destroy() { 131 clearTimeout(this.transitionTimeout); 132 if (this.updateCall !== null) { 133 cancelAnimationFrame(this.updateCall); 134 } 135 136 this.reset(); 137 138 this.removeEventListeners(); 139 this.element.vanillaTilt = null; 140 delete this.element.vanillaTilt; 141 142 this.element = null; 143 } 144 145 onDeviceOrientation(event) { 146 if (event.gamma === null || event.beta === null) { 147 return; 148 } 149 150 this.updateElementPosition(); 151 152 if (this.gyroscopeSamples > 0) { 153 this.lastgammazero = this.gammazero; 154 this.lastbetazero = this.betazero; 155 156 if (this.gammazero === null) { 157 this.gammazero = event.gamma; 158 this.betazero = event.beta; 159 } else { 160 this.gammazero = (event.gamma + this.lastgammazero) / 2; 161 this.betazero = (event.beta + this.lastbetazero) / 2; 162 } 163 164 this.gyroscopeSamples -= 1; 165 } 166 167 const totalAngleX = this.settings.gyroscopeMaxAngleX - this.settings.gyroscopeMinAngleX; 168 const totalAngleY = this.settings.gyroscopeMaxAngleY - this.settings.gyroscopeMinAngleY; 169 170 const degreesPerPixelX = totalAngleX / this.width; 171 const degreesPerPixelY = totalAngleY / this.height; 172 173 const angleX = event.gamma - (this.settings.gyroscopeMinAngleX + this.gammazero); 174 const angleY = event.beta - (this.settings.gyroscopeMinAngleY + this.betazero); 175 176 const posX = angleX / degreesPerPixelX; 177 const posY = angleY / degreesPerPixelY; 178 179 if (this.updateCall !== null) { 180 cancelAnimationFrame(this.updateCall); 181 } 182 183 this.event = { 184 clientX: posX + this.left, 185 clientY: posY + this.top, 186 }; 187 188 this.updateCall = requestAnimationFrame(this.updateBind); 189 } 190 191 onMouseEnter() { 192 this.updateElementPosition(); 193 this.element.style.willChange = "transform"; 194 this.setTransition(); 195 } 196 197 onMouseMove(event) { 198 if (this.updateCall !== null) { 199 cancelAnimationFrame(this.updateCall); 200 } 201 202 this.event = event; 203 this.updateCall = requestAnimationFrame(this.updateBind); 204 } 205 206 onMouseLeave() { 207 this.setTransition(); 208 209 if (this.settings.reset) { 210 requestAnimationFrame(this.resetBind); 211 } 212 } 213 214 reset() { 215 this.event = { 216 clientX: this.left + this.width / 2, 217 clientY: this.top + this.height / 2 218 }; 219 220 if (this.element && this.element.style) { 221 this.element.style.transform = `perspective(${this.settings.perspective}px) ` + 222 `rotateX(0deg) ` + 223 `rotateY(0deg) ` + 224 `scale3d(1, 1, 1)`; 225 } 226 227 this.resetGlare(); 228 } 229 230 resetGlare() { 231 if (this.glare) { 232 this.glareElement.style.transform = "rotate(180deg) translate(-50%, -50%)"; 233 this.glareElement.style.opacity = "0"; 234 } 235 } 236 237 updateInitialPosition() { 238 if (this.settings.startX === 0 && this.settings.startY === 0) { 239 return; 240 } 241 242 this.onMouseEnter(); 243 244 if (this.fullPageListening) { 245 this.event = { 246 clientX: (this.settings.startX + this.settings.max) / (2 * this.settings.max) * this.clientWidth, 247 clientY: (this.settings.startY + this.settings.max) / (2 * this.settings.max) * this.clientHeight 248 }; 249 } else { 250 this.event = { 251 clientX: this.left + ((this.settings.startX + this.settings.max) / (2 * this.settings.max) * this.width), 252 clientY: this.top + ((this.settings.startY + this.settings.max) / (2 * this.settings.max) * this.height) 253 }; 254 } 255 256 257 let backupScale = this.settings.scale; 258 this.settings.scale = 1; 259 this.update(); 260 this.settings.scale = backupScale; 261 this.resetGlare(); 262 } 263 264 getValues() { 265 let x, y; 266 267 if (this.fullPageListening) { 268 x = this.event.clientX / this.clientWidth; 269 y = this.event.clientY / this.clientHeight; 270 } else { 271 x = (this.event.clientX - this.left) / this.width; 272 y = (this.event.clientY - this.top) / this.height; 273 } 274 275 x = Math.min(Math.max(x, 0), 1); 276 y = Math.min(Math.max(y, 0), 1); 277 278 let tiltX = (this.reverse * (this.settings.max - x * this.settings.max * 2)).toFixed(2); 279 let tiltY = (this.reverse * (y * this.settings.max * 2 - this.settings.max)).toFixed(2); 280 let angle = Math.atan2(this.event.clientX - (this.left + this.width / 2), -(this.event.clientY - (this.top + this.height / 2))) * (180 / Math.PI); 281 282 return { 283 tiltX: tiltX, 284 tiltY: tiltY, 285 percentageX: x * 100, 286 percentageY: y * 100, 287 angle: angle 288 }; 289 } 290 291 updateElementPosition() { 292 let rect = this.element.getBoundingClientRect(); 293 294 this.width = this.element.offsetWidth; 295 this.height = this.element.offsetHeight; 296 this.left = rect.left; 297 this.top = rect.top; 298 } 299 300 update() { 301 let values = this.getValues(); 302 303 this.element.style.transform = "perspective(" + this.settings.perspective + "px) " + 304 "rotateX(" + (this.settings.axis === "x" ? 0 : values.tiltY) + "deg) " + 305 "rotateY(" + (this.settings.axis === "y" ? 0 : values.tiltX) + "deg) " + 306 "scale3d(" + this.settings.scale + ", " + this.settings.scale + ", " + this.settings.scale + ")"; 307 308 if (this.glare) { 309 this.glareElement.style.transform = `rotate(${values.angle}deg) translate(-50%, -50%)`; 310 this.glareElement.style.opacity = `${values.percentageY * this.settings["max-glare"] / 100}`; 311 } 312 313 this.element.dispatchEvent(new CustomEvent("tiltChange", { 314 "detail": values 315 })); 316 317 this.updateCall = null; 318 } 319 320 /** 321 * Appends the glare element (if glarePrerender equals false) 322 * and sets the default style 323 */ 324 prepareGlare() { 325 // If option pre-render is enabled we assume all html/css is present for an optimal glare effect. 326 if (!this.glarePrerender) { 327 // Create glare element 328 const jsTiltGlare = document.createElement("div"); 329 jsTiltGlare.classList.add("js-tilt-glare"); 330 331 const jsTiltGlareInner = document.createElement("div"); 332 jsTiltGlareInner.classList.add("js-tilt-glare-inner"); 333 334 jsTiltGlare.appendChild(jsTiltGlareInner); 335 this.element.appendChild(jsTiltGlare); 336 } 337 338 this.glareElementWrapper = this.element.querySelector(".js-tilt-glare"); 339 this.glareElement = this.element.querySelector(".js-tilt-glare-inner"); 340 341 if (this.glarePrerender) { 342 return; 343 } 344 345 Object.assign(this.glareElementWrapper.style, { 346 "position": "absolute", 347 "top": "0", 348 "left": "0", 349 "width": "100%", 350 "height": "100%", 351 "overflow": "hidden", 352 "pointer-events": "none" 353 }); 354 355 Object.assign(this.glareElement.style, { 356 "position": "absolute", 357 "top": "50%", 358 "left": "50%", 359 "pointer-events": "none", 360 "background-image": `linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)`, 361 "width": `${this.element.offsetWidth * 2}px`, 362 "height": `${this.element.offsetWidth * 2}px`, 363 "transform": "rotate(180deg) translate(-50%, -50%)", 364 "transform-origin": "0% 0%", 365 "opacity": "0", 366 }); 367 } 368 369 updateGlareSize() { 370 if (this.glare) { 371 Object.assign(this.glareElement.style, { 372 "width": `${this.element.offsetWidth * 2}`, 373 "height": `${this.element.offsetWidth * 2}`, 374 }); 375 } 376 } 377 378 updateClientSize() { 379 this.clientWidth = window.innerWidth 380 || document.documentElement.clientWidth 381 || document.body.clientWidth; 382 383 this.clientHeight = window.innerHeight 384 || document.documentElement.clientHeight 385 || document.body.clientHeight; 386 } 387 388 onWindowResize() { 389 this.updateGlareSize(); 390 this.updateClientSize(); 391 } 392 393 setTransition() { 394 clearTimeout(this.transitionTimeout); 395 this.element.style.transition = this.settings.speed + "ms " + this.settings.easing; 396 if (this.glare) this.glareElement.style.transition = `opacity ${this.settings.speed}ms ${this.settings.easing}`; 397 398 this.transitionTimeout = setTimeout(() => { 399 this.element.style.transition = ""; 400 if (this.glare) { 401 this.glareElement.style.transition = ""; 402 } 403 }, this.settings.speed); 404 405 } 406 407 /** 408 * Method return patched settings of instance 409 * @param {boolean} settings.reverse - reverse the tilt direction 410 * @param {number} settings.max - max tilt rotation (degrees) 411 * @param {startX} settings.startX - the starting tilt on the X axis, in degrees. Default: 0 412 * @param {startY} settings.startY - the starting tilt on the Y axis, in degrees. Default: 0 413 * @param {number} settings.perspective - Transform perspective, the lower the more extreme the tilt gets 414 * @param {string} settings.easing - Easing on enter/exit 415 * @param {number} settings.scale - 2 = 200%, 1.5 = 150%, etc.. 416 * @param {number} settings.speed - Speed of the enter/exit transition 417 * @param {boolean} settings.transition - Set a transition on enter/exit 418 * @param {string|null} settings.axis - What axis should be disabled. Can be X or Y 419 * @param {boolean} settings.glare - What axis should be disabled. Can be X or Y 420 * @param {number} settings.max-glare - the maximum "glare" opacity (1 = 100%, 0.5 = 50%) 421 * @param {boolean} settings.glare-prerender - false = VanillaTilt creates the glare elements for you, otherwise 422 * @param {boolean} settings.full-page-listening - If true, parallax effect will listen to mouse move events on the whole document, not only the selected element 423 * @param {string|object} settings.mouse-event-element - String selector or link to HTML-element what will be listen mouse events 424 * @param {boolean} settings.reset - false = If the tilt effect has to be reset on exit 425 * @param {gyroscope} settings.gyroscope - Enable tilting by deviceorientation events 426 * @param {gyroscopeSensitivity} settings.gyroscopeSensitivity - Between 0 and 1 - The angle at which max tilt position is reached. 1 = 90deg, 0.5 = 45deg, etc.. 427 * @param {gyroscopeSamples} settings.gyroscopeSamples - How many gyroscope moves to decide the starting position. 428 */ 429 extendSettings(settings) { 430 let defaultSettings = { 431 reverse: false, 432 max: 15, 433 startX: 0, 434 startY: 0, 435 perspective: 1000, 436 easing: "cubic-bezier(.03,.98,.52,.99)", 437 scale: 1, 438 speed: 300, 439 transition: true, 440 axis: null, 441 glare: false, 442 "max-glare": 1, 443 "glare-prerender": false, 444 "full-page-listening": false, 445 "mouse-event-element": null, 446 reset: true, 447 gyroscope: true, 448 gyroscopeMinAngleX: -45, 449 gyroscopeMaxAngleX: 45, 450 gyroscopeMinAngleY: -45, 451 gyroscopeMaxAngleY: 45, 452 gyroscopeSamples: 10 453 }; 454 455 let newSettings = {}; 456 for (var property in defaultSettings) { 457 if (property in settings) { 458 newSettings[property] = settings[property]; 459 } else if (this.element.hasAttribute("data-tilt-" + property)) { 460 let attribute = this.element.getAttribute("data-tilt-" + property); 461 try { 462 newSettings[property] = JSON.parse(attribute); 463 } catch (e) { 464 newSettings[property] = attribute; 465 } 466 467 } else { 468 newSettings[property] = defaultSettings[property]; 469 } 470 } 471 472 return newSettings; 473 } 474 475 static init(elements, settings) { 476 if (elements instanceof Node) { 477 elements = [elements]; 478 } 479 480 if (elements instanceof NodeList) { 481 elements = [].slice.call(elements); 482 } 483 484 if (!(elements instanceof Array)) { 485 return; 486 } 487
488 elements.forEach((element) => { 489 if (!("vanillaTilt" in element)) { 490 element.vanillaTilt = new VanillaTilt(element, settings); 491 } 492 }); 493 } 494} 495 496if (typeof document !== "undefined") { 497 /* expose the class to window */ 498 window.VanillaTilt = VanillaTilt; 499 500 /** 501 * Auto load 502 */ 503 VanillaTilt.init(document.querySelectorAll("[data-tilt]")); 504} 505 506return VanillaTilt; 507 508}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.