Skip to content

Commit fea0ec5

Browse files
committed
🇬🇧 Added relativeInput and clipRelativeInput functionality. Created relative.html example to demonstrate new features.
1 parent f1b54b5 commit fea0ec5

File tree

18 files changed

+287
-98
lines changed

18 files changed

+287
-98
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,20 @@ There are a number of behaviours that you can setup for any given **Parallax**
4040
instance. These behaviours can either be specified in the markup via data
4141
attributes or in JavaScript via the constructor and API.
4242

43-
| Behaviour | Values | Description |
44-
| ------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------ |
45-
| `calibrate-x` | `true` or `false` | Specifies whether or not to cache & calculate the motion relative to the initial `x` axis value on initialisation. |
46-
| `calibrate-y` | `true` or `false` | Specifies whether or not to cache & calculate the motion relative to the initial `y` axis value on initialisation. |
47-
| `invert-x` | `true` or `false` | `true` moves layers in opposition to the device motion, `false` slides them away. |
48-
| `invert-y` | `true` or `false` | `true` moves layers in opposition to the device motion, `false` slides them away. |
49-
| `limit-x` | `number` or `false` | A numeric value limits the total range of motion in `x`, `false` allows layers to move with complete freedom. |
50-
| `limit-y` | `number` or `false` | A numeric value limits the total range of motion in `y`, `false` allows layers to move with complete freedom. |
51-
| `scalar-x` | `number` | Multiplies the input motion by this value, increasing or decreasing the sensitivity of the layer motion. |
52-
| `scalar-y` | `number` | Multiplies the input motion by this value, increasing or decreasing the sensitivity of the layer motion. |
53-
| `friction-x` | `number` `0 - 1` | The amount of friction the layers experience. This essentially adds some easing to the layer motion. |
54-
| `friction-y` | `number` `0 - 1` | The amount of friction the layers experience. This essentially adds some easing to the layer motion. |
43+
| Behaviour | Values | Description |
44+
| ------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
45+
| `relativeInput` | `true` or `false` | Specifies whether or not to use the coordinate system of the `element` passed to the parallax `constructor`. **Mouse input only.** |
46+
| `clipRelativeInput` | `true` or `false` | Specifies whether or not to clip the mouse input to the bounds of the `element` passed to the parallax `constructor`. **Mouse input only.** |
47+
| `calibrate-x` | `true` or `false` | Specifies whether or not to cache & calculate the motion relative to the initial `x` axis value on initialisation. |
48+
| `calibrate-y` | `true` or `false` | Specifies whether or not to cache & calculate the motion relative to the initial `y` axis value on initialisation. |
49+
| `invert-x` | `true` or `false` | `true` moves layers in opposition to the device motion, `false` slides them away. |
50+
| `invert-y` | `true` or `false` | `true` moves layers in opposition to the device motion, `false` slides them away. |
51+
| `limit-x` | `number` or `false` | A numeric value limits the total range of motion in `x`, `false` allows layers to move with complete freedom. |
52+
| `limit-y` | `number` or `false` | A numeric value limits the total range of motion in `y`, `false` allows layers to move with complete freedom. |
53+
| `scalar-x` | `number` | Multiplies the input motion by this value, increasing or decreasing the sensitivity of the layer motion. |
54+
| `scalar-y` | `number` | Multiplies the input motion by this value, increasing or decreasing the sensitivity of the layer motion. |
55+
| `friction-x` | `number` `0 - 1` | The amount of friction the layers experience. This essentially adds some easing to the layer motion. |
56+
| `friction-y` | `number` `0 - 1` | The amount of friction the layers experience. This essentially adds some easing to the layer motion. |
5557

