mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Node.js: add Raster support.
This commit is contained in:
parent
896afefa99
commit
0ee1e80c68
3 changed files with 20 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
vm = require('vm'),
|
vm = require('vm'),
|
||||||
path = require('path');
|
path = require('path'),
|
||||||
|
Canvas = require('canvas');
|
||||||
|
|
||||||
__dirname = path.resolve(__dirname, '../src/');
|
__dirname = path.resolve(__dirname, '../src/');
|
||||||
|
|
||||||
|
@ -10,8 +11,10 @@ var context = vm.createContext({
|
||||||
server: true,
|
server: true,
|
||||||
version: 'dev'
|
version: 'dev'
|
||||||
},
|
},
|
||||||
|
fs: fs,
|
||||||
// Node Canvas library: https://github.com/learnboost/node-canvas
|
// Node Canvas library: https://github.com/learnboost/node-canvas
|
||||||
Canvas: require('canvas'),
|
Canvas: Canvas,
|
||||||
|
Image: Canvas.Image,
|
||||||
// Copy over global variables:
|
// Copy over global variables:
|
||||||
console: console,
|
console: console,
|
||||||
require: require,
|
require: require,
|
||||||
|
|
|
@ -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 it's a string, get the element with this id first.
|
||||||
if (typeof object === 'string')
|
if (typeof object === 'string')
|
||||||
object = document.getElementById(object);
|
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.setImage(object);
|
||||||
}
|
}
|
||||||
this._matrix = new Matrix();
|
this._matrix = new Matrix();
|
||||||
|
@ -170,8 +179,11 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
if (this._canvas)
|
if (this._canvas)
|
||||||
CanvasProvider.returnCanvas(this._canvas);
|
CanvasProvider.returnCanvas(this._canvas);
|
||||||
this._image = image;
|
this._image = image;
|
||||||
// TODO: Cross browser compatible?
|
/*#*/ if (options.browser) {
|
||||||
this._size = new Size(image.naturalWidth, image.naturalHeight);
|
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._canvas = null;
|
||||||
this._context = null;
|
this._context = null;
|
||||||
this._changed(Change.GEOMETRY);
|
this._changed(Change.GEOMETRY);
|
||||||
|
|
|
@ -582,8 +582,7 @@ var View = this.View = PaperScopeItem.extend(/** @lends View# */{
|
||||||
/*#*/ } // options.browser
|
/*#*/ } // options.browser
|
||||||
}, new function() {
|
}, new function() {
|
||||||
/*#*/ if (options.server) {
|
/*#*/ if (options.server) {
|
||||||
var fs = require('fs'),
|
var path = require('path');
|
||||||
path = require('path');
|
|
||||||
// Utility function that converts a number to a string with
|
// Utility function that converts a number to a string with
|
||||||
// x amount of padded 0 digits:
|
// x amount of padded 0 digits:
|
||||||
function toPaddedString(number, length) {
|
function toPaddedString(number, length) {
|
||||||
|
|
Loading…
Reference in a new issue