forked from ericdrowell/KineticJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs-demo.js
70 lines (64 loc) · 1.68 KB
/
nodejs-demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
});
var layer = new Kinetic.Layer();
stage.add(layer);
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();
stage.setSize({
width : 200,
height : 200
});
// check tween works
var tween = new Kinetic.Tween({
node : rect,
duration : 1,
x : -50
});
tween.play();
// After tween we want to convert stage to dataURL
setTimeout(function(){
stage.toDataURL({
callback: function(data){
// Then add result to stage
var img = new Kinetic.window.Image();
img.onload = function() {
var image = new Kinetic.Image({
image : img,
x : 10,
y : 50
});
layer.add(image);
layer.draw();
// save stage image as file
stage.toDataURL({
callback: function(data){
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;
}
});
}, 1050);