5658
In addition to the behaviours described above, there are **two** methods `enable()`
5759
and `disable()` that *activate* and *deactivate* the **Parallax** instance respectively.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "parallax",
33
"description": "Parallax Engine that reacts to the orientation of a smart device.",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"license": "MIT",
66
"homepage": "http://wagerfield.github.io/parallax/",
77
"authors": [

deploy/jquery.parallax.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
var NAME = 'parallax';
4646
var MAGIC_NUMBER = 30;
4747
var DEFAULTS = {
48+
relativeInput: false,
49+
clipRelativeInput: false,
4850
calibrationThreshold: 100,
4951
calibrationDelay: 500,
5052
supportDelay: 500,
@@ -98,11 +100,14 @@
98100
this.depths = [];
99101
this.raf = null;
100102

101-
// Offset
102-
this.ox = 0;
103-
this.oy = 0;
104-
this.ow = 0;
105-
this.oh = 0;
103+
// Element
104+
this.bounds = null;
105+
this.ex = 0;
106+
this.ey = 0;
107+
this.ew = 0;
108+
this.eh = 0;
109+
this.ecx = 0;
110+
this.ecy = 0;
106111

107112
// Calibration
108113
this.cx = 0;
@@ -171,8 +176,8 @@
171176

172177
Plugin.prototype.ww = null;
173178
Plugin.prototype.wh = null;
174-
Plugin.prototype.hw = null;
175-
Plugin.prototype.hh = null;
179+
Plugin.prototype.wcx = null;
180+
Plugin.prototype.wcy = null;
176181
Plugin.prototype.portrait = null;
177182
Plugin.prototype.desktop = !navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|BB10|mobi|tablet|opera mini|nexus 7)/i);
178183
Plugin.prototype.vendors = [null,['-webkit-','webkit'],['-moz-','Moz'],['-o-','O'],['-ms-','ms']];
@@ -218,18 +223,10 @@
218223
};
219224

220225
Plugin.prototype.updateDimensions = function() {
221-
222-
// Cache Context Dimensions
223-
this.ox = this.$context.offset().left;
224-
this.oy = this.$context.offset().top;
225-
this.ow = this.$context.width();
226-
this.oh = this.$context.height();
227-
228-
// Cache Window Dimensions
229226
this.ww = window.innerWidth;
230227
this.wh = window.innerHeight;
231-
this.hw = this.ww / 2;
232-
this.hh = this.wh / 2;
228+
this.wcw = this.ww / 2;
229+
this.wcy = this.wh / 2;
233230
};
234231

235232
Plugin.prototype.queueCalibration = function(delay) {
@@ -417,9 +414,34 @@
417414

418415
Plugin.prototype.onMouseMove = function(event) {
419416

420-
// Calculate Input
421-
this.ix = (event.pageX - this.hw) / this.hw;
422-
this.iy = (event.pageY - this.hh) / this.hh;
417+
// Calculate Mouse Input
418+
if (!this.orientationSupport && this.relativeInput) {
419+
420+
// Calculate input relative to the element.
421+
this.bounds = this.element.getBoundingClientRect();
422+
this.ex = this.bounds.left;
423+
this.ey = this.bounds.top;
424+
this.ew = this.bounds.width;
425+
this.eh = this.bounds.height;
426+
this.ecx = this.ew / 2;
427+
this.ecy = this.eh / 2;
428+
this.ix = (event.clientX - this.ex - this.ecx) / this.ecx;
429+
this.iy = (event.clientY - this.ey - this.ecy) / this.ecy;
430+
431+
// Clip input to the element bounds.
432+
if (this.clipRelativeInput) {
433+
this.ix = Math.max(this.ix,-1);
434+
this.ix = Math.min(this.ix, 1);
435+
this.iy = Math.max(this.iy,-1);
436+
this.iy = Math.min(this.iy, 1);
437+
}
438+
439+
} else {
440+
441+
// Calculate input relative to the window.
442+
this.ix = (event.clientX - this.wcx) / this.wcx;
443+
this.iy = (event.clientY - this.wcy) / this.wcy;
444+
}
423445
};
424446

425447
var API = {

deploy/jquery.parallax.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deploy/parallax.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
var NAME = 'Parallax';
4646
var MAGIC_NUMBER = 30;
4747
var DEFAULTS = {
48+
relativeInput: false,
49+
clipRelativeInput: false,
4850
calibrationThreshold: 100,
4951
calibrationDelay: 500,
5052
supportDelay: 500,
@@ -95,11 +97,14 @@
9597
this.depths = [];
9698
this.raf = null;
9799

98-
// Offset
99-
this.ox = 0;
100-
this.oy = 0;
101-
this.ow = 0;
102-
this.oh = 0;
100+
// Element
101+
this.bounds = null;
102+
this.ex = 0;
103+
this.ey = 0;
104+
this.ew = 0;
105+
this.eh = 0;
106+
this.ecx = 0;
107+
this.ecy = 0;
103108

104109
// Calibration
105110
this.cx = 0;
@@ -221,8 +226,8 @@
221226

222227
Parallax.prototype.ww = null;
223228
Parallax.prototype.wh = null;
224-
Parallax.prototype.hw = null;
225-
Parallax.prototype.hh = null;
229+
Parallax.prototype.wcx = null;
230+
Parallax.prototype.wcy = null;
226231
Parallax.prototype.portrait = null;
227232
Parallax.prototype.desktop = !navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|BB10|mobi|tablet|opera mini|nexus 7)/i);
228233
Parallax.prototype.vendors = [null,['-webkit-','webkit'],['-moz-','Moz'],['-o-','O'],['-ms-','ms']];
@@ -263,18 +268,10 @@
263268
};
264269

