UNPKG

odin

Version:

Node.js Canvas/WebGL Javascript Game Framework

348 lines (275 loc) 11.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: core/rendering/canvas.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: core/rendering/canvas.js</h1> <section> <article> <pre class="prettyprint source"><code>if (typeof(define) !== "function") { var define = require("amdefine")(module); } define([ "odin/base/event_emitter", "odin/base/device", "odin/base/dom", "odin/core/game/log", "odin/core/game/config" ], function(EventEmitter, Device, Dom, Log, Config) { "use strict"; var addEvent = Dom.addEvent, removeEvent = Dom.removeEvent, addMeta = Dom.addMeta, floor = Math.floor, CANVAS_ID = 0, SCALE_REG = /-scale\s *=\s*[.0-9]+/g, VIEWPORT = "viewport", VIEWPORT_WIDTH = "viewport-width", VIEWPORT_HEIGHT = "viewport-height", CANVAS_STYLE = [ "background: #000000;", "position: absolute;", "top: 50%;", "left: 50%;", "padding:0px;", "margin: 0px;" ].join("\n"); addMeta(VIEWPORT, "viewport", "initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"); addMeta(VIEWPORT_WIDTH, "viewport", "width=device-width"); addMeta(VIEWPORT_HEIGHT, "viewport", "height=device-height"); /** * @class Canvas * @extends EventEmitter * @brief canvas helper * @param Number width * @param Number height */ function Canvas(opts) { opts || (opts = {}); EventEmitter.call(this); /** * @property Number canvasId * @memberof Canvas */ this.canvasId = ++CANVAS_ID; /** * @property Boolean fullScreen * @memberof Canvas */ this.fullScreen = opts.fullScreen ? opts.fullScreen : (opts.width == undefined && opts.height == undefined) ? true : false; /** * @property String customCursor * @memberof Canvas */ this.customCursor = opts.customCursor != undefined ? opts.customCursor : false; /** * @property Boolean hideMouse * @memberof Canvas */ this.hideMouse = opts.hideMouse != undefined ? opts.hideMouse : false; /** * @property Number width * @memberof Canvas */ this.width = opts.width !== undefined ? opts.width : window.innerWidth; /** * @property Number height * @memberof Canvas */ this.height = opts.height !== undefined ? opts.height : window.innerHeight; /** * @property Number aspect * @memberof Canvas */ this.aspect = this.width / this.height; /** * @property Number pixelWidth * @memberof Canvas */ this.pixelWidth = this.width; /** * @property Number pixelHeight * @memberof Canvas */ this.pixelHeight = this.height; /** * @property HTMLCanvasElement element * @memberof Canvas */ this.element = undefined; } EventEmitter.extend(Canvas); Canvas.prototype.init = function() { if (this.element) this.destroy(); var element = document.createElement("canvas"), style = element.style; element.id = "canvas-" + this.canvasId; style.cssText = CANVAS_STYLE; style.cursor = this.customCursor ? "url(" + this.customCursor + ")" : this.hideMouse ? "none" : "default"; document.body.appendChild(element); if (!Config.debug) { element.oncontextmenu = function() { return false; }; } addEvent(window, "resize orientationchange", this.handleResize, this); element.requestPointerLock || (element.requestPointerLock = ( element.webkitRequestPointerLock || element.mozRequestPointerLock || element.oRequestPointerLock || element.msRequestPointerLock )); element.exitPointerLock || (element.exitPointerLock = ( document.webkitExitPointerLock || document.mozExitPointerLock || document.oExitPointerLock || document.msExitPointerLock )); element.requestFullscreen || (element.requestFullscreen = ( element.webkitRequestFullscreen || element.mozRequestFullscreen || element.oRequestFullscreen || element.msRequestFullscreen )); element.exitFullscreen || (element.exitFullscreen = ( element.webkitExitFullscreen || element.mozExitFullscreen || element.oExitFullscreen || element.msExitFullscreen )); this.element = element; this.handleResize(); }; Canvas.prototype.destroy = function() { if (!this.element) return this; removeEvent(window, "resize orientationchange", this.handleResize, this); document.body.removeChild(this.element); this.element = undefined; return this; }; /** * @method setFullscreen * @memberof Canvas * @brief sets fullScreen boolean * @param Number width */ Canvas.prototype.setFullscreen = function(value) { if (!this.element || this.fullScreen === value) return this; this.fullScreen = !! value; this.handleResize(); return this; }; /** * @method setWidth * @memberof Canvas * @brief sets width and updates aspect * @param Number width */ Canvas.prototype.setWidth = function(width) { if (!this.element || this.width === width) return this; this.width = width; this.fullScreen = false; this.aspect = this.width / this.height; this.handleResize(); return this; }; /** * @method setHeight * @memberof Canvas * @brief sets height and updates aspect * @param Number height */ Canvas.prototype.setHeight = function(height) { if (!this.element || this.height === height) return this; this.height = height; this.fullScreen = false; this.aspect = this.width / this.height; this.handleResize(); return this; }; /** * @method style * @memberof Canvas * @brief sets style of html element * @param String key * @param String value */ Canvas.prototype.style = function(key, value) { if (!this.element) return this; this.element.style[key] = value; return this; }; /** * @method setBackgroundColor * @memberof Canvas * @brief sets html background color * @param String color */ Canvas.prototype.setBackgroundColor = function(color) { if (!this.element) return this; this.element.style.background = color; return this; }; Canvas.prototype.handleResize = function() { var viewportScale = document.getElementById(VIEWPORT).getAttribute("content"), w = window.innerWidth, h = window.innerHeight, aspect = w / h, element = this.element, style = element.style, width, height; if (this.fullScreen) { width = w; height = h; } else { if (aspect > this.aspect) { width = h * this.aspect; height = h; } else { width = w; height = w / this.aspect; } } this.pixelWidth = floor(width); this.pixelHeight = floor(height); element.width = width; element.height = height; style.marginLeft = -floor(width * 0.5) - 1 + "px"; style.marginTop = -floor(height * 0.5) - 1 + "px"; style.width = floor(width) + "px"; style.height = floor(height) + "px"; document.getElementById(VIEWPORT).setAttribute("content", viewportScale.replace(SCALE_REG, "-scale=" + Device.invPixelRatio)); document.getElementById(VIEWPORT_WIDTH).setAttribute("content", "width=" + w); document.getElementById(VIEWPORT_HEIGHT).setAttribute("content", "height=" + h); window.scrollTo(1, 1); this.emit("resize"); }; return Canvas; } ); </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Canvas.html">Canvas</a></li><li><a href="GUIObject.html">GUIObject</a></li><li><a href="MeshFilter.html">MeshFilter</a></li><li><a href="Odin.html">Odin</a></li><li><a href="Odin.Class.html">Class</a></li><li><a href="Odin.EventEmitter.html">EventEmitter</a></li><li><a href="Odin.GameObject.html">GameObject</a></li><li><a href="Odin.Scene.html">Scene</a></li><li><a href="P2Contact.html">P2Contact</a></li><li><a href="P2Equation.html">P2Equation</a></li><li><a href="P2Friction.html">P2Friction</a></li><li><a href="P2Solver.html">P2Solver</a></li><li><a href="ParticleSystem.html">ParticleSystem</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="SpriteAnimation.html">SpriteAnimation</a></li><li><a href="WebGLRenderer.html">WebGLRenderer</a></li><li><a href="WebGLRenderer2D.html">WebGLRenderer2D</a></li></ul> </nav> <br clear="both"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Mon Feb 24 2014 16:15:46 GMT-0600 (CST) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>