Permalink
Browse files

update NodeJS example

  • Loading branch information...
lavrton committed Mar 4, 2014
1 parent a0aa0d9 commit e136a69e12d9f5fd3ba212bba00812895e229847
Showing with 15 additions and 41 deletions.
  1. +4 −33 README.md
  2. +11 −8 nodejs-demo.js
View
@@ -25,46 +25,17 @@ Run `grunt docs` and see created 'documentation' folder.
#NodeJS
Support of NodeJS is in alpha state!
And not published in npm.
Support of NodeJS is experimental!
And package is not published in npm yet.
We are using (node-canvas)[https://github.com/LearnBoost/node-canvas] to create canvas element.
* You have to install node-canvas dependencies (https://github.com/LearnBoost/node-canvas/wiki/_pages)[https://github.com/LearnBoost/node-canvas/wiki/_pages]
* Run `npm install KineticJS`
###Example
```javascript
var fs = require('fs'),
Kinetic = require('KineticJS');
var layer = new Kinetic.Layer({
width : 200,
height : 200
});
var rect = new Kinetic.Rect({
width : 100,
height : 100,
x : 50,
y : 50,
fill : 'green'
});
var text = new Kinetic.Text({
text : 'Generated inside node js',
x : 20,
y : 20,
fill : 'black'
});
layer.add(rect).add(text);
layer.draw();
var stream = layer.createPNGStream();
var file = fs.createWriteStream(__dirname + '/helloworld.png');
stream.on('data', function(chunk) {
file.write(chunk);
});
```
See file `nodejs-demo.js`.
#Pull Requests
I'd be happy to review any pull requests that may better the KineticJS project, in particular if you have a bug fix, enhancement, or a new shape (see `src/shapes` for examples). Before doing so, please first make sure that all of the tests pass (`grunt test`).
View
@@ -1,6 +1,8 @@
var fs = require('fs'),
Kinetic = require('./dist/kinetic-dev');
// Create stage. Container parameter is not required in NodeJS.
var stage = new Kinetic.Stage({
width : 100,
height : 100
@@ -36,26 +38,27 @@ var tween = new Kinetic.Tween({
});
tween.play();
// After tween we want to convert stage to dataURL
setTimeout(function(){
stage.toDataURL({
callback: function(data){
// adding image to stage
// Then add result to stage
var img = new Kinetic.window.Image();
img.onload = function() {
var image = new Kinetic.Image({
image : img,
x : 10
x : 10,
y : 50
});
layer.add(image);
layer.draw();
// save stage to disk
// save stage image as file
stage.toDataURL({
callback: function(data){
console.log(1);
var base64Data = data.replace(/^data:image\/png;base64,/,"");
fs.writeFile("out.png", base64Data, 'base64', function(err) {
console.log(err);
var base64Data = data.replace(/^data:image\/png;base64,/, '');
fs.writeFile('out.png', base64Data, 'base64', function(err) {
err && console.log(err);
console.log('See out.png');
});
}
});

0 comments on commit e136a69

Please sign in to comment.