Code Monkey home page Code Monkey logo

labyrinth's Introduction

Labyrinth

This is a HTML5/Javascript project that demonstrates device orientation events. Note that the project was programmed such that it only works on a mobile device (e.g. phones, tablets).

Usage

After loading the demo, it will ask you to rotate your device to landscape mode. After rotating, tap the screen to begin, and you can begin tilting your device to move a "marble" around on the screen. Rotating back to portrait mode will "pause" the demo.

Each time you resume the demo from a paused state, it will recalibrate the center position based on how the device is held.

The demo can be found here.

Documentation

Media Queries

This project used media queries to determine what kind of device is being used by the user. In CSS, different styles were applied based on the device type. For example, the first block would apply to phones, and the second block would apply to tablets. Note that if using a tablet, it would apply the phone styling first, then also apply the tablet styling on top of it.

@media only screen and (min-device-width: 320px) {
	#overlaytext {
		font-size: 25px;
	}
}
@media only screen and (min-device-width: 768px) {
	#overlaytext {
		font-size: 50px;
	}
}

In Javascript, I first checked to see if the browser supported this functionality. Then, I added some if statements to determine the device type. I used this to set certain settings that would be more "optimized" for a phone versus a tablet.

if (window.matchMedia) {
	// PHONE
	if (window.matchMedia('(max-device-width: 767px)').matches) {
		// do something
	}
	// TABLET
	else if (window.matchMedia('(max-device-width: 1024px)').matches) {
		// do something
	}
}

Device Orientation Change - Portrait/Landscape

To detect when a device changes orientation (from landscape to portrait and vice versa), I employed the use of the window.onorientationchange event. All modern mobile browsers support this event.

if (window.DeviceOrientationEvent) {
    // do something...
}
else {
    // display text that this device does not support device orientation
}

Once you have determined that the device supports orientation events, you can then receive notifications of when a device changes orientation and subsequently perform an action.

window.onorientationchange = function() { 
	switch(window.orientation) {  
	    // Note: The following numbers don't necessarily work on all devices
	    // See http://www.matthewgifford.com/blog/2011/12/22/a-misconception-about-window-orientation/ for more detail
  		case -90: // landscape
  		    // do something
  			break;
 		case 90: // landscape
 		    // do something
    		break; 
  		default: // portrait
  		    // do something
    		break; 
	}
};

Device Orientation Change - Ball Movement

In addition to receiving notifications when a device changes orientation from landscape to orientation, you can add a listener to receive notifications whenever a device changes its orientation at all.

window.addEventListener('deviceorientation', function(event) {
    // event.gamma - angle of device around x axis
    // event.beta - angle of device around y axis
    // event.alpha - angle of device around z axis
}

Animation

Instead of using a timer loop to create animations, there is a feature to allow the browser to notify your application when it is ready for the next frame of the animation.

// obtain a reference to the appropriate function name (this function has different names between browsers)
// if the browser does not support it, we'll fall back to using a timer loop
window.reqAnimFrame = window.mozRequestAnimationFrame ||
                window.webkitRequestAnimationFrame ||
                window.msRequestAnimationFrame ||
                window.oRequestAnimationFrame ||
                window.requestAnimationFrame || 
                function(callback) {
          			window.setTimeout(callback, 1000 / 60);
        		};
        		
function animate() {
    // draw something here
    
    reqAnimFrame(animate); // call animate function again when browser is ready
}

// initial animate call
animate();

References

Media Queries

Device Orientation

Animation

labyrinth's People

Contributors

moduli avatar stchiang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.