@abi-software/flatmap-viewer 2.2.2-beta.1 → 2.2.2-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.rst CHANGED
@@ -38,7 +38,7 @@ The map server endpoint is specified as ``MAP_ENDPOINT`` in ``src/main.js``. It
38
38
  Package Installation
39
39
  ====================
40
40
 
41
- * ``npm install @abi-software/flatmap-viewer@2.2.2-beta.1``
41
+ * ``npm install @abi-software/flatmap-viewer@2.2.2-beta.2``
42
42
 
43
43
  Documentation
44
44
  -------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abi-software/flatmap-viewer",
3
- "version": "2.2.2-beta.1",
3
+ "version": "2.2.2-beta.2",
4
4
  "description": "Flatmap viewer using Maplibre GL",
5
5
  "repository": "https://github.com/AnatomicMaps/flatmap-viewer.git",
6
6
  "main": "src/main.js",
package/src/editor.js ADDED
@@ -0,0 +1,198 @@
1
+ import { Bezier } from "bezier-js";
2
+
3
+
4
+ class BezierCurve
5
+ {
6
+ constructor(id, ...points) {
7
+ this.__id = id;
8
+ this.__bezier = new Bezier(...points);
9
+ }
10
+
11
+ asGeoJSON(samples=100)
12
+ {
13
+ const coords = [];
14
+ for (let ts = 0; ts <= samples; ts++) {
15
+ const pt = this.__bezier.get(float(ts)/float(samples));
16
+ coords.push([pt.x, pt.y]);
17
+ }
18
+ return {
19
+ 'type': 'Feature',
20
+ 'geometry': {
21
+ 'type': 'LineString',
22
+ 'coordinates': coords
23
+ },
24
+ 'properties': {
25
+ 'bezier': this.__id
26
+ }
27
+ };
28
+ }
29
+
30
+ asPoints()
31
+ {
32
+ const geojson = [];
33
+ for (const n of [0, 1, 2, 3]) {
34
+ const pt = this.__bezier.points[n];
35
+ geojson.push({
36
+ 'type': 'Feature',
37
+ 'geometry': {
38
+ 'type': 'Point',
39
+ 'coordinates': [pt.x, pt.y]
40
+ },
41
+ 'properties': {
42
+ 'bezier': this.__id,
43
+ 'point': n
44
+ }
45
+ });
46
+ }
47
+ return geojson;
48
+ }
49
+ }
50
+
51
+
52
+ export class NetworkEditor
53
+ {
54
+ constructor(flatmap)
55
+ {
56
+ this.__map = flatmap.map;
57
+ this.__canvas = this.__map.getCanvasContainer();
58
+
59
+ this.__geojson = { // lines and points as separate sources???
60
+ 'type': 'FeatureCollection',
61
+ 'features': [
62
+ {
63
+ 'type': 'Feature',
64
+ 'geometry': {
65
+ 'type': 'Point',
66
+ 'coordinates': [0, 0]
67
+ },
68
+ 'properties': {
69
+ 'id': 0
70
+ }
71
+ }
72
+ ]
73
+ };
74
+
75
+ // Add a single point to the map
76
+ this.__map.addSource('curves', {
77
+ 'type': 'geojson',
78
+ 'data': this.__geojson
79
+ });
80
+
81
+ this.__map.addLayer({
82
+ 'id': 'lines',
83
+ 'type': 'line',
84
+ 'source': 'curves',
85
+ 'paint': {
86
+ 'line-color': '#3887be'
87
+ }
88
+ });
89
+ this.__map.addLayer({
90
+ 'id': 'points',
91
+ 'type': 'circle',
92
+ 'source': 'curves',
93
+ 'paint': {
94
+ 'circle-radius': 10,
95
+ 'circle-color': '#3887be'
96
+ }
97
+ });
98
+
99
+ this.__currentPoint = null;
100
+
101
+ this.__map.on('mouseenter', this.mouseEnter.bind(this));
102
+ this.__map.on('mouseleave', this.mouseLeave.bind(this));
103
+ this.__map.on('mousedown', this.mouseDown.bind(this));
104
+ this.__map.on('touchstart', this.touchStart.bind(this));
105
+ }
106
+
107
+ addPoint(coords)
108
+ {
109
+ const nextId = this.__geojson.features.length;
110
+ this.__geojson.features.push({
111
+ 'type': 'Feature',
112
+ 'geometry': {
113
+ 'type': 'Point',
114
+ 'coordinates': coords
115
+ },
116
+ 'properties': {
117
+ 'id': nextId
118
+ }
119
+ });
120
+ this.__map.getSource('curves').setData(this.__geojson);
121
+ return nextId;
122
+ }
123
+
124
+ mouseEnter(e) {
125
+ console.log('Mouse enter...');
126
+
127
+ this.__map.setPaintProperty('lines', 'line-color', '#3bb2d0');
128
+ this.__map.setPaintProperty('points', 'circle-color', '#3bb2d0');
129
+ this.__canvas.style.cursor = 'move';
130
+ }
131
+
132
+
133
+ mouseLeave(e) {
134
+ this.__map.setPaintProperty('lines', 'line-color', '#3887be');
135
+ this.__map.setPaintProperty('points', 'circle-color', '#3887be');
136
+ this.__canvas.style.cursor = '';
137
+ }
138
+
139
+
140
+ mouseDown(e) {
141
+ console.log('Mouse down...')
142
+ // Prevent the default map drag behavior.
143
+ e.preventDefault();
144
+
145
+
146
+ const features = this.__map.queryRenderedFeatures(e.point, {'layers': ['lines', 'points']});
147
+ if (features.length === 0) {
148
+ const coords = e.lngLat;
149
+ this.__currentPoint = this.addPoint([coords.lng, coords.lat]);
150
+ } else {
151
+ const currentPoint = features[0].properties.id;
152
+ if (this.__currentPoint === null) {
153
+ this.__currentPoint = currentPoint;
154
+ } else if (this.__currentPoint === currentPoint) {
155
+ this.__currentPoint = null;
156
+ }
157
+ }
158
+
159
+ this.__canvas.style.cursor = 'grab';
160
+
161
+ this.__map.on('mousemove', this.onMove.bind(this));
162
+ this.__map.once('mouseup', this.onUp.bind(this));
163
+
164
+ }
165
+
166
+ touchStart(e) {
167
+ if (e.points.length !== 1) return;
168
+
169
+ // Prevent the default map drag behavior.
170
+ e.preventDefault();
171
+
172
+ this.__map.on('touchmove', this.onMove.bind(this));
173
+ this.__map.once('touchend', this.onUp.bind(this));
174
+ }
175
+
176
+ onMove(e) {
177
+ console.log('Mouse move...')
178
+
179
+ // Set a UI indicator for dragging.
180
+ this.__canvas.style.cursor = 'grabbing'; // ????
181
+
182
+ if (this.__currentPoint !== null) {
183
+ const coords = e.lngLat;
184
+ this.__geojson.features[this.__currentPoint].geometry.coordinates = [coords.lng, coords.lat];
185
+ this.__map.getSource('curves').setData(this.__geojson);
186
+ }
187
+ }
188
+
189
+ onUp(e) {
190
+ console.log('End draw at', e.lngLat)
191
+
192
+ this.__canvas.style.cursor = '';
193
+
194
+ // Unbind mouse/touch events
195
+ this.__map.off('mousemove', this.onMove.bind(this));
196
+ this.__map.off('touchmove', this.onMove.bind(this));
197
+ }
198
+ }
@@ -183,6 +183,10 @@ li.flatmap-contextmenu-item:hover {
183
183
  font-size: 10pt;
184
184
  cursor: pointer;
185
185
  text-align: left;
186
+ background: white;
187
+ border: 1px solid gray;
188
+ padding: 4px;
189
+ opacity: 0.8;
186
190
  }
187
191
  .flatmap-nerve-grid input {
188
192
  height: 1.1em;