Node.js: add Raster support.

This commit is contained in:
Jonathan Puckey 2011-08-21 16:38:06 +02:00
parent 896afefa99
commit 0ee1e80c68
3 changed files with 20 additions and 6 deletions

View file

@ -1,6 +1,7 @@
var fs = require('fs'),
vm = require('vm'),
path = require('path');
path = require('path'),
Canvas = require('canvas');
__dirname = path.resolve(__dirname, '../src/');
@ -10,8 +11,10 @@ var context = vm.createContext({
server: true,
version: 'dev'
},
fs: fs,
// Node Canvas library: https://github.com/learnboost/node-canvas
Canvas: require('canvas'),
Canvas: Canvas,
Image: Canvas.Image,
// Copy over global variables:
console: console,
require: require,

View file

@ -39,7 +39,16 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
// If it's a string, get the element with this id first.
if (typeof object === 'string')
object = document.getElementById(object);
/*#*/ } // options.browser
/*#*/ } else if (options.server) {
// If we're running on the server and it's a string,
// load it from disk:
if (typeof object === 'string') {
// TODO: load images async
var data = fs.readFileSync(object);
object = new Image();
object.src = data;
}
/*#*/ } // options.server
this.setImage(object);
}
this._matrix = new Matrix();
@ -170,8 +179,11 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
if (this._canvas)
CanvasProvider.returnCanvas(this._canvas);
this._image = image;
// TODO: Cross browser compatible?
/*#*/ if (options.browser) {
this._size = new Size(image.naturalWidth, image.naturalHeight);
/*#*/ } else if (options.server) {
this._size = new Size(image.width, image.height);
/*#*/ } // options.server
this._canvas = null;
this._context = null;
this._changed(Change.GEOMETRY);

View file

@ -582,8 +582,7 @@ var View = this.View = PaperScopeItem.extend(/** @lends View# */{
/*#*/ } // options.browser
}, new function() {
/*#*/ if (options.server) {
var fs = require('fs'),
path = require('path');
var path = require('path');
// Utility function that converts a number to a string with
// x amount of padded 0 digits:
function toPaddedString(number, length) {