-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (111 loc) · 3.21 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const resl = require('resl')
const css = require('dom-css')
const fit = require('canvas-fit')
const control = require('control-panel')
// setup canvas and camera
const canvas = document.body.appendChild(document.createElement('canvas'))
const regl = require('regl')(canvas)
const camera = require('./camera.js')(canvas, {scale: true, rotate: true})
window.addEventListener('resize', fit(canvas), false)
// import draw functions
const drawBackground = require('./draw/background')(regl)
const drawSpots = require('./draw/spots')(regl)
const drawRegions = require('./draw/regions')(regl)
const drawOutlines = require('./draw/outlines')(regl)
// setup control panel and state
var state = {
color: [0.6, 0.2, 0.9],
showSpots: true,
showRegions: true
}
var panel = control([
{type: 'color', label: 'dot color', format: 'array', initial: state.color},
{type: 'checkbox', label: 'show spots', initial: state.showSpots},
{type: 'checkbox', label: 'show regions', initial: state.showRegions}
],
{theme: 'dark', position: 'top-left'}
)
panel.on('input', function (data) {
state.color = typeof(data['dot color']) == 'string' ? state.color : data['dot color']
state.showSpots = data['show spots']
state.showRegions = data['show regions']
})
// load assets and draw
resl({
manifest: {
'background': {
type: 'image',
src: '../example_2/background.png'
},
'spots': {
type: 'text',
src: '../example_2/spots.json',
parser: JSON.parse
},
'regions': {
type: 'text',
src: '../example_2/regions.json',
parser: JSON.parse
}
},
onDone: ({background, spots, regions}) => {
const width = background.naturalWidth
const height = background.naturalHeight
const scale = height/width
const texture = regl.texture(background)
var xy
const positions = spots.map(function (spot) {
xy = spot.geometry.coordinates
return [xy[1] / (width / 2) - 1.0, xy[0] / (height / 2) - 1]
})
const vertices = regions.map(function (region) {
return region.geometry.coordinates.map(function (xy) {
return [xy[1] / (width / 2) - 1.0, xy[0] / (height / 2) - 1]
})
})
regl.frame(() => {
regl.clear({
depth: 1,
color: [0, 0, 0, 1]
})
if (state.showSpots) {
drawSpots({
distance: camera.distance,
color: state.color,
positions: positions,
count: positions.length,
view: camera.view(),
scale: scale
})
}
drawBackground({
background: texture,
view: camera.view(),
scale: scale
})
if (state.showRegions) {
drawRegions(vertices.map(function (v) {
return {
distance: camera.distance,
color: state.color,
vertices: v,
count: v.length,
view: camera.view(),
scale: scale
}})
)
drawOutlines(vertices.map(function (v) {
return {
distance: camera.distance,
color: state.color,
vertices: v,
count: v.length,
view: camera.view(),
scale: scale
}})
)
}
camera.tick()
})
}
})