update NodeJS example

This commit is contained in:
Лаврёнов Антон 2014-03-04 19:40:06 +08:00
parent a0aa0d9f80
commit e136a69e12
2 changed files with 14 additions and 40 deletions

View File

@ -25,8 +25,8 @@ Run `grunt docs` and see created 'documentation' folder.
#NodeJS #NodeJS
Support of NodeJS is in alpha state! Support of NodeJS is experimental!
And not published in npm. And package is not published in npm yet.
We are using (node-canvas)[https://github.com/LearnBoost/node-canvas] to create canvas element. We are using (node-canvas)[https://github.com/LearnBoost/node-canvas] to create canvas element.
@ -34,37 +34,8 @@ We are using (node-canvas)[https://github.com/LearnBoost/node-canvas] to create
* Run `npm install KineticJS` * Run `npm install KineticJS`
###Example ###Example
```javascript
var fs = require('fs'),
Kinetic = require('KineticJS');
var layer = new Kinetic.Layer({ See file `nodejs-demo.js`.
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);
});
```
#Pull Requests #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`). 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 File

@ -1,6 +1,8 @@
var fs = require('fs'), var fs = require('fs'),
Kinetic = require('./dist/kinetic-dev'); Kinetic = require('./dist/kinetic-dev');
// Create stage. Container parameter is not required in NodeJS.
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
width : 100, width : 100,
height : 100 height : 100
@ -36,26 +38,27 @@ var tween = new Kinetic.Tween({
}); });
tween.play(); tween.play();
// After tween we want to convert stage to dataURL
setTimeout(function(){ setTimeout(function(){
stage.toDataURL({ stage.toDataURL({
callback: function(data){ callback: function(data){
// adding image to stage // Then add result to stage
var img = new Kinetic.window.Image(); var img = new Kinetic.window.Image();
img.onload = function() { img.onload = function() {
var image = new Kinetic.Image({ var image = new Kinetic.Image({
image : img, image : img,
x : 10 x : 10,
y : 50
}); });
layer.add(image); layer.add(image);
layer.draw(); layer.draw();
// save stage to disk // save stage image as file
stage.toDataURL({ stage.toDataURL({
callback: function(data){ callback: function(data){
console.log(1); var base64Data = data.replace(/^data:image\/png;base64,/, '');
var base64Data = data.replace(/^data:image\/png;base64,/,""); fs.writeFile('out.png', base64Data, 'base64', function(err) {
err && console.log(err);
fs.writeFile("out.png", base64Data, 'base64', function(err) { console.log('See out.png');
console.log(err);
}); });
} }
}); });