diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13a060f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +._* +Thumbs.db +node_modules diff --git a/package.json b/package.json new file mode 100644 index 0000000..05587b8 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "qcanver", + "description": "Canvas API wrapper and some useful functions.", + "version": "0.0.1", + "author": "daisun", + "maintainers": [{ + "name": "Seiya Konno", + "email": "nulltask@gmail.com" + }], + "keywords": ["isomorphic", "canvas", "wrapper"], + "dependencies": { + "canvas": "0.x" + }, + "repository": "git@github.com:nulltask/qcanver.git", + "devDependencies": { + "mocha": "*", + "express": "2.x", + "jade": "0.x" + }, + "engines": { + "node": ">= 0.6.0 && < 0.7.0" + }, + "main": "./qcanver.js" +} diff --git a/qcanver.js b/qcanver.js index 9f4d35e..f41bc24 100755 --- a/qcanver.js +++ b/qcanver.js @@ -1,4 +1,4 @@ -(function (window) { +(function (global) { var qCanver = (function (undefined) { var slice = Array.prototype.slice, toString = Object.prototype.toString, @@ -39,7 +39,7 @@ var qCanver = (function (undefined) { qCanver = (function (_QCanver) { var cache = {}, qCanver = function(id, noCache) { - var canvas = document.getElementById(id); + var canvas = Object.prototype.toString.call(id) === '[object String]' ? document.getElementById(id) : id; if (canvas && canvas.getContext) { if (!noCache) { if (cache[id]) { @@ -54,15 +54,20 @@ var qCanver = (function (undefined) { qCanver.Version = "0.0.1"; qCanver.create = function (id, width, height) { - var canvas = document.createElement("canvas"); - canvas.setAttribute("id", id); - if (qCanver.type(width) === "number") { - canvas.setAttribute("width", width); - } - if (qCanver.type(height) === "number") { - canvas.setAttribute("height", height); + var canvas; + if (qCanver.isWindow(global)) { + canvas = document.createElement("canvas"); + canvas.setAttribute("id", id); + if (qCanver.type(width) === "number") { + canvas.setAttribute("width", width); + } + if (qCanver.type(height) === "number") { + canvas.setAttribute("height", height); + } + } else { + var Canvas = require('canvas'); + canvas = new Canvas(width, height); } - return new _QCanver(id, canvas); }; qCanver.extend = function (/*(name, f) | obj*/) { @@ -602,10 +607,20 @@ var qCanver = (function (undefined) { } return this; + }, + isWindow: function(obj) { + return obj != null && obj == obj.window; } }); })(qCanver); +if (typeof exports !== 'undefined') { + if (typeof module !== 'undefined' && module.exports) { + exports = module.exports = qCanver; + } + exports.qCanver = qCanver; +} else { + global['qCanver'] = qCanver; +} - window.qCanver = qCanver; -})(window); \ No newline at end of file +})(this); \ No newline at end of file