/* * Device * * Copyright (c) 2014 Renato de Pontes Pereira. * * 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. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; var Device = function(canvas) { this.initialize(canvas); } var p = Device.prototype; p.canvas = null; // Platform p.desktop = false; // OS p.iOS = false; p.android = false; p.chromeOS = false; p.linux = false; p.macOS = false; p.windows = false; p.windowsPhone = false; // Features p.canvas = false; p.file = false; p.fileSystem = false; p.localStorage = false; p.webGL = false; p.worker = false; p.touch = false; p.mspointer = false; p.css3D = false; p.pointerLock = false; p.typedArray = false; p.vibration = false; p.getUserMedia = false; p.quirksMode = false; // Browser p.ejecta = false; p.cocoonJS = false; p.webApp = false; p.arora = false; p.chrome = false; p.epiphany = false; p.firefox = false; p.ie = false; p.ieVersion = 0; p.trident = false; p.tridentVersion = 0; p.mobileSafari = false; p.midori = false; p.opera = false; p.safari = false; p.silk = false; // Audio p.audioData = false; p.webAudio = false; p.ogg = false; p.opus = false; p.mp3 = false; p.wav = false; p.m4a = false; p.webm = false; // Device p.iPhone = false; p.iPhone4 = false; p.iPad = false; p.pixelRatio = 0; p.littleEndian = false; p.fullscreen = false; p.initialize = function(canvas) { this.canvas = canvas; this._checkAudio(); this._checkBrowser(); this._checkCSS3D(); this._checkDevice(); this._checkFeatures(); this._checkOS(); this._checkFullscreen(); } p._checkOS = function () { var ua = navigator.userAgent; if (/Android/.test(ua)){ this.android = true; } else if (/CrOS/.test(ua)) { this.chromeOS = true; } else if (/iP[ao]d|iPhone/i.test(ua)) { this.iOS = true; } else if (/Linux/.test(ua)) { this.linux = true; } else if (/Mac OS/.test(ua)) { this.macOS = true; } else if (/Windows/.test(ua)) { this.windows = true; if (/Windows Phone/i.test(ua)) { this.windowsPhone = true; } } if (this.windows || this.macOS || (this.linux && this.silk === false)) { this.desktop = true; } // Windows Phone / Table reset if (this.windowsPhone || ((/Windows NT/i.test(ua)) && (/Touch/i.test(ua)))) { this.desktop = false; } } p._checkFeatures = function () { try { this.localStorage = !!localStorage.getItem; } catch (error) { this.localStorage = false; } this.file = !!window['File'] && !!window['FileReader'] && !!window['FileList'] && !!window['Blob']; this.fileSystem = !!window['requestFileSystem']; this.webGL = ( function () { try { var canvas = document.createElement( 'canvas' ); return !! window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ); } catch( e ) { return false; } } )(); if (this.webGL === null || this.webGL === false) { this.webGL = false; } else { this.webGL = true; } this.worker = !!window['Worker']; if ('ontouchstart' in document.documentElement || (window.navigator.maxTouchPoints && window.navigator.maxTouchPoints > 1)) { this.touch = true; } if (window.navigator.msPointerEnabled || window.navigator.pointerEnabled) { this.mspointer = true; } this.pointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document; this.quirksMode = (document.compatMode === 'CSS1Compat') ? false : true; this.getUserMedia = !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); } p._checkBrowser = function () { var ua = navigator.userAgent; if (/Arora/.test(ua)) { this.arora = true; } else if (/Chrome/.test(ua)) { this.chrome = true; } else if (/Epiphany/.test(ua)) { this.epiphany = true; } else if (/Firefox/.test(ua)) { this.firefox = true; } else if (/Mobile Safari/.test(ua)) { this.mobileSafari = true; } else if (/MSIE (\d+\.\d+);/.test(ua)) { this.ie = true; this.ieVersion = parseInt(RegExp.$1, 10); } else if (/Midori/.test(ua)) { this.midori = true; } else if (/Opera/.test(ua)) { this.opera = true; } else if (/Safari/.test(ua)) { this.safari = true; } else if (/Silk/.test(ua)) { this.silk = true; } else if (/Trident\/(\d+\.\d+)(.*)rv:(\d+\.\d+)/.test(ua)) { this.ie = true; this.trident = true; this.tridentVersion = parseInt(RegExp.$1, 10); this.ieVersion = parseInt(RegExp.$3, 10); } // WebApp mode in iOS if (navigator['standalone']) { this.webApp = true; } if (navigator['isCocoonJS']) { this.cocoonJS = true; } if (typeof window.ejecta !== "undefined") { this.ejecta = true; } } p._checkAudio = function () { this.audioData = !!(window['Audio']); this.webAudio = !!(window['webkitAudioContext'] || window['AudioContext']); var audioElement = document.createElement('audio'); var result = false; try { if (result = !!audioElement.canPlayType) { if (audioElement.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/, '')) { this.ogg = true; } if (audioElement.canPlayType('audio/ogg; codecs="opus"').replace(/^no$/, '')) { this.opus = true; } if (audioElement.canPlayType('audio/mpeg;').replace(/^no$/, '')) { this.mp3 = true; } // Mimetypes accepted: // developer.mozilla.org/En/Media_formats_supported_by_the_audio_and_video_elements // bit.ly/iphoneoscodecs if (audioElement.canPlayType('audio/wav; codecs="1"').replace(/^no$/, '')) { this.wav = true; } if (audioElement.canPlayType('audio/x-m4a;') || audioElement.canPlayType('audio/aac;').replace(/^no$/, '')) { this.m4a = true; } if (audioElement.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/, '')) { this.webm = true; } } } catch (e) { } } p._checkDevice = function () { this.pixelRatio = window['devicePixelRatio'] || 1; this.iPhone = navigator.userAgent.toLowerCase().indexOf('iphone') != -1; this.iPhone4 = (this.pixelRatio == 2 && this.iPhone); this.iPad = navigator.userAgent.toLowerCase().indexOf('ipad') != -1; if (typeof Int8Array !== 'undefined') { this.littleEndian = new Int8Array(new Int16Array([1]).buffer)[0] > 0; this.typedArray = true; } else { this.littleEndian = false; this.typedArray = false; } navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate; if (navigator.vibrate) { this.vibration = true; } } p._checkCSS3D = function () { var el = document.createElement('p'); var has3d; var transforms = { 'webkitTransform': '-webkit-transform', 'OTransform': '-o-transform', 'msTransform': '-ms-transform', 'MozTransform': '-moz-transform', 'transform': 'transform' }; // Add it to the body to get the computed style. document.body.insertBefore(el, null); for (var t in transforms) { if (el.style[t] !== undefined) { el.style[t] = "translate3d(1px,1px,1px)"; has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]); } } document.body.removeChild(el); this.css3D = (has3d !== undefined && has3d.length > 0 && has3d !== "none"); } p._checkFullscreen = function () { var fs = [ 'requestFullscreen', 'webkitRequestFullscreen', 'msRequestFullscreen', 'mozRequestFullScreen', ]; for (var i = 0; i < fs.length; i++) { if (this.canvas[fs[i]]) { this.fullscreen = true; break; } } if (window['Element'] && Element['ALLOW_KEYBOARD_INPUT']) { this.fullscreenKeyboard = true; } } p.canPlayAudio = function (type) { if (type == 'mp3' && this.mp3) { return true; } else if (type === 'ogg' && (this.ogg || this.opus)) { return true; } else if (type === 'm4a' && this.m4a) { return true; } else if (type === 'wav' && this.wav) { return true; } else if (type === 'webm' && this.webm) { return true; } return false; } creatine.Device = Device; }());