This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
browser.js
96 lines (88 loc) · 2.4 KB
/
browser.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var document = require('global/document');
var hg = require('../../index.js');
var svg = require('virtual-dom/virtual-hyperscript/svg');
var shapes = require('./shapes.js');
var dragEvent = require('./lib/drag-handler.js');
function App() {
return hg.state({
p1: hg.value([100, 100]),
p2: hg.value([200, 200]),
p3: hg.value([100, 200]),
c: hg.value([250, 250]),
p: hg.value([250, 300]),
width: hg.value(800),
height: hg.value(600),
channels: {
movePoint: movePoint
}
});
}
function movePoint(state, data) {
var point = state[data.name]();
state[data.name].set([
point[0] + data.x,
point[1] + data.y
]);
}
App.render = function render(state) {
return svg('svg', {
'width': state.width,
'height': state.height,
'style': { 'border': '1px solid black' }
}, [
svg('text', {
style: {
'-webkit-user-select': 'none',
'-moz-user-select': 'none'
},
x: 20,
y: 20,
'font-size': 20
}, 'The points are draggable'),
rootScene(state)
]);
};
function rootScene(state) {
return svg('g', [
shapes.triangle(state.p1, state.p2, state.p3),
shapes.circle(state.p, state.c),
shapes.segment(state.p, state.c),
shapes.point({
cx: state.c[0],
cy: state.c[1],
'ev-mousedown': dragEvent(state.channels.movePoint, {
name: 'c'
})
}),
shapes.point({
cx: state.p[0],
cy: state.p[1],
'ev-mousedown': dragEvent(state.channels.movePoint, {
name: 'p'
})
}),
shapes.point({
cx: state.p1[0],
cy: state.p1[1],
'ev-mousedown': dragEvent(state.channels.movePoint, {
name: 'p1'
})
}),
shapes.point({
cx: state.p2[0],
cy: state.p2[1],
'ev-mousedown': dragEvent(state.channels.movePoint, {
name: 'p2'
})
}),
shapes.point({
cx: state.p3[0],
cy: state.p3[1],
'ev-mousedown': dragEvent(state.channels.movePoint, {
name: 'p3'
})
})
]);
}
hg.app(document.body, App(), App.render);