265270
Parallax.prototype.updateDimensions = function() {
266-
267-
// Cache Context Dimensions
268-
this.ox = this.offset(this.element).left;
269-
this.oy = this.offset(this.element).top;
270-
this.ow = this.element.offsetWidth;
271-
this.oh = this.element.offsetHeight;
272-
273-
// Cache Window Dimensions
274271
this.ww = window.innerWidth;
275272
this.wh = window.innerHeight;
276-
this.hw = this.ww / 2;
277-
this.hh = this.wh / 2;
273+
this.wcx = this.ww / 2;
274+
this.wcy = this.wh / 2;
278275
};
279276

280277
Parallax.prototype.queueCalibration = function(delay) {
@@ -459,9 +456,34 @@
459456

460457
Parallax.prototype.onMouseMove = function(event) {
461458

462-
// Calculate Input
463-
this.ix = (event.pageX - this.hw) / this.hw;
464-
this.iy = (event.pageY - this.hh) / this.hh;
459+
// Calculate Mouse Input
460+
if (!this.orientationSupport && this.relativeInput) {
461+
462+
// Calculate input relative to the element.
463+
this.bounds = this.element.getBoundingClientRect();
464+
this.ex = this.bounds.left;
465+
this.ey = this.bounds.top;
466+
this.ew = this.bounds.width;
467+
this.eh = this.bounds.height;
468+
this.ecx = this.ew / 2;
469+
this.ecy = this.eh / 2;
470+
this.ix = (event.clientX - this.ex - this.ecx) / this.ecx;
471+
this.iy = (event.clientY - this.ey - this.ecy) / this.ecy;
472+
473+
// Clip input to the element bounds.
474+
if (this.clipRelativeInput) {
475+
this.ix = Math.max(this.ix,-1);
476+
this.ix = Math.min(this.ix, 1);
477+
this.iy = Math.max(this.iy,-1);
478+
this.iy = Math.min(this.iy, 1);
479+
}
480+
481+
} else {
482+
483+
// Calculate input relative to the window.
484+
this.ix = (event.clientX - this.wcx) / this.wcx;
485+
this.iy = (event.clientY - this.wcy) / this.wcy;
486+
}
465487
};
466488

467489
// Expose Parallax

deploy/parallax.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/images/layer1.png

-1.02 KB
Loading

examples/images/layer2.png

-1.07 KB
Loading

examples/images/layer3.png

-1 KB
Loading

examples/images/layer4.png

-884 Bytes
Loading

0 commit comments

Comments
 (0)