I am trying to draw a spiral raster example (link) on a server (running Node.js). However, I am facing an issue where my path is not displayed on the exported frame and I can only see the downloaded image. Probably a silly mistake on my side, however, days of looking through Google and documentation didn't help me to solve an issue.
What I did:
- Add
paper.
prefixes - Changed +/- to corresponding
add()
andsubtract()
- Tried presenting a path within a layer.
Here is my code:
var paper = require('paper');
var fs = require('fs');
var drawer = {};
var canvas = new paper.Canvas(1000, 1000);
paper.setup(canvas);
var layer = paper.project.activeLayer;
drawer.drawSpiral = function(url, filename, fn) {
var url = 'http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png';
var raster = new paper.Raster(url);
raster.onLoad = function() {
raster.fitBounds(paper.view.bounds);
var position = paper.view.center;
var count = 0;
var max = Math.min(raster.bounds.width, raster.bounds.height) * 0.5;
var path = new paper.Path();
path.fillColor = 'black';
path.closed = true;
while ((paper.view.center - position).length < max) {
count++;
var vector = new paper.Point(count * 5, count / 100);
var rot = vector.rotate(90);
var color = raster.getAverageColor(position.add(vector).divide(2));
var value = color ? (1 - color.gray) * 3.7 : 0;
rot.length = Math.max(value, 0.2);
path.add(position.add(vector).subtract(rot));
path.insert(0, position.add(vector).add(rot));
position = position.add(vector);
}
path.smooth();
layer.insertChild(0, path);
layer.fitBounds(paper.view.bounds);
drawer.exportPNG(filename, fn);
};
}
drawer.exportPNG = function(filename, fn) {
out = fs.createWriteStream(__dirname + '/static/img/' + filename + '.png');
stream = canvas.pngStream();
stream.on('data', function(chunk) {
out.write(chunk);
});
stream.on('end', function() {
fn();
});
}
module.exports = drawer;
Invoked, obviously, like this:
var drawer = require('./drawer.js');
drawer.drawSpiral('','abc', function(){});