Node.js: Fix bug when using paper-core.js

And improve Node.js examples. Closes #975
This commit is contained in:
Jürg Lehni 2016-02-15 23:59:31 +01:00
parent 16a7baa967
commit c35814f94f
9 changed files with 51 additions and 49 deletions

View file

@ -1,7 +1,7 @@
// Please note: When loading paper as a normal module installed in node_modules,
// you would use this instead:
// var paper = require('paper');
var paper = require('../../dist/paper-full.js');
var paper = require('../../dist/paper-core.js');
paper.setup(new paper.Size(1024, 768));
var layer = paper.project.activeLayer;

View file

@ -1,7 +1,7 @@
// Please note: When loading paper as a normal module installed in node_modules,
// you would use this instead:
// var paper = require('paper');
var paper = require('../../dist/paper-full.js');
var paper = require('../../dist/paper-core.js');
var http = require('http');
http.createServer(function(request, response) {

View file

@ -1,7 +1,7 @@
// Please note: When loading paper as a normal module installed in node_modules,
// you would use this instead:
// var paper = require('paper');
var paper = require('../../dist/paper-full.js');
var paper = require('../../dist/paper-core.js');
var path = require('path');
var fs = require('fs');
@ -12,7 +12,7 @@ fs.readFile('./in.json', { encoding: 'utf8' }, function (err, data) {
throw err;
paper.project.importJSON(data);
paper.view.update();
fs.writeFile(path.resolve(__dirname, 'out.pdf'), canvas.toBuffer(), function (err) {
fs.writeFile(path.resolve('./out.pdf'), canvas.toBuffer(), function (err) {
if (err)
throw err;
console.log('Saved!');

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
// Please note: When loading paper as a normal module installed in node_modules,
// you would use this instead:
// var paper = require('paper');
var paper = require('../../dist/paper-full.js');
var paper = require('../../dist/paper-core.js');
var fs = require('fs');
var canvas = paper.createCanvas(800, 600);
@ -12,6 +12,7 @@ var raster = new paper.Raster(url);
raster.position = paper.view.center;
raster.onLoad = function() {
paper.view.update();
console.log('The image has loaded:' + raster.bounds);
// Saving the canvas to a file.

View file

@ -1,7 +1,7 @@
// Please note: When loading paper as a normal module installed in node_modules,
// you would use this instead:
// var paper = require('paper');
var paper = require('../../dist/paper-full.js');
var paper = require('../../dist/paper-core.js');
var path = require('path');
var fs = require('fs');
@ -45,7 +45,7 @@ with (paper) {
var svg = project.exportSVG({ asString: true });
console.log(svg);
fs.writeFile(path.resolve(__dirname, 'out.svg'),svg, function (err) {
fs.writeFile(path.resolve('./out.svg'),svg, function (err) {
if (err) throw err;
console.log('Saved!');
});

View file

@ -1,12 +1,12 @@
// Please note: When loading paper as a normal module installed in node_modules,
// you would use this instead:
// var paper = require('paper');
var paper = require('../../dist/paper-full.js');
var paper = require('../../dist/paper-core.js');
var path = require('path');
var fs = require('fs');
paper.setup(new paper.Size(300, 600));
paper.project.importSVG(/*'in.svg'*/'file://' + path.resolve(__dirname, 'in.svg'), {
paper.project.importSVG(/*'in.svg'*/'file://' + path.resolve('./in.svg'), {
onLoad: function(item) {
paper.view.exportFrames({
amount: 1,

View file

@ -46,7 +46,7 @@
"gulp-cached": "^1.1.0",
"gulp-git-streamed": "^1.0.0",
"gulp-jshint": "^2.0.0",
"gulp-prepro": "^2.3.0",
"gulp-prepro": "^2.4.0",
"gulp-qunits": "^2.0.1",
"gulp-rename": "^1.2.2",
"gulp-shell": "^0.5.2",
@ -60,7 +60,7 @@
"jshint": "2.8.x",
"jshint-summary": "^0.4.0",
"merge-stream": "^1.0.0",
"prepro": "^2.3.0",
"prepro": "^2.4.0",
"qunitjs": "^1.20.0",
"require-dir": "^0.3.0",
"resemblejs": "^2.1.0",

View file

@ -12,46 +12,47 @@
var fs = require('fs'),
path = require('path');
Canvas = require('canvas');
module.exports = function(paper) {
var sourceMaps = {},
sourceMapSupprt = 'require("source-map-support").install(paper.PaperScript.sourceMapSupport);\n';
if (paper.PaperScript) {
var sourceMapSupprt = 'require("source-map-support").install(paper.PaperScript.sourceMapSupport);\n',
sourceMaps = {};
paper.PaperScript.sourceMapSupport = {
retrieveSourceMap: function(source) {
var map = sourceMaps[source];
return map ? { url: source, map: map } : null;
}
};
// Register the .pjs extension for automatic compilation as PaperScript
require.extensions['.pjs'] = function(module, filename) {
// Requiring a PaperScript on Node.js returns an initialize method which
// needs to receive a Canvas object when called and returns the
// PaperScope.
module.exports = function(canvas) {
var source = fs.readFileSync(filename, 'utf8'),
code = sourceMapSupprt + source,
compiled = paper.PaperScript.compile(code, {
url: filename,
source: source,
sourceMaps: true,
offset: -1 // remove sourceMapSupprt...
}),
scope = new paper.PaperScope();
// Keep track of sourceMaps so retrieveSourceMap() can link them up
scope.setup(canvas);
scope.__filename = filename;
scope.__dirname = path.dirname(filename);
// Expose core methods and values
scope.require = require;
scope.console = console;
sourceMaps[filename] = compiled.map;
paper.PaperScript.execute(compiled, scope);
return scope;
paper.PaperScript.sourceMapSupport = {
retrieveSourceMap: function(source) {
var map = sourceMaps[source];
return map ? { url: source, map: map } : null;
}
};
};
// Register the .pjs extension for automatic compilation as PaperScript
require.extensions['.pjs'] = function(module, filename) {
// Requiring a PaperScript on Node.js returns an initialize method which
// needs to receive a Canvas object when called and returns the
// PaperScope.
module.exports = function(canvas) {
var source = fs.readFileSync(filename, 'utf8'),
code = sourceMapSupprt + source,
compiled = paper.PaperScript.compile(code, {
url: filename,
source: source,
sourceMaps: true,
offset: -1 // remove sourceMapSupprt...
}),
scope = new paper.PaperScope();
// Keep track of sourceMaps so retrieveSourceMap() can link them up
scope.setup(canvas);
scope.__filename = filename;
scope.__dirname = path.dirname(filename);
// Expose core methods and values
scope.require = require;
scope.console = console;
sourceMaps[filename] = compiled.map;
paper.PaperScript.execute(compiled, scope);
return scope;
};
};
}
paper.PaperScope.inject({
createCanvas: function(width, height, type) {