1 /* 2 *Copyright (c) 2009, TellurianRing.com 3 *All rights reserved. 4 * 5 *Redistribution and use in source and binary forms, with or without modification, 6 *are permitted provided that the following conditions are met: 7 * 8 * Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * Neither the name of the Organization (TellurianRing.com) nor the names of 14 * its contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 *WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 *DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 21 *ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 *(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 *LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 24 *ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 *(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 /** 29 * A utility function that checks to see if a given set of objects all are 30 * defined. This does not check against null. 31 * @param An undefined number of arguments. 32 */ 33 function exists() { // {{{ 34 var exists = true; 35 // This loops through each argument and checks to see if it's defined. 36 // It shortcuts the first time it finds one that doesn't. 37 for(var index = 0; index < arguments.length && exists; index++) { 38 exists = exists && (typeof arguments[index] != 'undefined') 39 } 40 return exists; 41 }// }}} exists 42 43 /** 44 * A utility function for create a canvas element with the given ID, width, and 45 * height. It is not added to the document, that is left up to the caller. 46 * @param _id - The ID of the canvas object that will be created. 47 * @param _w - The width of the canvas object. 48 * @param _h - The height of the canvas object. 49 * @param _def - The default content of the canvas object. This content is shown 50 * in browsers that don't support the HTML 5 Canvas element. 51 */ 52 function createCanvas(_id, _w, _h, _def) { // {{{ 53 54 var default_content = 55 '<h2>Ooops. Your browser does not support the HTML 5 Canvas element.</h2>' + 56 '<h3>Please install a compatible browser.<h3>' + 57 '<h4>Such As: ' + 58 '<a href="http://www.mozilla.com/en-US/firefox/personal.html">Firefox</a>, ' + 59 '<a href="http://www.apple.com/safari/">Safari</a>, ' + 60 '<a href="http://www.google.com/chrome">Google Chrome</a>, ' + 61 'or <a href="http://www.opera.com/">Opera</a>' + 62 '</h4>'; 63 64 // create the element. 65 var canvas = document.createElement("canvas"); 66 67 // set the attributes for id, width, and height 68 canvas.setAttribute("id", _id); 69 canvas.setAttribute("width", _w); 70 canvas.setAttribute("height", _h); 71 72 // also add the width and height style attributes. 73 canvas.style.width = _w + "px"; 74 canvas.style.height = _h + "px"; 75 76 canvas.innerHTML = check(_def, default_content); 77 78 return canvas; 79 }// }}} createCanvas 80 81 /** 82 * A utility function which checks if the given _value exists. If it does, it 83 * returns that, otherwise it returns the given _default. 84 * @param _value The value to check for. 85 * @param _default The value to return if the given _value doesn't exist. 86 */ 87 function check(_value, _default) { 88 if(exists(_value)) { 89 return _value; 90 } 91 return _default; 92 } 93 94 /** 95 * A convenience function which creates a new Font object using nameless parameters. 96 * @function 97 * @param family 98 * @param size 99 * @param style 100 * @param varient 101 * @param weight 102 */ 103 function font(family, size, style, varient, weight) { 104 var obj = {family: family, size: size, style: style, varient: varient, weight: weight}; 105 return Font(obj); 106 } 107 108 /** 109 * Utility function to convert degrees into radians. 110 * @function 111 * @param {Number} _degrees The degrees to convert. 112 */ 113 function toRadians(_degrees) { 114 return _degrees * Math.PI / 180.0; 115 } 116 // These properties are for jEdit - Programmer's Text Editor. 117 // Load this file in jEdit to see what they do. 118 // ::folding=explicit:mode=javascript:noTabs=true:collapseFolds=4:: 119