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 283 return { 284 tiltX: tiltX, 285 tiltY: tiltY, 286 percentageX: x * 100, 287 percentageY: y * 100, 288 angle: angle 289 }; 290 } 291 292 updateElementPosition() { 293 let rect = this.element.getBoundingClientRect(); 294 295 this.width = this.element.offsetWidth; 296 this.height = this.element.offsetHeight; 297 this.left = rect.left; 298 this.top = rect.top; 299 } 300 301 update() { 302 let values = this.getValues(); 303 304 this.element.style.transform = "perspective(" + this.settings.perspective + "px) " + 305 "rotateX(" + (this.settings.axis === "x" ? 0 : values.tiltY) + "deg) " + 306 "rotateY(" + (this.settings.axis === "y" ? 0 : values.tiltX) + "deg) " + 307 "scale3d(" + this.settings.scale + ", " + this.settings.scale + ", " + this.settings.scale + ")"; 308 309 if (this.glare) { 310 this.glareElement.style.transform = `rotate(${values.angle}deg) translate(-50%, -50%)`; 311 this.glareElement.style.opacity = `${values.percentageY * this.settings["max-glare"] / 100}`; 312 } 313 314 this.element.dispatchEvent(new CustomEvent("tiltChange", { 315 "detail": values 316 })); 317 318 this.updateCall = null; 319 } 320 321 /** 322 * Appends the glare element (if glarePrerender equals false) 323 * and sets the default style 324 */ 325 prepareGlare() { 326 // If option pre-render is enabled we assume all html/css is present for an optimal glare effect. 327 if (!this.glarePrerender) { 328 // Create glare element 329 const jsTiltGlare = document.createElement("div"); 330 jsTiltGlare.classList.add("js-tilt-glare"); 331 332 const jsTiltGlareInner = document.createElement("div"); 333 jsTiltGlareInner.classList.add("js-tilt-glare-inner"); 334 335 jsTiltGlare.appendChild(jsTiltGlareInner); 336 this.element.appendChild(jsTiltGlare); 337 } 338 339 this.glareElementWrapper = this.element.querySelector(".js-tilt-glare"); 340 this.glareElement = this.element.querySelector(".js-tilt-glare-inner"); 341 342 if (this.glarePrerender) { 343 return; 344 } 345 346 Object.assign(this.glareElementWrapper.style, { 347 "position": "absolute", 348 "top": "0", 349 "left": "0", 350 "width": "100%", 351 "height": "100%", 352 "overflow": "hidden", 353 "pointer-events": "none" 354 }); 355 356 Object.assign(this.glareElement.style, { 357 "position": "absolute", 358 "top": "50%", 359 "left": "50%", 360 "pointer-events": "none", 361 "background-image": `linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)`, 362 "width": `${this.element.offsetWidth * 2}px`, 363 "height": `${this.element.offsetWidth * 2}px`, 364 "transform": "rotate(180deg) translate(-50%, -50%)", 365 "transform-origin": "0% 0%", 366 "opacity": "0", 367 }); 368 } 369 370 updateGlareSize() { 371 if (this.glare) { 372 Object.assign(this.glareElement.style, { 373 "width": `${this.element.offsetWidth * 2}`, 374 "height": `${this.element.offsetWidth * 2}`, 375 }); 376 } 377 } 378 379 updateClientSize() { 380 this.clientWidth = window.innerWidth 381 || document.documentElement.clientWidth 382 || document.body.clientWidth; 383 384 this.clientHeight = window.innerHeight 385 || document.documentElement.clientHeight 386 || document.body.clientHeight; 387 } 388 389 onWindowResize() { 390 this.updateGlareSize(); 391 this.updateClientSize(); 392 } 393 394 setTransition() { 395 clearTimeout(this.transitionTimeout); 396 this.element.style.transition = this.settings.speed + "ms " + this.settings.easing; 397 if (this.glare) this.glareElement.style.transition = `opacity ${this.settings.speed}ms ${this.settings.easing}`; 398 399 this.transitionTimeout = setTimeout(() => { 400 this.element.style.transition = ""; 401 if (this.glare) { 402 this.glareElement.style.transition = ""; 403 } 404 }, this.settings.speed); 405 406 } 407 408 /** 409 * Method return patched settings of instance 410 * @param {boolean} settings.reverse - reverse the tilt direction 411 * @param {number} settings.max - max tilt rotation (degrees) 412 * @param {startX} settings.startX - the starting tilt on the X axis, in degrees. Default: 0 413 * @param {startY} settings.startY - the starting tilt on the Y axis, in degrees. Default: 0 414 * @param {number} settings.perspective - Transform perspective, the lower the more extreme the tilt gets 415 * @param {string} settings.easing - Easing on enter/exit 416 * @param {number} settings.scale - 2 = 200%, 1.5 = 150%, etc.. 417 * @param {number} settings.speed - Speed of the enter/exit transition 418 * @param {boolean} settings.transition - Set a transition on enter/exit 419 * @param {string|null} settings.axis - What axis should be disabled. Can be X or Y 420 * @param {boolean} settings.glare - What axis should be disabled. Can be X or Y 421 * @param {number} settings.max-glare - the maximum "glare" opacity (1 = 100%, 0.5 = 50%) 422 * @param {boolean} settings.glare-prerender - false = VanillaTilt creates the glare elements for you, otherwise 423 * @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 424 * @param {string|object} settings.mouse-event-element - String selector or link to HTML-element what will be listen mouse events 425 * @param {boolean} settings.reset - false = If the tilt effect has to be reset on exit 426 * @param {gyroscope} settings.gyroscope - Enable tilting by deviceorientation events 427 * @param {gyroscopeSensitivity} settings.gyroscopeSensitivity - Between 0 and 1 - The angle at which max tilt position is reached. 1 = 90deg, 0.5 = 45deg, etc.. 428 * @param {gyroscopeSamples} settings.gyroscopeSamples - How many gyroscope moves to decide the starting position. 429 */ 430 extendSettings(settings) { 431 let defaultSettings = { 432 reverse: false, 433 max: 15, 434 startX: 0, 435 startY: 0, 436 perspective: 1000, 437 easing: "cubic-bezier(.03,.98,.52,.99)", 438 scale: 1, 439 speed: 300, 440 transition: true, 441 axis: null, 442 glare: false, 443 "max-glare": 1, 444 "glare-prerender": false, 445 "full-page-listening": false, 446 "mouse-event-element": null, 447 reset: true, 448 gyroscope: true, 449 gyroscopeMinAngleX: -45, 450 gyroscopeMaxAngleX: 45, 451 gyroscopeMinAngleY: -45, 452 gyroscopeMaxAngleY: 45, 453 gyroscopeSamples: 10 454 }; 455 456 let newSettings = {}; 457 for (var property in defaultSettings) { 458 if (property in settings) { 459 newSettings[property] = settings[property]; 460 } else if (this.element.hasAttribute("data-tilt-" + property)) { 461 let attribute = this.element.getAttribute("data-tilt-" + property); 462 try { 463 newSettings[property] = JSON.parse(attribute); 464 } catch (e) { 465 newSettings[property] = attribute; 466 } 467 468 } else { 469 newSettings[property] = defaultSettings[property]; 470 } 471 } 472 473 return newSettings; 474 } 475 476 static init(elements, settings) { 477 if (elements instanceof Node) { 478 elements = [elements]; 479 } 480 481 if (elements instanceof NodeList) { 482 elements = [].slice.call(elements); 483 } 484 485 if (!(elements instanceof Array)) { 486 return; 487 } 488
489 elements.forEach((element) => { 490 if (!("vanillaTilt" in element)) { 491 element.vanillaTilt = new VanillaTilt(element, settings); 492 } 493 }); 494 } 495} 496 497if (typeof document !== "undefined") { 498 /* expose the class to window */ 499 window.VanillaTilt = VanillaTilt; 500 501 /** 502 * Auto load 503 */ 504 VanillaTilt.init(document.querySelectorAll("[data-tilt]")); 505} 506 507return VanillaTilt; 508 509}());
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.