From 0ee1e80c683f74cf8330b8670e134b0b53b4cd65 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Sun, 21 Aug 2011 16:38:06 +0200 Subject: [PATCH] Node.js: add Raster support. --- node.js/index.js | 7 +++++-- src/item/Raster.js | 16 ++++++++++++++-- src/ui/View.js | 3 +-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/node.js/index.js b/node.js/index.js index cc4c97db..a619f911 100644 --- a/node.js/index.js +++ b/node.js/index.js @@ -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, diff --git a/src/item/Raster.js b/src/item/Raster.js index c72c659f..5651d4f9 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -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); diff --git a/src/ui/View.js b/src/ui/View.js index 06270c9b..a1bb54cd 100644 --- a/src/ui/View.js +++ b/src/ui/View.js @@ -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) {