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

18 files changed

+287
-98
lines changed

README.md

+14-12
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

+1-1
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

+42-20
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deploy/parallax.js

+42-20
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

+1-1
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

examples/images/layer5.png

-1003 Bytes
Loading

examples/images/layer6.png

-1.13 KB
Loading

examples/images/layers.psd

-15.4 KB
Binary file not shown.

examples/relative.html

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
5+
<meta charset="utf-8">
6+
7+
<title>Parallax.js | Relative Example</title>
8+
9+
<!-- Behavioral Meta Data -->
10+
<meta name="apple-mobile-web-app-capable" content="yes">
11+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
12+
13+
<!-- Styles -->
14+
<link rel="stylesheet" type="text/css" href="styles/styles.css"/>
15+
16+
</head>
17+
<body>
18+
19+
<div id="container" class="container">
20+
<ul id="scene" class="scene border">
21+
<li class="layer" data-depth="1.00"><img src="images/layer1.png"></li>
22+
<li class="layer" data-depth="0.80"><img src="images/layer2.png"></li>
23+
<li class="layer" data-depth="0.60"><img src="images/layer3.png"></li>
24+
<li class="layer" data-depth="0.40"><img src="images/layer4.png"></li>
25+
<li class="layer" data-depth="0.20"><img src="images/layer5.png"></li>
26+
<li class="layer" data-depth="0.00"><img src="images/layer6.png"></li>
27+
</ul>
28+
<br>
29+
<input type="checkbox" id="relative" checked>
30+
<label for="relative">relativeInput</label>
31+
<input type="checkbox" id="clip">
32+
<label for="clip">clipRelativeInput</label>
33+
</div>
34+
35+
<!-- Scripts -->
36+
<script src="../deploy/parallax.js"></script>
37+
<script>
38+
39+
// Elements
40+
var scene = document.getElementById('scene');
41+
var clipCheckbox = document.getElementById('clip');
42+
var relativeCheckbox = document.getElementById('relative');
43+
44+
// Pretty simple huh?
45+
var parallax = new Parallax(scene, {
46+
relativeInput: relativeCheckbox.checked,
47+
clipRelativeInput: clipCheckbox.checked
48+
});
49+
50+
relativeCheckbox.addEventListener('change', function(event) {
51+
parallax.relativeInput = relativeCheckbox.checked;
52+
});
53+
54+
clipCheckbox.addEventListener('change', function(event) {
55+
parallax.clipRelativeInput = clipCheckbox.checked;
56+
});
57+
58+
</script>
59+
60+
</body>
61+
</html>

examples/styles/styles.css

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
body {
2+
font-family: monospace;
3+
font-size: 18px;
24
background: #111;
5+
color: #666;
36
margin: 0;
47
}
58

@@ -8,10 +11,45 @@ img {
811
width: 100%;
912
}
1013

11-
.scene {
12-
max-width: 500px;
14+
input[type=checkbox] {
15+
display: none;
16+
}
17+
18+
label {
19+
display: inline-block;
20+
margin-right: 1em;
21+
padding: 0.4em 0;
22+
cursor: pointer;
23+
}
24+
25+
input[type=checkbox] + label:before {
26+
display: inline-block;
27+
position: relative;
28+
background: #444;
29+
margin-right: 8px;
30+
content: "";
31+
height: 16px;
32+
width: 16px;
33+
top: 1px;
34+
}
35+
36+
input[type=checkbox]:checked + label:before {
37+
background: #00FFAA;
38+
}
39+
40+
.container {
41+
max-width: 600px;
1342
margin: 0 auto;
43+
padding: 5%;
44+
}
45+
46+
.scene {
1447
padding: 0;
48+
margin: 0;
49+
}
50+
51+
.border {
52+
border: 2px dashed #00FFAA;
1553
}
1654

1755
.layer:nth-child(1) {

package.json

+1-1
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
"author": "Matthew Wagerfield <[email protected]>",

0 commit comments

Comments
 (0)