/* * tmlib.js v0.1.4 * http://github.com/phi1618/tmlib.js * MIT licensed * * Copyright (C) 2010 phi, http://tmlife.net */ (function() { "use strict"; })(); /* * tm namespace */ var tm = tm || {}; tm.global = window || global || this; (function() { /** * バージョン */ tm.VERSION = "0.1.4"; /** * tmlib.js のルートパス */ tm.LIB_ROOT = (function(){ if (!window.document) return ; var scripts = document.getElementsByTagName("script"); for (var i=0, len=scripts.length; i 0 || ua.indexOf("iPad") > 0 || ua.indexOf("Android") > 0); })(); /** * クラス定義 */ tm.createClass = function(prop) { // デフォルト値 prop.init = prop.init || function() {}; prop.superClass = prop.superClass || null; // クラス作成 var tm_class = function() { var temp = new tm_class.prototype.creator(); tm_class.prototype.init.apply(temp, arguments); return temp }; // 継承 if (prop.superClass) { tm_class.prototype = Object.create(prop.superClass.prototype); tm_class.prototype.superInit = function() { // 一時的に superClass として扱われるようにする var temp_proto = this.__proto__; this.__proto__ = prop.superClass.prototype; // 親の初期化を呼び出す prop.superClass.prototype.init.apply(this, arguments); // 元に戻す this.__proto__ = temp_proto; }; } tm_class.prototype.selfClass = tm_class; // プロパティを追加 for (var key in prop) { tm_class.prototype[key] = prop[key]; } // クリエイタの生成 tm_class.prototype.creator = function() { return this; }; // クリエイタの継承 tm_class.prototype.creator.prototype = tm_class.prototype; return tm_class; }; /** * 使用する */ tm.using = function() { for (var key in tm) { window[key] = tm[key]; } }; /** * ループ */ tm.setLoop = function(fn, delay) { var temp = function() { // 開始時間 var start = (new Date()).getTime(); // 実行 fn(); // 経過時間 var progress = (new Date()).getTime() - start; // 次回までの待ち時間を計算 var newDelay = delay-progress; newDelay = (newDelay > 0) ? newDelay : 0; // 次回呼び出し登録 setTimeout(arguments.callee, newDelay); }; setTimeout(temp, delay); }; /** * キーコード */ tm.keyCode = { "backspace" : 8, "tab" : 9, "enter" : 13, "return" : 13, "shift" : 16, "ctrl" : 17, "alt" : 18, "pause" : 19, "capslock" : 20, "escape" : 27, "pageup" : 33, "pagedown" : 34, "end" : 35, "home" : 36, "left" : 37, "up" : 38, "right" : 39, "down" : 40, "insert" : 45, "delete" : 46, "0" : 48, "1" : 49, "2" : 50, "3" : 51, "4" : 52, "5" : 53, "6" : 54, "7" : 55, "8" : 56, "9" : 57, "a" : 65, "A" : 65, "b" : 66, "B" : 66, "c" : 67, "C" : 67, "d" : 68, "D" : 68, "e" : 69, "E" : 69, "f" : 70, "F" : 70, "g" : 71, "G" : 71, "h" : 72, "H" : 72, "i" : 73, "I" : 73, "j" : 74, "J" : 74, "k" : 75, "K" : 75, "l" : 76, "L" : 76, "m" : 77, "M" : 77, "n" : 78, "N" : 78, "o" : 79, "O" : 79, "p" : 80, "P" : 80, "q" : 81, "Q" : 81, "r" : 82, "R" : 82, "s" : 83, "S" : 83, "t" : 84, "T" : 84, "u" : 85, "U" : 85, "v" : 86, "V" : 86, "w" : 87, "W" : 87, "x" : 88, "X" : 88, "y" : 89, "Y" : 89, "z" : 90, "Z" : 90, "numpad0" : 96, "numpad1" : 97, "numpad2" : 98, "numpad3" : 99, "numpad4" : 100, "numpad5" : 101, "numpad6" : 102, "numpad7" : 103, "numpad8" : 104, "numpad9" : 105, "multiply" : 106, "add" : 107, "subtract" : 109, "decimalpoint" : 110, "divide" : 111, "f1" : 112, "f2" : 113, "f3" : 114, "f4" : 115, "f5" : 116, "f6" : 117, "f7" : 118, "f8" : 119, "f9" : 120, "f10" : 121, "f11" : 122, "f12" : 123, "numlock" : 144, "scrolllock": 145, "semicolon" : 186, "equalsign" : 187, "comma" : 188, "dash" : 189, "period" : 190, "forward slash" : 191, "/": 191, "grave accent" : 192, "open bracket" : 219, "back slash" : 220, "close braket" : 221, "single quote" : 222, "space" : 32 }; tm.inform = function(parent){ parent = parent || document.body; var eInfo = document.createElement("div"); eInfo.setAttribute("class", "tm-info"); eInfo.addEventListener("mouseover", function(){ this.style.opacity = 0.9; }, false); eInfo.addEventListener("mouseout", function(){ this.style.opacity = 0.25; }, false); with(eInfo.style) { position = "absolute"; width = "100%"; // top = "0px"; bottom = "0px"; left = "0px"; right = "0px"; margin = "0px"; padding = "10px 0px"; zIndex = "0"; textAlign = "center"; fontFamily = '"Meiryo", "メイリオ", "ヒラギノ角ゴ Pro W3", sans-serif'; fontSize = "13px"; opacity = "0.25"; backgroundColor = "rgb(230,230,255)"; background = "-webkit-linear-gradient(top, hsla(0, 100%, 100%, 0.8) 0%, hsla(0, 100%, 100%, 0.3) 50%, hsla(0, 100%, 100%, 0.1) 51%, hsla(0, 100%, 100%, 0.2) 100%), rgb(190,190,210)"; background = "-moz-linear-gradient(top, hsla(0, 100%, 100%, 0.8) 0%, hsla(0, 100%, 100%, 0.3) 50%, hsla(0, 100%, 100%, 0.1) 51%, hsla(0, 100%, 100%, 0.2) 100%), rgb(190,190,210)"; WebkitTransition = "1s"; MozTransition = "1s"; } /* eInfo.innerHTML = "このプログラムで利用している JavaScript ライブラリ 『tmlib.js』 はこちらからダウンロードできます. 詳しくはBlogに書いています.".format({ "tmlibLink": "http://code.google.com/p/tmlib-js/downloads/list", "blogLink" : "http://tmlife.net/tmlib" }); */ eInfo.innerHTML = "このプログラムで利用している JavaScript ライブラリ 『tmlib.js』 についてはこちら.".format({ "projectLink" : "https://github.com/phi1618/tmlib.js" }); parent.appendChild(eInfo); }; })(); (function() { if (!window) return ; if (!window.requestAnimationFrame) { window.requestAnimationFrame = window[tm.VENDER_PREFIX + "RequestAnimationFrame"] || function(callback) { window.setTimeout(callback, 1000/60); }; } if (!window.cancelRequestAnimationFrame) { window.cancelRequestAnimationFrame = window[tm.VENDER_PREFIX + "CancelRequestAnimationFrame"] || window.clearTimeout; } })(); (function() { if (!window.document) return ; _loadCheckList = []; tm.addLoadCheckList = function(obj) { console.assert(obj.isLoaded !== undefined, "isLoaded が定義されていません!!"); _loadCheckList.push(obj); }; _preloadListners = []; _mainListners = []; tm.preload = function(fn) { _preloadListners.push(fn); }; tm.main = function(fn) { _mainListners.push(fn); }; var _preload = function() { for (var i=0,len=_preloadListners.length; iend; i+=step, ++index) { this[index] = i; } } return this; }); /** * @method shuffle * シャッフル */ Array.defineInstanceMethod("shuffle", function() { for (var i=0,len=this.length; i b) ? b : x ); }; /** * @property DEG_TO_RAD * Degree to Radian. */ Math.DEG_TO_RAD = Math.PI/180; /** * @property RAD_TO_DEG * Radian to Degree. */ Math.RAD_TO_DEG = 180/Math.PI; /** * @method * Degree を Radian に変換 */ Math.degToRad = function(deg) { return deg * Math.DEG_TO_RAD; }; /** * @method * Radian を Degree に変換 */ Math.radToDeg = function(rad) { return rad * Math.RAD_TO_DEG; }; /** * @method * ランダムな値を指定された範囲内で生成 */ Math.rand = function(min, max) { return window.Math.floor( Math.random()*(max-min+1) ) + min; }; /** * @method * ランダムな値を指定された範囲内で生成 */ Math.randf= function(min, max) { return window.Math.random()*(max-min)+min; }; /** * @method * 長さを取得 */ Math.magnitude = function() { return Math.sqrt(Math.magnitudeSq.apply(null, arguments)); }; /** * @method * 長さの2乗を取得 */ Math.magnitudeSq = function() { var n = 0; for (var i=0,len=arguments.length; i>> 0; }); /** * @method toHex * 16進数化 */ Number.defineInstanceMethod("toHex", function() { return this.toString(16); }); /** * @method toBin * 2進数化 */ Number.defineInstanceMethod("toBin", function() { return this.toString(2); }); /** * @method padding * 文字埋め */ Number.defineInstanceMethod("padding", function(n, ch) { var str = this+''; n = n-str.length; ch = ch || '0'; while(n-- > 0) { str = ch + str; } return str; }); })(); /* * string.js */ (function() { /** * @class String * * `String` is a global object that may be used to construct String instances. */ /** * @method * * フォーマット * * ## example * * document.write("{0} + {1} = {2}".format(5, 10, 5+10)); // "5 + 10 = 15" * document.write("rgb({r}, {g}, {b})".format({ // "rgb(128, 0, 255)" * r: 128, * g: 0, * b: 255 * })); */ String.prototype.format = function(arg) { // 置換ファンク var rep_fn = undefined; // オブジェクトの場合 if (typeof arg == "object") { /** @ignore */ rep_fn = function(m, k) { return arg[k]; } } // 複数引数だった場合 else { var args = arguments; /** @ignore */ rep_fn = function(m, k) { return args[ parseInt(k) ]; } } return this.replace( /\{(\w+)\}/g, rep_fn ); }; /** * @method * トリム * * Reference * */ String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); }; /** * @method * キャピタライズ * * ## Reference * * - [キャピタライズ(単語の先頭の大文字化)を行う - oct inaodu](http://d.hatena.ne.jp/brazil/20051212/1134369083) * - [デザインとプログラムの狭間で: javascriptでキャピタライズ(一文字目を大文字にする)](http://design-program.blogspot.com/2011/02/javascript.html) * */ String.prototype.capitalize = function() { return this.replace(/\w+/g, function(word){ return word.capitalizeFirstLetter(); }); }; /** * @method * 先頭文字のみキャピタライズ */ String.prototype.capitalizeFirstLetter = function() { return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase(); }; /** * @method * ダッシュ */ String.prototype.toDash = function() { return this.replace(/([A-Z])/g, function(m){ return '-'+m.toLowerCase(); }); } /** * @method * ハッシュ値に変換 */ String.prototype.toHash= function() { return TM.crc32(this); }; /** * @method * 左側に指定された文字を詰めて右寄せにする */ String.prototype.padding = function(n, ch) { var str = this.toString(); n = n-str.length; ch = ch || ' '; while(n-- > 0) { str = ch + str; } return str; }; /** * @method * 左側に指定された文字を詰めて右寄せにする */ String.prototype.paddingLeft = String.prototype.padding; /** * @method * 右側に指定された文字を詰めて左寄せにする */ String.prototype.paddingRight = function(n, ch) { var str = this.toString(); n = n-str.length; ch = ch || ' '; while(n-- > 0) { str = str + ch; } return str; }; /** * @method * リピート */ String.prototype.repeat = function(n) { // TODO: 確認する var arr = Array(n); for (var i=0; i>> 8) ^ x; } return (crc ^ (-1)) >>> 0; }; })(); /* * list.js */ (function(){ /** * @class * Item クラス */ tm.Item = tm.createClass({ prev: null, next: null, data: null, /** * 初期化 */ init: function() { } }); /** * @class * List クラス * ### Reference * - * - * - * - * - * - */ tm.List = tm.createClass({ /** * 初期化 */ init: function() { this._length = 0; this._head = tm.Item(); this._tail = tm.Item(); this._head.next = this._tail; this._tail.prev = this._head; }, /** * 追加 */ add: function(data) { var item = tm.Item(); item.data = data; item.prev = this._tail.prev; item.next = this._tail; this._tail.prev.next = item; this._tail.prev = item; ++this._length; return this; }, /** * 削除 */ remove: function(index) { var current = this.getItem(index); current.prev.next = current.next; current.next.prev = current.prev; --this._length; return current; }, /** * 取得 */ get: function(index) { return this.getItem(index).data; }, /** * アイテムを取得 */ getItem: function(index) { var current = this._head.next; var i=0; while (i++ < index) { current = current.next; } return current; }, /** * 繰り返し */ forEach: function(fn) { // TODO: }, /** * クリア */ clear: function() { // TODO: }, /** * クローン */ clone: function() { // TODO: }, /** * 最初の要素を取得 */ getFirst: function() { // TODO: }, /** * 最後の要素を取得 */ getLast: function() { // TODO: }, /** * 最初に一致した位置のインデックスを取得 */ indexOf: function(obj) { // TODO: }, /** * 最後に一致した位置のインデックスを取得 */ lastIndexOf: function(obj) { // TODO: }, /** * 配列に変換 */ toArray: function() { if (this._length <= 0) return []; var current = this._head.next; var arr = []; while (current.data != null) { arr.push(current.data); current = current.next; } return arr; }, /** * 文字列に変換 */ toString: function() { var arr = this.toArray(); for (var i=0,len=arr.length; i * - * - * - * - */ tm.util.Random = { /** * Dummy */ randint: function(min, max) { return window.Math.floor( Math.random()*(max-min+1) ) + min; }, /** * Dummy */ randfloat: function(min, max) { return window.Math.random()*(max-min)+min; }, /** * Dummy */ randbool: function() { return this.randint(0, 1) === 1; }, }; })(); /* * ajax.js */ tm.util = tm.util || {}; (function() { var AJAX_DEFAULT_SETTINGS = { type :"POST", async: true, data: null, contentType: 'application/x-www-form-urlencoded', dataType: 'text', username: null, password: null, success : function(data){ alert("success!!\n"+data); }, error : function(data){ alert("error!!"); } }; tm.util.Ajax = { load: function(params) { for (var key in AJAX_DEFAULT_SETTINGS) { params[key] = params[key] || AJAX_DEFAULT_SETTINGS[key]; } var httpRequest = new XMLHttpRequest(); var ajax_params = ""; var conv_func = tm.util.Ajax.DATA_CONVERTE_TABLE[params.dataType]; // コールバック httpRequest.onreadystatechange = function() { if (httpRequest.readyState == 4) { // 成功 if (httpRequest.status === 200) { // タイプ別に変換をかける var data = conv_func[httpRequest.responseText]; params.success(data); } // status === 0 はローカルファイル用 else if (httpRequest.status === 0) { // タイプ別に変換をかける var data = conv_func(httpRequest.responseText); params.success(data); } else { params.error(httpRequest.responseText); } } else { //console.log("通信中"); } }; httpRequest.open(params.type, params.url, params.async, params.username, params.password); // オープン httpRequest.setRequestHeader('Content-Type', params.contentType); // ヘッダをセット httpRequest.send(null); }, loadJSONP: function(url, callback) { var g = tm.global; g.tmlib_js_dummy_func_count = tm.global.tmlib_js_dummy_func || 0; var dummy_func_name = "tmlib_js_dummy_func" + (g.tmlib_js_dummy_func_count++); g[dummy_func_name] = callback; var elm = document.createElement("script"); elm.type = "text/javascript"; elm.charset = "UTF-8"; elm.src = url + "&callback=" + dummy_func_name; elm.setAttribute("defer", true); document.getElementsByTagName("head")[0].appendChild(elm); } }; /** * データコンバータテーブル */ tm.util.Ajax.DATA_CONVERTE_TABLE = { undefined: function(data) { return data; }, text: function(data) { return data; }, xml: function(data) { var div = document.createElement("div"); div.innerHTML = data; return div; }, dom: function(data) { var div = document.createElement("div"); div.innerHTML = data; return tm.dom.Element(div); }, json: function(data) { return JSON.parse(data); }, script: function(data) { eval(data); return data; }, /** * ### Reference * - * @param {Object} data */ bin: function(data) { var bytearray = []; for (var i=0, len=data.length; i 0) { for (var i=0,len=this.tasks.length; iTest Program. * @example * TM.loadScript("input", "keyboard"); * * TM.main(function() { * var k = TM.$Key(document); * k.run(); * TM.setLoop(function(){ * if (k.getKey('a')) { console.log("press 'a'!!"); } * }); * }); */ init: function(element) { this.element = element || document; this.key = {}; this.press = {}; this.down = {}; this.up = {}; this.last = {}; var self = this; this.element.addEventListener("keydown", function(e){ self.key[e.keyCode] = true; }); this.element.addEventListener("keyup", function(e){ // delete self.key[e.keyCode]; self.key[e.keyCode] = false; // self.button |= 1<Test Program. */ init: function(element) { this.element = element || window.document; this.position = tm.geom.Vector2(0, 0); this.deltaPosition = tm.geom.Vector2(0, 0); this.prevPosition = tm.geom.Vector2(0, 0); var self = this; this.element.addEventListener("touchstart", function(e){ self._touchmove(e); self.prevPosition.setObject(self.position); self.touched = true; }); this.element.addEventListener("touchend", function(e){ self.touched = false; }); this.element.addEventListener("touchmove", function(e){ self._touchmove(e); // 画面移動を止める e.stop(); }); }, /** * run. * 自動でマウス情報を更新したい際に使用する */ run: function(fps) { var self = this; fps = fps || 30; tm.setLoop(function() { self.update(); }, 1000/fps); return this; }, /** * 情報更新処理 * マイフレーム呼んで下さい. */ update: function() { this.last = this.now; this.now = this.touched; this.start = (this.now ^ this.last) & this.now; this.end = (this.now ^ this.last) & this.last; // 変化値を保存 this.deltaPosition.setObject(this.position).sub(this.prevPosition); // 前回の座標を保存 this.prevPosition.setObject(this.position); }, /** * タッチしているかを判定 */ getTouch: function() { return this.touched != 0; }, /** * タッチ開始時に true */ getTouchStart: function() { return this.start != 0; }, /** * タッチ終了時に true */ getTouchEnd: function() { return this.end != 0; }, _touchmove: function(e) { var t = e.touches[0]; this.x = t.pageX; this.y = t.pageY; }, _touchmoveScale: function(e) { var t = e.touches[0]; this.x = t.pageX; this.y = t.pageY; if (e.target.style.width) { this.x *= e.target.width / parseInt(e.target.style.width); } if (e.target.style.height) { this.y *= e.target.height / parseInt(e.target.style.height); } }, }); /** * @property x * x座標値 */ tm.input.Touch.prototype.accessor("x", { "get": function() { return this.position.x; }, "set": function(v) { this.position.x = v; } }); /** * @property y * y座標値 */ tm.input.Touch.prototype.accessor("y", { "get": function() { return this.position.y; }, "set": function(v) { this.position.y = v; } }); /** * @property dx * dx値 */ tm.input.Touch.prototype.accessor("dx", { "get": function() { return this.deltaPosition.x; }, "set": function(v) { this.deltaPosition.x = v; } }); /** * @property dy * dy値 */ tm.input.Touch.prototype.accessor("dy", { "get": function() { return this.deltaPosition.y; }, "set": function(v) { this.deltaPosition.y = v; } }); /** * @method * ポインティング状態取得(mouse との差異対策) */ tm.input.Touch.prototype.getPointing = tm.input.Touch.prototype.getTouch; /** * @method * ポインティングを開始したかを取得(mouse との差異対策) */ tm.input.Touch.prototype.getPointingStart = tm.input.Touch.prototype.getTouchStart; /** * @method * ポインティングを終了したかを取得(mouse との差異対策) */ tm.input.Touch.prototype.getPointingEnd = tm.input.Touch.prototype.getTouchEnd; })(); /* * accelerometer.js */ tm.input = tm.input || {}; (function() { /** * @class * タッチクラス */ tm.input.Accelerometer = tm.createClass({ /** * @constructs * @see Test Program. * * ### Reference * - * - * - * - */ init: function(element) { this.gravity = tm.geom.Vector3(0, 0, 0); this.acceleration = tm.geom.Vector3(0, 0, 0); this.orientation = tm.geom.Vector3(0, 0, 0); var self = this; window.addEventListener("devicemotion", function(e) { var acceleration = self.acceleration; var gravity = self.gravity; acceleration.x = e.acceleration.x; acceleration.y = e.acceleration.y; acceleration.z = e.acceleration.z; gravity.x = e.accelerationIncludingGravity.x; gravity.y = e.accelerationIncludingGravity.y; gravity.z = e.accelerationIncludingGravity.z; }); window.addEventListener("deviceorientation", function(e) { var orientation = self.orientation; orientation.alpha = e.alpha; // z(0~360) orientation.beta = e.beta; // x(-180~180) orientation.gamma = e.gamma; // y(-90~90) }); }, }); })(); /* * color.js */ /* * */ tm.graphics = tm.graphics || {}; (function() { /** * @class カラークラス */ tm.graphics.Color = tm.createClass({ /** * R値 */ r: 255, /** * G値 */ g: 255, /** * B値 */ b: 255, /** * A値 */ a: 1.0, /** * @constructor * 初期化 */ init: function(r, g, b, a) { this.canvas = canvas || document.createElement("canvas"); this.context = this.canvas.getContext("2d"); this.context.lineCap = "round"; this.context.lineJoin = "round"; }, /** * CSS 用 RGBA 文字列に変換 */ toCSSValue: function() { return "rgba({r},{g},{b},{a})".format(this); }, }); })(); /* * canvas.js */ /* * */ tm.graphics = tm.graphics || {}; (function() { /** * キャンバス */ tm.graphics.Canvas = tm.createClass({ /** * 要素 */ element: null, /** * キャンバス */ canvas: null, /** * コンテキスト */ context: null, /** * @constructor * 初期化 */ init: function(canvas) { this.canvas = null; if (typeof canvas == "string") { this.canvas = document.querySelector(canvas); } else { this.canvas = canvas || document.createElement("canvas"); } this.element = this.canvas; this.context = this.canvas.getContext("2d"); this.context.lineCap = "round"; this.context.lineJoin = "round"; }, /** * リサイズする */ resize: function(width, height) { this.canvas.width = width; this.canvas.height = height; return this; }, /** * リサイズウィンドウ */ resizeWindow: function() { this.canvas.style.position = "fixed"; this.canvas.style.margin = "0px"; this.canvas.style.padding = "0px"; this.canvas.style.left = "0px"; this.canvas.style.top = "0px"; return this.resize(window.innerWidth, window.innerHeight); }, /** * フィット */ resizeToFitScreen: function() { this.canvas.style.position = "fixed"; this.canvas.style.margin = "0px"; this.canvas.style.padding = "0px"; this.canvas.style.left = "0px"; this.canvas.style.top = "0px"; return this.resize(window.innerWidth, window.innerHeight); }, /** * 拡縮で画面にフィットさせる * 名前は仮. 検討する */ fitWindow: function(everFlag) { everFlag = everFlag || true; var _fitFunc = function() { var e = this.element; var s = e.style; s.position = "absolute"; s.left = "0px"; s.top = "0px"; var rateWidth = e.width/window.innerWidth; var rateHeight= e.height/window.innerHeight; var rate = e.height/e.width; if (rateWidth > rateHeight) { s.width = innerWidth+"px"; s.height = innerWidth*rate+"px"; } else { s.width = innerHeight/rate+"px"; s.height = innerHeight+"px"; } }.bind(this); // 一度実行しておく _fitFunc(); // リサイズ時のリスナとして登録しておく if (everFlag) { window.addEventListener("resize", _fitFunc, false); } }, /** * クリア */ clear: function(x, y, width, height) { x = x || 0; y = y || 0; width = width || this.width; height= height|| this.height; this.context.clearRect(x, y, width, height); return this; }, /** * 色指定クリア * @param {String} fillStyle * @param {Number} [x=0] * @param {Number} [y=0] * @param {Number} [width=this.width] * @param {Number} [height=this.height] */ clearColor: function(fillStyle, x, y, width, height) { x = x || 0; y = y || 0; width = width || this.width; height= height|| this.height; this.save(); this.resetTransform(); // 行列初期化 this.fillStyle = fillStyle; // 塗りつぶしスタイルセット this.context.fillRect(x, y, width, height); this.restore(); return this; }, /** * パスを開始(リセット) */ beginPath: function() { this.context.beginPath(); return this; }, /** * パスを閉じる */ closePath: function() { this.context.closePath(); return this; }, /** * 新規パス生成 */ moveTo: function(x, y) { this.context.moveTo(x, y); return this; }, /** * パスに追加 */ lineTo: function(x, y) { this.context.lineTo(x, y); return this; }, /** * パス内を塗りつぶす */ fill: function() { this.context.fill(); return this; }, /** * パス上にラインを引く */ stroke: function() { this.context.stroke(); return this; }, /** * クリップ */ clip: function() { this.context.clip(); return this; }, /** * 点描画 */ drawPoint: function(x, y) { return this.strokeRect(x, y, 1, 1); // return this.beginPath().moveTo(x-0.5, y-0.5).lineTo(x+0.5, y+0.5).stroke(); }, /** * ラインパスを作成 */ line: function(x0, y0, x1, y1) { return this.moveTo(x0, y0).lineTo(x1, y1); }, /** * ラインを描画 */ drawLine: function(x0, y0, x1, y1) { return this.beginPath().line(x0, y0, x1, y1).stroke(); }, /** * ダッシュラインを描画 */ drawDashLine: function(x0, y0, x1, y1, pattern) { var patternTable = null; if (typeof(pattern) == "string") { patternTable = pattern; } else { pattern = pattern || 0xf0f0; patternTable = pattern.toString(2); } patternTable = patternTable.padding(16, '1'); var vx = x1-x0; var vy = y1-y0; var len = Math.sqrt(vx*vx + vy*vy); vx/=len; vy/=len; var x = x0; var y = y0; for (var i=0; ihttp://www.w3.org/TR/2010/WD-2dcontext-20100624/#colors-and-styles */ setColorStyle: function(stroke, fill) { fill = fill || stroke; this.context.strokeStyle = stroke; this.context.fillStyle = fill; return this; }, /** * テキストをセット */ setText: function(font, align, baseline) { var c = this.context; c.font = font; c.textAlign = align; c.textBaseline = baseline; }, /** * ラインスタイルを一括セット * @see http://www.w3.org/TR/2010/WD-2dcontext-20100624/#line-styles */ setLineStyle: function(width, cap, join, miter) { with(this.context) { lineWidth = width || 1; lineCap = cap || "round"; lineJoin = join || "round"; miterLimit = miter || 10.0; } return this; }, /** * 影をセット * - * - */ setShadow: function(color, offsetX, offsetY, blur) { var ctx = this.context; ctx.shadowColor = color || "black"; ctx.shadowOffsetX = offsetX || 0; ctx.shadowOffsetY = offsetY || 0; ctx.shadowBlur = blur || 0; return this; }, getElement: function() { return this.element; }, }); tm.graphics.Canvas.MIME_TYPE_PNG = "image/png"; tm.graphics.Canvas.MIME_TYPE_JPG = "image/jpeg"; tm.graphics.Canvas.MIME_TYPE_SVG = "image/svg+xml"; /** * @property width * 幅 */ tm.graphics.Canvas.prototype.accessor("width", { "get": function() { return this.canvas.width; }, "set": function(v) { this.canvas.width = v; } }); /** * @property height * 高さ */ tm.graphics.Canvas.prototype.accessor("height", { "get": function() { return this.canvas.height; }, "set": function(v) { this.canvas.height = v; } }); /** * @property fillStyle * 塗りつぶしスタイル */ tm.graphics.Canvas.prototype.accessor("fillStyle", { "get": function() { return this.context.fillStyle; }, "set": function(v) { this.context.fillStyle = v; } }); /** * @property strokeStyle * ストロークスタイル */ tm.graphics.Canvas.prototype.accessor("strokeStyle", { "get": function() { return this.context.strokeStyle; }, "set": function(v) { this.context.strokeStyle = v; } }); /** * @property globalAlpha * アルファ指定 */ tm.graphics.Canvas.prototype.accessor("globalAlpha", { "get": function() { return this.context.globalAlpha; }, "set": function(v) { this.context.globalAlpha = v; } }); /** * @property globalCompositeOperation * ブレンド指定 */ tm.graphics.Canvas.prototype.accessor("globalCompositeOperation", { "get": function() { return this.context.globalCompositeOperation; }, "set": function(v) { this.context.globalCompositeOperation = v; } }); /** * @property shadowBlur * シャドウブラー */ tm.graphics.Canvas.prototype.accessor("shadowBlur", { "get": function() { return this.context.shadowBlur; }, "set": function(v) { this.context.shadowBlur = v; } }); /** * @property shadowColor * シャドウブラーカラー */ tm.graphics.Canvas.prototype.accessor("shadowColor", { "get": function() { return this.context.shadowColor; }, "set": function(v) { this.context.shadowColor = v; } }); /** * @property lineCap * ライン終端の描画方法 */ tm.graphics.Canvas.prototype.accessor("lineCap", { "get": function() { return this.context.lineCap; }, "set": function(v) { this.context.lineCap = v; } }); /** * @property lineJoin * ラインつなぎ目の描画方法 */ tm.graphics.Canvas.prototype.accessor("lineJoin", { "get": function() { return this.context.lineJoin; }, "set": function(v) { this.context.lineJoin = v; } }); /** * @property miterLimit * マイターリミット */ tm.graphics.Canvas.prototype.accessor("miterLimit", { "get": function() { return this.context.miterLimit; }, "set": function(v) { this.context.miterLimit = v; } }); /** * @property lineWidth * ライン幅設定 */ tm.graphics.Canvas.prototype.accessor("lineWidth", { "get": function() { return this.context.lineWidth; }, "set": function(v) { this.context.lineWidth = v; } }); /** * @property font * フォント */ tm.graphics.Canvas.prototype.accessor("font", { "get": function() { return this.context.font; }, "set": function(v) { this.context.font = v; } }); /** * @property textAlign * テキストのアラインメント */ tm.graphics.Canvas.prototype.accessor("textAlign", { "get": function() { return this.context.textAlign; }, "set": function(v) { this.context.textAlign = v; } }); /** * @property textBaseline * テキストのベースライン */ tm.graphics.Canvas.prototype.accessor("textBaseline", { "get": function() { return this.context.textBaseline; }, "set": function(v) { this.context.textBaseline = v; } }); /** * @property centerX * センターX */ tm.graphics.Canvas.prototype.getter("centerX", function() { return this.canvas.width/2; }); /** * @property centerY * センターY */ tm.graphics.Canvas.prototype.getter("centerY", function(){ return this.canvas.height/2; }); })(); /* * texture.js */ tm.graphics = tm.graphics || {}; (function() { /** * @class * テクスチャクラス */ tm.graphics.Texture = tm.createClass({ element: null, loaded: false, /** * 初期化 */ init: function(src) { this.element = new Image(); this.element.src = src; var self = this; this.element.onload = function() { self.loaded = true; }; }, getElement: function() { return this.element; }, }); /** * @property width * 幅 */ tm.graphics.Texture.prototype.getter("width", function() { return this.element.width; }); /** * @property height * 高さ */ tm.graphics.Texture.prototype.getter("height", function() { return this.element.height; }); })(); (function(){ /** * @class * テクスチャマネージャクラス */ tm.graphics.TextureManager = { textures: {}, loaded: true, }; /** * @static * @method * 追加 */ tm.graphics.TextureManager.add = function(name, src) { if (src === undefined) { src = name; } this.textures[name] = tm.graphics.Texture(src); this.loaded = false; }; /** * @static * @method * 取得 */ tm.graphics.TextureManager.get = function(name) { return this.textures[name]; }; /** * ロードチェック */ tm.graphics.TextureManager.isLoaded = function() { for (var key in this.textures) { if (this.textures[key].loaded == false) { return false; } } return true; }; tm.addLoadCheckList(tm.graphics.TextureManager); })(); /* * bitmap.js */ tm.graphics = tm.graphics || {}; (function() { /** * @class * ビットマップクラス */ tm.graphics.Bitmap = tm.createClass({ imageData: null, /** * 初期化 */ init: function(imageData) { if (arguments.length == 1) { this.imageData = imageData; this.data = imageData.data; } else if (arguments.length == 2) { var w = arguments[0]; var h = arguments[1]; this.imageData = dummyContext.createImageData(w, h); this.data = this.imageData.data; } }, /** * index 指定でピクセル値を取得 * 最も高速 */ getPixelIndex: function(index) { var i = index*4; return [ this.data[i+0], this.data[i+1], this.data[i+2], this.data[i+3] ]; }, /** * x, y 指定でピクセル値を取得 */ getPixelXY: function(x, y) { return this.getPixelIndex( this.posToIndex(x, y) ); }, /** * ピクセル値を取得 * ### Memo * - index 指定か x, y 指定にするか検討中 * - 配列で返すか数値で返すか検討中. 速度の早いやつを採用する */ getPixel: function(x, y) { return this.getPixelIndex( this.posToIndex(x, y) ); }, getPixelAsNumber: function(index) { var i = index*4; return (this.data[i+3] << 24) | (this.data[i+0] << 16) | (this.data[i+1] << 8) | this.data[i+2]; }, getPixelAsObject: function(index) { var i = index*4; return { r: this.data[i+0], g: this.data[i+1], b: this.data[i+2], a: this.data[i+3] }; }, getPixelAsArray: function(index) { var i = index*4; return [ this.data[i+0], this.data[i+1], this.data[i+2], this.data[i+3] ]; }, /** * 指定した範囲内のピクセル平均値を取得 */ getPixelAverage: function(x, y, width, height) { var rgba = [0, 0, 0, 0]; // 範囲 var l = x; var r = x+width; var t = y; var b = y+height; // ハミ出し調整 if (l < 0) { l = 0; } if (r > this.width) { r = this.width; } if (t < 0) { t = 0; } if (b > this.height) { b = this.height; } // 範囲内のピクセル全てを取得 var temp = []; var bitmapWidth = this.width; for (var i=t; i>>16, (pixel & 0x0000ff00)>>>8, (pixel & 0x000000ff)>>>0); }, /** * argb */ setPixel32FromNumber: function(index, pixel) { return this.setPixel32(index, (pixel & 0x00ff0000)>>>16, (pixel & 0x0000ff00)>>>8, (pixel & 0x000000ff)>>>0, (pixel & 0xff000000)>>>24); }, /** * object */ setPixelFromObject: function(index, pixel) { return this.setPixel(pixel.r, pixel.g, pixel.b); }, setPixel32FromObject: function(index, pixel) { return this.setPixel32(pixel.r, pixel.g, pixel.b, pixel.a); }, /** * string * rgb, hsl, #... #...... などに対応予定 */ setPixelFromString: function(index, pixel) { }, /** * 位置をインデックスに変換 */ posToIndex: function(x, y) { return y*this.imageData.width + x; }, // filter: function(rect, filter) filter: function(filter) { for (var i=0; i * - */ tm.graphics.BlurFilter = tm.createClass({ /** * 初期化 */ init: function(blurX, blurY, quality) { this.blurX = blurX || 4; this.blurY = blurY || 4; this.quality = quality || 1; }, /** * apply */ apply: function(src, dst) { var halfX = Math.floor(this.blurX/2); var halfY = Math.floor(this.blurY/2); var rangeX = this.blurX; var rangeY = this.blurY; var srcWidth = src.width; var srcHeight = src.height; var len = src.length; // ブラー処理 var _apply = function(src, dst) { for (var i=0; i * - * - * - * - */ tm.graphics.ColorMatrixFilter = tm.createClass({ /** * 初期化 */ init: function(colorMatrix) { this.colorMatrix = colorMatrix; }, /** * apply */ apply: function(src, dst) { var cm = this.colorMatrix; for (var i=0,len=src.length; i this.duration) { // ループ if (this.isLooping) { this.rewind(); // 座標を更新 this.update(); // イベント開始 this.dispatchEvent(tm.event.TweenEvent("loop", this.time, this.now)); } // 終了 else { this.time = this.duration; // 座標を更新 this.update(); // 停止 this.stop(); // イベント this.dispatchEvent(tm.event.TweenEvent("finish", this.time, this.now)); } } // 更新 else { this.time = time; // 座標を更新 this.update(); } } }); })(); /* * easing */ (function() { /** * イージング * ### Reference * - * - * - * - */ tm.anim.easing = { "default": function(t, b, c, d) { return c*t/d + b; }, linear: function(t, b, c, d) { return c*t/d + b; }, swing: function(t, b, c, d) { return -c *(t/=d)*(t-2) + b; }, easeInQuad: function(t, b, c, d) { return c*(t/=d)*t + b; }, easeOutQuad: function(t, b, c, d) { return -c *(t/=d)*(t-2) + b; }, easeInOutQuad: function(t, b, c, d) { if((t/=d/2) < 1) return c/2*t*t + b; return -c/2 *((--t)*(t-2) - 1) + b; }, easeInCubic: function(t, b, c, d) { return c*(t/=d)*t*t + b; }, easeOutCubic: function(t, b, c, d) { return c*((t=t/d-1)*t*t + 1) + b; }, easeInOutCubic: function(t, b, c, d) { if((t/=d/2) < 1) return c/2*t*t*t + b; return c/2*((t-=2)*t*t + 2) + b; }, easeOutInCubic: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutCubic(t*2, b, c/2, d); return tm.anim.easing.easeInCubic((t*2)-d, b+c/2, c/2, d); }, easeInQuart: function(t, b, c, d) { return c*(t/=d)*t*t*t + b; }, easeOutQuart: function(t, b, c, d) { return -c *((t=t/d-1)*t*t*t - 1) + b; }, easeInOutQuart: function(t, b, c, d) { if((t/=d/2) < 1) return c/2*t*t*t*t + b; return -c/2 *((t-=2)*t*t*t - 2) + b; }, easeOutInQuart: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutQuart(t*2, b, c/2, d); return tm.anim.easing.easeInQuart((t*2)-d, b+c/2, c/2, d); }, easeInQuint: function(t, b, c, d) { return c*(t/=d)*t*t*t*t + b; }, easeOutQuint: function(t, b, c, d) { return c*((t=t/d-1)*t*t*t*t + 1) + b; }, easeInOutQuint: function(t, b, c, d) { if((t/=d/2) < 1) return c/2*t*t*t*t*t + b; return c/2*((t-=2)*t*t*t*t + 2) + b; }, easeOutInQuint: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutQuint(t*2, b, c/2, d); return tm.anim.easing.easeInQuint((t*2)-d, b+c/2, c/2, d); }, easeInSine: function(t, b, c, d) { return -c * Math.cos(t/d *(Math.PI/2)) + c + b; }, easeOutSine: function(t, b, c, d) { return c * Math.sin(t/d *(Math.PI/2)) + b; }, easeInOutSine: function(t, b, c, d) { return -c/2 *(Math.cos(Math.PI*t/d) - 1) + b; }, easeOutInSine: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutSine(t*2, b, c/2, d); return tm.anim.easing.easeInSine((t*2)-d, b+c/2, c/2, d); }, easeInExpo: function(t, b, c, d) { return(t==0) ? b : c * Math.pow(2, 10 *(t/d - 1)) + b - c * 0.001; }, easeOutExpo: function(t, b, c, d) { return(t==d) ? b+c : c * 1.001 *(-Math.pow(2, -10 * t/d) + 1) + b; }, easeInOutExpo: function(t, b, c, d) { if(t==0) return b; if(t==d) return b+c; if((t/=d/2) < 1) return c/2 * Math.pow(2, 10 *(t - 1)) + b - c * 0.0005; return c/2 * 1.0005 *(-Math.pow(2, -10 * --t) + 2) + b; }, easeOutInExpo: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutExpo(t*2, b, c/2, d); return tm.anim.easing.easeInExpo((t*2)-d, b+c/2, c/2, d); }, easeInCirc: function(t, b, c, d) { return -c *(Math.sqrt(1 -(t/=d)*t) - 1) + b; }, easeOutCirc: function(t, b, c, d) { return c * Math.sqrt(1 -(t=t/d-1)*t) + b; }, easeInOutCirc: function(t, b, c, d) { if((t/=d/2) < 1) return -c/2 *(Math.sqrt(1 - t*t) - 1) + b; return c/2 *(Math.sqrt(1 -(t-=2)*t) + 1) + b; }, easeOutInCirc: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutCirc(t*2, b, c/2, d); return tm.anim.easing.easeInCirc((t*2)-d, b+c/2, c/2, d); }, easeInElastic: function(t, b, c, d, a, p) { var s; if(t==0) return b; if((t/=d)==1) return b+c; if(!p) p=d*.3; if(!a || a < Math.abs(c)) { a=c; s=p/4; } else s = p/(2*Math.PI) * Math.asin(c/a); return -(a*Math.pow(2,10*(t-=1)) * Math.sin((t*d-s)*(2*Math.PI)/p )) + b; }, easeOutElastic: function(t, b, c, d, a, p) { var s; if(t==0) return b; if((t/=d)==1) return b+c; if(!p) p=d*.3; if(!a || a < Math.abs(c)) { a=c; s=p/4; } else s = p/(2*Math.PI) * Math.asin(c/a); return(a*Math.pow(2,-10*t) * Math.sin((t*d-s)*(2*Math.PI)/p ) + c + b); }, easeInOutElastic: function(t, b, c, d, a, p) { var s; if(t==0) return b; if((t/=d/2)==2) return b+c; if(!p) p=d*(.3*1.5); if(!a || a < Math.abs(c)) { a=c; s=p/4; } else s = p/(2*Math.PI) * Math.asin(c/a); if(t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin((t*d-s)*(2*Math.PI)/p )) + b; return a*Math.pow(2,-10*(t-=1)) * Math.sin((t*d-s)*(2*Math.PI)/p )*.5 + c + b; }, easeOutInElastic: function(t, b, c, d, a, p) { if(t < d/2) return tm.anim.easing.easeOutElastic(t*2, b, c/2, d, a, p); return tm.anim.easing.easeInElastic((t*2)-d, b+c/2, c/2, d, a, p); }, easeInBack: function(t, b, c, d, s) { if(s == undefined) s = 1.70158; return c*(t/=d)*t*((s+1)*t - s) + b; }, easeOutBack: function(t, b, c, d, s) { if(s == undefined) s = 1.70158; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; }, easeInOutBack: function(t, b, c, d, s) { if(s == undefined) s = 1.70158; if((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; }, easeOutInBack: function(t, b, c, d, s) { if(t < d/2) return tm.anim.easing.easeOutBack(t*2, b, c/2, d, s); return tm.anim.easing.easeInBack((t*2)-d, b+c/2, c/2, d, s); }, easeInBounce: function(t, b, c, d) { return c - tm.anim.easing.easeOutBounce(d-t, 0, c, d) + b; }, easeOutBounce: function(t, b, c, d) { if((t/=d) <(1/2.75)) { return c*(7.5625*t*t) + b; } else if(t <(2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; } else if(t <(2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; } else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; } }, easeInOutBounce: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeInBounce(t*2, 0, c, d) * .5 + b; else return tm.anim.easing.easeOutBounce(t*2-d, 0, c, d) * .5 + c*.5 + b; }, easeOutInBounce: function(t, b, c, d) { if(t < d/2) return tm.anim.easing.easeOutBounce(t*2, b, c/2, d); return tm.anim.easing.easeInBounce((t*2)-d, b+c/2, c/2, d); } }; })(); /* * */ tm.app = tm.app || {}; (function() { /** * @class * アプリケーション用オブジェクトの基底となるクラス */ tm.app.Element = tm.createClass({ superClass: tm.event.EventDispatcher, parent: null, children: null, /** * 初期化 */ init: function() { this.superInit(); this.children = []; this._listeners = {}; }, /** * 親から離れる */ remove: function() { console.assert(this.parent); this.parent.removeChild(this); return this; }, /** * 子供を追加 */ addChild: function(child) { if (child.parent) child.remove(); child.parent = this; this.children.push(child); return child; }, /** * parent に自分を子供として追加 */ addChildTo: function(parent) { parent.addChild(this); // if (this.parent) this.remove(); // this.parent = parent; // parent.children.push(child); return this; }, /** * まとめて追加 * scene 遷移時に子供をごっそり移譲するときなどに使用 * まだ動作確認していない */ addChildren: function(children) { var tempChildren = children.slice(); for (var i=beginIndex,len=tempChildren.length; i 0) { var tempChildren = this.children.slice(); for (var i=0,len=tempChildren.length; i 0) { context.shadowColor = this.shadowColor; context.shadowOffsetX = this.shadowOffsetX; context.shadowOffsetY = this.shadowOffsetY; context.shadowBlur = this.shadowBlur; } // // 座標計算 // var matrix = this.getFinalMatrix(); // var m = matrix.m; // context.setTransform( m[0], m[1], m[3], m[4], m[6], m[7] ); context.translate(this.position.x, this.position.y); context.rotate(this.rotation * Math.DEG_TO_RAD); context.scale(this.scale.x, this.scale.y); this.draw(canvas); // 子供達も実行 if (this.children.length > 0) { var tempChildren = this.children.slice(); for (var i=0,len=tempChildren.length; i */ replaceScene: function(scene) { var e = null; if (this.currentScene) { e = tm.event.Event("exit"); e.app = this; this.currentScene.dispatchEvent(e); } e = tm.event.Event("enter"); e.app = this; this.currentScene = scene; this.currentScene.dispatchEvent(e); }, /** * シーンをプッシュする * ポーズやオブション画面などで使用する */ pushScene: function(scene) { e = tm.event.Event("exit"); e.app = this; this.currentScene.dispatchEvent(e); this._scenes.push(scene); ++this._sceneIndex; e = tm.event.Event("enter"); e.app = this; scene.dispatchEvent(e); }, /** * シーンをポップする * ポーズやオブション画面などで使用する */ popScene: function() { var scene = this._scenes.pop(scene); --this._sceneIndex; e = tm.event.Event("exit"); e.app = this; scene.dispatchEvent(e); // e = tm.event.Event("enter"); e.app = this; this.currentScene.dispatchEvent(e); return scene; }, enableStats: function() { if (window["Stats"]) { // Stats this.stats = new Stats(); // 右上に設定 this.stats.domElement.style.position = "fixed"; this.stats.domElement.style.left = "5px"; this.stats.domElement.style.top = "5px"; document.body.appendChild(this.stats.domElement); } else { console.warn("not defined stats."); } }, enableDatGUI: function() { if (window.dat) { var gui = new dat.GUI(); return gui; } }, start: function() { this.isPlaying = true; }, stop: function() { this.isPlaying = false; }, _update: function() { // デバイス系 Update this.mouse.update(); this.keyboard.update(); this.touch.update(); if (this.isPlaying) { this.currentScene._update(this); ++this.frame; } }, _draw: function() { this.canvas.clearColor(this.background, 0, 0); this.canvas.fillStyle = "white"; this.canvas.strokeStyle = "white"; // 描画は全てのシーン行う for (var i=0, len=this._scenes.length; i 0) { tween.delay -= 1000/app.fps; continue; } var time = tween.time + 1000/app.fps; tween._setTime(time); if (tween.time >= tween.duration) { // 削除 this.tweens.erase(tween); // 全てのアニメーション終了チェック if (this.tweens.length <= 0) { this.isAnimation = false; var e = tm.event.Event("animationend"); this.element.dispatchEvent(e); } } else { tween.update(); } } }, addTween: function(param) { if (!param.target) param.target = this.element; var tween = tm.anim.Tween(param); tween.delay = param.delay || 0; this.tweens.push(tween); if (this.isAnimation == false) { this.isAnimation = true; var e = tm.event.Event("animationstart"); this.element.dispatchEvent(e); } return tween; }, addTweens: function(args) { for (var i=0,len=arguments.length; i * ### Example * tm.social.Twitter.createURL({ * type : "tweet", // タイプ(tweet/retweet/favorite/user) * tweet_id : "210219483959263232", // 対象となる Tweet * in_reply_to : "210219483959263232", // 返信する対象となる Tweet * text : "Test", // テキスト * screen_name : "phi_jp", // スクリーンネーム * hashtags : "javascript,tmlibjs", // ハッシュタグ * url : "http://tmlife.net", // url * via : "phi_jp", // ~から * related : "tmlib.js tmlife", // 関連ワード * }); */ tm.social.Twitter.createURL = function(prop) { var param_string_list = []; for (var key in prop) { if (key == "type") continue; var value = encodeURIComponent(prop[key]); var param_string = key+"="+value; param_string_list.push(param_string); } var url = "{baseURL}/{type}?{param}".format({ baseURL : BASE_URL, type : prop.type, param : param_string_list.join('&'), }); return url; }; })(); (function() { var BASE_URL = "http://api.twitter.com/1/{type}/{kind}.json"; tm.social.Twitter.api = function(type, kind, param, callback) { var url = BASE_URL.format({ type:type, kind:kind }); var qs = tm.util.QueryString.stringify(param); tm.util.Ajax.loadJSONP(url + "?" + qs, callback); }; })(); (function() { var BASE_URL = "http://search.twitter.com/search.json"; tm.social.Twitter.search = function(param, callback) { var url = BASE_URL; var qs = tm.util.QueryString.stringify(param); tm.util.Ajax.loadJSONP(url + "?" + qs, callback); }; })(); (function() { /* * format = xml or json */ var BASE_URL = "http://api.twitter.com/1/statuses/followers.json"; //http://api.twitter.com/1/statuses/followers.json?id=tmlife_jp /** * * user_id ユーザーID * screen_name screen_name * cursor -1 を指定すると先頭から 100 * include_entities true を指定すると entities を取得できる * */ tm.social.Twitter.getFollowers = function(param, callback) { tm.social.Twitter.api("statuses", "followers", param, callback); /* tm.social.Twitter.api("statuses", "public_timeline", param, callback); tm.social.Twitter.api("statuses", "home_timeline", param, callback); tm.social.Twitter.api("statuses", "friends_timeline", param, callback); tm.social.Twitter.api("statuses", "user_timeline", param, callback); tm.social.Twitter.api("statuses", "replies", param, callback); tm.social.Twitter.api("statuses", "mentions", param, callback); */ }; })(); /* * nineleap.js */ tm.social = tm.social || {}; (function() { /** * @class * 9leap ネームスペース */ tm.social.Nineleap = tm.social.Nineleap || {}; var BASE_URL = "http://9leap.net/games/{id}/result?score={score}&result={result}"; tm.social.Nineleap.createURL = function(id, score, result) { return BASE_URL.format({ id : id, score : score, result : result }); }; tm.social.Nineleap.postRanking = function(score, result) { if (location.hostname == 'r.jsgames.jp') { var id = location.pathname.match(/^\/games\/(\d+)/)[1]; location.replace( this.createURL(id, score, result) ); } else { console.warn("9leap に投稿されていません!"); } }; })(); /* * chart.js */ tm.google = tm.google || {}; (function() { /** * @class * チャートネームスペース */ tm.google.Chart = tm.google.Chart || {}; var DYNAMIC_ICONS_BASE_URL = "https://chart.googleapis.com/chart?chst={type}&chld={data}"; var QR_CODE_BASE_URL = "https://chart.googleapis.com/chart?chs={size}&cht={type}&chl={text}&chco={color}"; /** * ダイナミックアイコン * @param {string} type d_bubble_text_small OR d_fnote_title OR chst=d_fnote etc... * @param {string} data * ### Example * tm.google.Chart.createDynamicIcons("d_bubble_icon_text_small", "ski|bb|Hello, world!|FFFFFF|000000"); * tm.google.Chart.createDynamicIcons("d_fnote", "balloon|1|000000|l|Hello, world!"); * tm.google.Chart.createDynamicIcons("d_fnote", "pinned_c|1|000000|l|Hello, world!"); * tm.google.Chart.createDynamicIcons("d_weather", "taped_y|sunny|Barcelona|max+25°C|min+15°C"); * tm.google.Chart.createDynamicIcons("d_simple_text_icon_left", "flag_jp|14|000|flag_jp|24|000|FFF"); */ tm.google.Chart.createDynamicIcons = function(type, data) { // data = encodeURIComponent(data); return DYNAMIC_ICONS_BASE_URL.format({ type:type, data:data }); }; /** * QRCode 生成 * @param {Object} prop * ### Reference * - * - * - * ### Example * tm.google.Chart.createQRCode("160x160", "http://tmlife.net"); * tm.google.Chart.createQRCode("160x160", "Hello, world"); */ tm.google.Chart.createQRCode = function(size, text, color) { text = encodeURIComponent(text); return QR_CODE_BASE_URL.format({ type:"qr", size:size, text:text, }); }; /** * Tex 生成 */ tm.google.Chart.createTeX = function() { //&chco=ff0000ff }; })();