konva/resources/nodejs-demo.js

70 lines
1.7 KiB
JavaScript
Raw Normal View History

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({
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();
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();
stage.setSize({
width : 200,
height : 200
});
// check tween works
2015-01-27 15:07:51 +08:00
var tween = new Konva.Tween({
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
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();
img.onload = function() {
2015-01-27 15:07:51 +08:00
var image = new Konva.Image({
image : img,
2014-03-04 19:40:06 +08:00
x : 10,
y : 50
});
layer.add(image);
layer.draw();
2014-03-04 19:40:06 +08:00
// save stage image as file
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');
});
}
});
};
img.src = data;
2014-02-27 19:55:39 +08:00
}
});
}, 1050);