From 13fd6a77ef7c3c97491c20e9232875e421018222 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 28 Oct 2021 18:28:36 +0200 Subject: [PATCH] wait #4835 @0.5 --- js/libs/fluidbook/fluidbook.parallax.js | 71 +++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 js/libs/fluidbook/fluidbook.parallax.js diff --git a/js/libs/fluidbook/fluidbook.parallax.js b/js/libs/fluidbook/fluidbook.parallax.js new file mode 100644 index 00000000..0e95dc74 --- /dev/null +++ b/js/libs/fluidbook/fluidbook.parallax.js @@ -0,0 +1,71 @@ +function FluidbookParallax(fluidbook) { + this.fluidbook = fluidbook; + this.init(); +} + +FluidbookParallax.prototype = { + frame: function (timestamp) { + var $this = this; + var progress = timestamp - this.lastFrame; + this.lastFrame = timestamp; + + $("[data-parallax]").each(function () { + var currentX = parseFloat($(this).css('marginLeft')); + var currentY = parseFloat($(this).css('marginTop')); + var friction = 5; + var d = friction * progress / 1000; + + $(this).css({ + marginTop: currentY + ($(this).data('parallax-dest-y') - currentY) * d, + marginLeft: currentX + ($(this).data('parallax-dest-x') - currentX) * d + }) + }); + + requestAnimationFrame(function (timestamp) { + $this.frame(timestamp); + }); + }, + + init: function () { + var $this = this; + this.lastFrame = new Date().getTime(); + requestAnimationFrame(function (timestamp) { + $this.frame(timestamp); + }); + + this.x = 0; + this.y = 0; + this.update(); + + if (Modernizr.hasEvent('deviceOrientation')) { + + } else { + $(window).on('pointermove', function (e) { + $this.x = -1 * $this.normalize((e.clientX - $this.fluidbook.resize.ww / 2) / $this.fluidbook.resize.ww); + $this.y = -1 * $this.normalize((e.clientY - $this.fluidbook.resize.hh / 2) / $this.fluidbook.resize.hh); + $this.update(); + }); + } + }, + + update: function () { + var $this = this; + $("[data-parallax]").each(function () { + var p = parseFloat($(this).data('parallax')); + var scale = 0.25; + var destx = scale * p * $this.x * $this.fluidbook.settings.width; + var desty = scale * p * $this.y * $this.fluidbook.settings.height; + $(this).data('parallax-dest-x', destx); + $(this).data('parallax-dest-y', desty); + }); + }, + + + normalize: function (v) { + return Math.max(-0.5, Math.min(0.5, v)); + }, + + changePage: function () { + + }, +}; \ No newline at end of file -- 2.39.5