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.
gyro.js (12kb) - readable, with comments
gyro.min.js (4kb) - minified
gyro.css (2kb) - meant as an example, can be modified
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.
The base setup is pretty simple, you need to:
startGyro()
function with the IDs of parent and child<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);
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 default 5
Changes how many elements will be parallaxed around.
<div id="parent"> <div id="child" count="15"></div> </div>
strength default 3
Changes how intensely the elements will be parallaxed around.
<div id="parent"> <div id="child" strength="6"></div> </div>
color1 default #B090CB
Changes the background color.
<div id="parent"> <div id="child" color1="#FFFFFF"></div> </div>
color2 default #2A2E45
Changes the color at the top.
<div id="parent"> <div id="child" color2="#000000"></div> </div>
isRound default true
If true, it will be spheres, if false squares.
<div id="parent"> <div id="child" isRound="false"></div> </div>
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 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 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 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.
Licensed under the MIT license.
Copyright © Tea Bähr 2021
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
code-prettify by © 2013 Google Inc.
Licensed under the Apache License, Version 2.0
Vim sunburst theme by David Leibovic