* Animation loop that applies the tilt effect based on the cursor position.
()
| 62 | * Animation loop that applies the tilt effect based on the cursor position. |
| 63 | */ |
| 64 | render() { |
| 65 | // Interpolate the current transform values towards the target values |
| 66 | // based on the cursor's position on the screen |
| 67 | this.imgTransforms.x = lerp(this.imgTransforms.x, map(cursor.x, 0, winsize.width, this.options.valuesFromTo.x[0], this.options.valuesFromTo.x[1]), this.options.amt); |
| 68 | this.imgTransforms.y = lerp(this.imgTransforms.y, map(cursor.y, 0, winsize.height, this.options.valuesFromTo.y[0], this.options.valuesFromTo.y[1]), this.options.amt); |
| 69 | this.imgTransforms.rz = lerp(this.imgTransforms.rz, map(cursor.x, 0, winsize.width, this.options.valuesFromTo.rz[0], this.options.valuesFromTo.rz[1]), this.options.amt); |
| 70 | |
| 71 | // Apply rotation on the X and Y-axis only if perspective is enabled |
| 72 | this.imgTransforms.rx = !this.options.perspective ? 0 : lerp(this.imgTransforms.rx, map(cursor.y, 0, winsize.height, this.options.valuesFromTo.rx[0], this.options.valuesFromTo.rx[1]), this.options.amt); |
| 73 | this.imgTransforms.ry = !this.options.perspective ? 0 : lerp(this.imgTransforms.ry, map(cursor.x, 0, winsize.width, this.options.valuesFromTo.ry[0], this.options.valuesFromTo.ry[1]), this.options.amt); |
| 74 | |
| 75 | // Apply the calculated transform values to the wrap element to create the 3D tilt effect |
| 76 | this.DOM.wrapEl.style.transform = `translateX(${this.imgTransforms.x}px) translateY(${this.imgTransforms.y}px) rotateX(${this.imgTransforms.rx}deg) rotateY(${this.imgTransforms.ry}deg) rotateZ(${this.imgTransforms.rz}deg)`; |
| 77 | |
| 78 | // Continue the loop with the next animation frame |
| 79 | requestAnimationFrame(() => this.render()); |
| 80 | } |
| 81 | } |
no test coverage detected