Gyroscope Parallax - Documentation

Overview

What is it?

This is a script for creating a visual effect that is controlled by moving your phone around in 3D-Space.

It only works on devices with a gyroscope - like a phone, tablet etc. It will not function on desktop devices. Apple devices aren't supported.

Download

gyro.js (12kb) - readable, with comments

gyro.min.js (4kb) - minified

gyro.css (2kb) - meant as an example, can be modified

Examples

Default values - this is how it looks if you don't change any parameters.

Dark Tunnel - some values are adjusted to make it look like a tunnel.

Playground - here you can try to modify some values to your desire.

Setup

The base setup is pretty simple, you need to:

<link href="gyro.css" rel="stylesheet">
    
<div id="parent">
    <div id="child"></div>
</div>

<script src="https://gka.github.io/chroma.js/libs/chroma.min.js"></script>
<script src="gyro.min.js"></script>
<script>
    startGyro("child", "parent");
</script>

You can call the parent and child element anything you want, as long as you call their id="" in the startGyro() function.

If you want to have controls to modify the parameters in real-time, add a third true parameter:

//startGyro(childID, parentID, useControls?)
startGyro("child", "parent", true);

Parameters

To adjust the settings in the code, give the child element any of these attributes.

The values are best understood if you play around with them while having controls enabled. Anyway, here are explanations how to use them:

Count

count default 5
Changes how many elements will be parallaxed around.

<div id="parent">
    <div id="child" count="15"></div>
</div>

Strength

strength default 3
Changes how intensely the elements will be parallaxed around.

<div id="parent">
    <div id="child" strength="6"></div>
</div>

Color1

color1 default #B090CB
Changes the background color.

<div id="parent">
    <div id="child" color1="#FFFFFF"></div>
</div>

Color2

color2 default #2A2E45
Changes the color at the top.

<div id="parent">
    <div id="child" color2="#000000"></div>
</div>

isRound

isRound default true
If true, it will be spheres, if false squares.

<div id="parent">
    <div id="child" isRound="false"></div>
</div>

isCut

isCut default false
If true, it will not show any overflow - and look more like a tunnel, rather than stacked-on-top shapes.

<div id="parent">
    <div id="child" isCut="true"></div>
</div>

isFit

isFit default false
If true, it will fit the parent container completely instead of being a equal-aspect-ratio fit with some padding..

<div id="parent">
    <div id="child" isFit="true"></div>
</div>

invX & invY

invX default false
invY default true
Inverts the X & Y axis.

As an example, if you tilt your phone invX="false" will make the elements tilt right too, while invX="true" makes the elements tilt to the left instead.

<div id="parent">
    <div id="child" invX="true"></div>
</div>

defaultGamma & defaultBeta

defaultGamma default false
defaultBeta default false
If you set these to a value between 0 and 360, it will take that rotation as the default/center. While it is set to false, it will take the starting orientation (while the page loads) as the default/center.

As an example, defaultGamma="45" will set the center-point to be when you hold your phone in a 45°-Angle. 0° would be lying flat on the table etc.

<div id="parent">
    <div id="child" defaultGamma="45"></div>
</div>

Here's a small illustration, showing the rotation axes:
Gamma means tilting your phone left and right,
Beta means tilting your phone up and down.