-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
54 lines (50 loc) · 1.2 KB
/
example.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
var h = require("virtual-dom/h");
var mainLoop = require("main-loop");
var mkWidget = require("./");
var createElement = require("virtual-dom/create-element");
var CanvasThing = mkWidget(function(props){
var canvas = createElement(h("canvas", {
width: 100,
height: 100,
style: {
border: "1px solid black"
}
}));
var ctx = canvas.getContext("2d");
var x = 0;
var y = 0;
var handle = setInterval(function(){
x++;
y++;
ctx.strokeRect(x, y, 10, 10);
}, 100);
return {
element: canvas,
destroy: function(){
clearInterval(handle);
}
};
});
var render = function(state){
var things = [];
while(things.length < state.count){
things.push(CanvasThing(state));
}
return h("div", [
h("div", [
h("button", {onclick: function(){
loop.update({count: state.count + 1});
}}, "Add Canvas"),
h("button", {onclick: function(){
loop.update({count: 0});
}}, "Clear Canvases")
]),
things
]);
};
var loop = mainLoop({count: 1}, render, {
create: require("virtual-dom/create-element"),
diff: require("virtual-dom/diff"),
patch: require("virtual-dom/patch")
});
document.body.appendChild(loop.target);