2014-02-27 19:55:39 +08:00
|
|
|
var fs = require('fs'),
|
2015-03-13 23:33:48 +08:00
|
|
|
Konva = require('../dist/konva-dev');
|
2014-02-27 19:55:39 +08:00
|
|
|
|
2014-03-04 19:40:06 +08:00
|
|
|
|
|
|
|
// Create stage. Container parameter is not required in NodeJS.
|
2015-01-27 15:07:51 +08:00
|
|
|
var stage = new Konva.Stage({
|
2014-02-28 10:37:57 +08:00
|
|
|
width : 100,
|
|
|
|
height : 100
|
2014-02-27 19:55:39 +08:00
|
|
|
});
|
|
|
|
|
2015-01-27 15:07:51 +08:00
|
|
|
var layer = new Konva.Layer();
|
2014-02-28 10:37:57 +08:00
|
|
|
stage.add(layer);
|
2015-01-27 15:07:51 +08:00
|
|
|
var rect = new Konva.Rect({
|
2014-02-27 19:55:39 +08:00
|
|
|
width : 100,
|
|
|
|
height : 100,
|
|
|
|
x : 50,
|
|
|
|
y : 50,
|
|
|
|
fill : 'green'
|
|
|
|
});
|
2015-01-27 15:07:51 +08:00
|
|
|
var text = new Konva.Text({
|
2014-02-27 19:55:39 +08:00
|
|
|
text : 'Generated inside node js',
|
|
|
|
x : 20,
|
|
|
|
y : 20,
|
|
|
|
fill : 'black'
|
|
|
|
});
|
|
|
|
layer.add(rect).add(text);
|
|
|
|
layer.draw();
|
2014-02-28 10:37:57 +08:00
|
|
|
stage.setSize({
|
|
|
|
width : 200,
|
|
|
|
height : 200
|
|
|
|
});
|
|
|
|
|
|
|
|
// check tween works
|
2015-01-27 15:07:51 +08:00
|
|
|
var tween = new Konva.Tween({
|
2014-02-28 10:37:57 +08:00
|
|
|
node : rect,
|
|
|
|
duration : 1,
|
|
|
|
x : -50
|
|
|
|
});
|
|
|
|
tween.play();
|
|
|
|
|
2014-03-04 19:40:06 +08:00
|
|
|
// After tween we want to convert stage to dataURL
|
2014-02-28 10:37:57 +08:00
|
|
|
setTimeout(function(){
|
|
|
|
stage.toDataURL({
|
|
|
|
callback: function(data){
|
2014-03-04 19:40:06 +08:00
|
|
|
// Then add result to stage
|
2015-01-27 15:07:51 +08:00
|
|
|
var img = new Konva.window.Image();
|
2014-02-28 10:37:57 +08:00
|
|
|
img.onload = function() {
|
2015-01-27 15:07:51 +08:00
|
|
|
var image = new Konva.Image({
|
2014-02-28 10:37:57 +08:00
|
|
|
image : img,
|
2014-03-04 19:40:06 +08:00
|
|
|
x : 10,
|
|
|
|
y : 50
|
2014-02-28 10:37:57 +08:00
|
|
|
});
|
|
|
|
layer.add(image);
|
|
|
|
layer.draw();
|
2014-03-04 19:40:06 +08:00
|
|
|
// save stage image as file
|
2014-02-28 10:37:57 +08:00
|
|
|
stage.toDataURL({
|
|
|
|
callback: function(data){
|
2014-03-04 19:40:06 +08:00
|
|
|
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');
|
2014-02-28 10:37:57 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
img.src = data;
|
2014-02-27 19:55:39 +08:00
|
|
|
|
2014-02-28 10:37:57 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1050);
|