forked from Raynos/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
64 lines (51 loc) · 1.38 KB
/
canvas.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
'use strict';
var document = require('global/document');
var hg = require('../index.js');
var h = require('../index.js').h;
function CanvasWidget(paint, data) {
if (!(this instanceof CanvasWidget)) {
return new CanvasWidget(paint, data);
}
this.data = data;
this.paint = paint;
}
CanvasWidget.prototype.type = 'Widget';
CanvasWidget.prototype.init = function init() {
var elem = document.createElement('canvas');
this.update(null, elem);
return elem;
};
CanvasWidget.prototype.update = function update(prev, elem) {
var context = elem.getContext('2d');
this.paint(context, this.data);
};
function App() {
return hg.state({
color: hg.value('red'),
channels: {
changeColor: changeColor
}
});
}
function changeColor(state, data) {
state.color.set(data.color);
}
App.render = function renderColor(state) {
var channels = state.channels;
return h('div', [
h('div', [
h('span', state.color + ' '),
h('input', {
'ev-event': hg.sendChange(channels.changeColor),
value: state.color,
name: 'color'
})
]),
CanvasWidget(drawColor, state.color)
]);
};
function drawColor(context, color) {
context.fillStyle = color;
context.fillRect(0, 0, 100, 100);
}
hg.app(document.body, App(), App.render);