@oicl/openbridge-webcomponents-full-bundle 2.2.0-next.24 → 2.2.0-next.25
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/bundle/openbridge-webcomponents.bundle.js +528 -198
- package/bundle/openbridge-webcomponents.bundle.js.map +1 -1
- package/custom-elements.json +271 -0
- package/dist/navigation-instruments/attitude/attitude.css_inline.js +64 -0
- package/dist/navigation-instruments/attitude/attitude.css_inline.js.map +1 -0
- package/dist/navigation-instruments/attitude/attitude.d.ts +121 -0
- package/dist/navigation-instruments/attitude/attitude.d.ts.map +1 -0
- package/dist/navigation-instruments/attitude/attitude.js +255 -0
- package/dist/navigation-instruments/attitude/attitude.js.map +1 -0
- package/dist/navigation-instruments/watch/tickmark.d.ts.map +1 -1
- package/dist/navigation-instruments/watch/tickmark.js +35 -13
- package/dist/navigation-instruments/watch/tickmark.js.map +1 -1
- package/dist/svghelpers/math.d.ts +7 -0
- package/dist/svghelpers/math.d.ts.map +1 -1
- package/dist/svghelpers/math.js +10 -1
- package/dist/svghelpers/math.js.map +1 -1
- package/llms/components/obc-attitude.md +69 -0
- package/llms/llms-full.txt +72 -0
- package/llms/llms.txt +1 -0
- package/package.json +1 -1
- package/src/navigation-instruments/attitude/attitude.css +54 -0
- package/src/navigation-instruments/attitude/attitude.spec.ts +208 -0
- package/src/navigation-instruments/attitude/attitude.stories.ts +87 -0
- package/src/navigation-instruments/attitude/attitude.ts +351 -0
- package/src/navigation-instruments/watch/tickmark.spec.ts +22 -0
- package/src/navigation-instruments/watch/tickmark.ts +49 -5
- package/src/svghelpers/math.spec.ts +25 -0
- package/src/svghelpers/math.ts +10 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LitElement,
|
|
3
|
+
PropertyValues,
|
|
4
|
+
html,
|
|
5
|
+
svg,
|
|
6
|
+
unsafeCSS,
|
|
7
|
+
type SVGTemplateResult,
|
|
8
|
+
} from 'lit';
|
|
9
|
+
import {property} from 'lit/decorators.js';
|
|
10
|
+
import {ResizeController} from '@lit-labs/observers/resize-controller.js';
|
|
11
|
+
import componentStyle from './attitude.css?inline';
|
|
12
|
+
import '../watch/watch.js';
|
|
13
|
+
import {
|
|
14
|
+
OUTER_RING_RADIUS,
|
|
15
|
+
WatchCircleType,
|
|
16
|
+
type WatchArea,
|
|
17
|
+
} from '../watch/watch.js';
|
|
18
|
+
import {Tickmark, TickmarkType} from '../watch/tickmark.js';
|
|
19
|
+
import {HdgArrowStyle, hdgArrow} from '../course-arrows/course-arrows.js';
|
|
20
|
+
import {customElement} from '../../decorator.js';
|
|
21
|
+
import {Priority} from '../types.js';
|
|
22
|
+
import {clamp, normalizeSignedAngle} from '../../svghelpers/math.js';
|
|
23
|
+
import {
|
|
24
|
+
applyPinnedHostSize,
|
|
25
|
+
computeRadialFrame,
|
|
26
|
+
estimateLabelWidthPx,
|
|
27
|
+
measureContainerPx,
|
|
28
|
+
observeInnerBox,
|
|
29
|
+
type RadialFrame,
|
|
30
|
+
} from '../../svghelpers/radial-frame.js';
|
|
31
|
+
|
|
32
|
+
/** Radius of the face, which is the watch's outer ring. */
|
|
33
|
+
const FACE_RADIUS = OUTER_RING_RADIUS;
|
|
34
|
+
/** Distance between two ladder rungs; one rung is `pitchTickInterval` degrees. */
|
|
35
|
+
const RUNG_SPACING = 64;
|
|
36
|
+
const RUNG_HALF_WIDTH = 12;
|
|
37
|
+
/**
|
|
38
|
+
* Range of `pitchTickInterval` that is drawn. The upper end keeps the ground
|
|
39
|
+
* band at least two rungs tall, so the face never holds more than a few bands.
|
|
40
|
+
* The lower end keeps ladder distances small enough to count rungs exactly.
|
|
41
|
+
*/
|
|
42
|
+
const MIN_PITCH_TICK_INTERVAL = 0.1;
|
|
43
|
+
const MAX_PITCH_TICK_INTERVAL = 90;
|
|
44
|
+
/** Stands in for a `pitchTickInterval` that is not a positive number. */
|
|
45
|
+
const DEFAULT_PITCH_TICK_INTERVAL = 10;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Marks of the roll scale, which turns with the horizon under the fixed
|
|
49
|
+
* pointer. The pointer sits on the side of the zero mark the vehicle rolls to.
|
|
50
|
+
*
|
|
51
|
+
* TODO(designer): Figma 20108:142406 labels this arc 270/315/0/45/90, the
|
|
52
|
+
* generic watch-face preset. Confirm the roll labels.
|
|
53
|
+
*/
|
|
54
|
+
const ROLL_SCALE_MARKS: readonly Tickmark[] = [
|
|
55
|
+
{angle: 270, type: TickmarkType.textOnly, text: '90'},
|
|
56
|
+
{angle: 315, type: TickmarkType.primary, text: '45'},
|
|
57
|
+
{angle: 0, type: TickmarkType.main, text: '0'},
|
|
58
|
+
{angle: 45, type: TickmarkType.primary, text: '45'},
|
|
59
|
+
{angle: 90, type: TickmarkType.textOnly, text: '90'},
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
/** The fixed symbol, a 240 × 28 box whose bar is centred on its row 4 (Figma 35754:89010). */
|
|
63
|
+
const VESSEL_SYMBOL_PATH =
|
|
64
|
+
'M100 0C102.209 0 104 1.791 104 4C104 8.243 105.686 12.313 108.687 15.313C111.687 18.314 115.757 20 120 20C124.243 20 128.313 18.314 131.313 15.313C134.314 12.313 136 8.243 136 4C136 1.791 137.791 0 140 0L236 0C238.209 0 240 1.791 240 4C240 6.209 238.209 8 236 8L143.663 8C142.838 12.881 140.517 17.424 136.971 20.971C132.47 25.472 126.365 28 120 28C113.635 28 107.53 25.472 103.029 20.971C99.483 17.424 97.162 12.881 96.337 8L4 8C1.791 8 0 6.209 0 4C0 1.791 1.791 0 4 0L100 0Z';
|
|
65
|
+
|
|
66
|
+
function isNumber(value: number | undefined | null): value is number {
|
|
67
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* `<obc-attitude>` – Attitude instrument that shows pitch and roll as a moving
|
|
72
|
+
* horizon behind a fixed vessel symbol, the way an artificial horizon does.
|
|
73
|
+
*
|
|
74
|
+
* The symbol in the centre stays level. The ground half of the face slides
|
|
75
|
+
* down as the pitch rises and tilts against the roll, so the picture matches
|
|
76
|
+
* what is seen looking forward. A ladder of short rungs on the horizon gives
|
|
77
|
+
* the pitch. The roll scale turns with the horizon, and the pointer at the
|
|
78
|
+
* top, which stays straight up like the symbol, reads the roll on it.
|
|
79
|
+
*
|
|
80
|
+
* ## Features / Variants
|
|
81
|
+
*
|
|
82
|
+
* - **Roll scale:** the band over the sky half of the face, labelled
|
|
83
|
+
* 90/45/0/45/90. It turns with the horizon, so the fixed pointer sits on
|
|
84
|
+
* the side the vehicle rolls to. `showLabels` turns the labels off.
|
|
85
|
+
* - **Full turns:** `pitch` and `roll` take any angle. Past ±90° of pitch the
|
|
86
|
+
* ladder runs on to the inverted horizon, with the ground above the sky,
|
|
87
|
+
* and the roll scale follows the sky to the lower half.
|
|
88
|
+
* - **Pitch ladder:** one rung per `pitchTickInterval` degrees. A smaller
|
|
89
|
+
* interval magnifies the pitch; a larger one shows more of it.
|
|
90
|
+
* - **Priority:** `priority` switches the symbol and the pointer between the
|
|
91
|
+
* `regular` and `enhanced` palette.
|
|
92
|
+
*
|
|
93
|
+
* ## Usage Guidelines
|
|
94
|
+
*
|
|
95
|
+
* Use for a vehicle that is steered in three dimensions and whose operator
|
|
96
|
+
* needs pitch and roll together at a glance. For readings against a scale
|
|
97
|
+
* with advice zones use `obc-pitch`, `obc-roll` or `obc-pitch-roll`; for
|
|
98
|
+
* pitch, roll and yaw deviation as a dot on a polar grid use
|
|
99
|
+
* `obc-pitch-roll-yaw`.
|
|
100
|
+
*
|
|
101
|
+
* The scales carry no numbers besides the roll labels. Put the values next to
|
|
102
|
+
* the instrument with `obc-readout` when they have to be read exactly.
|
|
103
|
+
*
|
|
104
|
+
* **TODO(designer)**: Figma 20108:142407 shows no pitch values on the ladder.
|
|
105
|
+
*
|
|
106
|
+
* @property pitch - Pitch in degrees, positive with the front up. The horizon drops below the
|
|
107
|
+
* symbol as it rises. Any angle is accepted and wraps over a full turn.
|
|
108
|
+
* @property roll - Roll in degrees, positive with the right side down. The horizon and the
|
|
109
|
+
* roll scale turn the other way, as they appear from on board. Any angle is accepted.
|
|
110
|
+
* @property pitchTickInterval - Degrees of pitch between two ladder rungs, from 0.1 to 90. Default
|
|
111
|
+
* `10`, which shows about ±29° on the face.
|
|
112
|
+
* @property showLabels - When `true` (default), labels the roll scale.
|
|
113
|
+
* @property priority - Palette of the symbol and the pointer: `regular` or `enhanced`.
|
|
114
|
+
* @property faceDiameter - Outer-ring diameter in CSS pixels. When set, the instrument renders at a
|
|
115
|
+
* fixed size, so instruments that share the value have rings of the same size.
|
|
116
|
+
* @experimental
|
|
117
|
+
*/
|
|
118
|
+
@customElement('obc-attitude')
|
|
119
|
+
export class ObcAttitude extends LitElement {
|
|
120
|
+
@property({type: Number}) pitch = 0;
|
|
121
|
+
@property({type: Number}) roll = 0;
|
|
122
|
+
@property({type: Number}) pitchTickInterval = 10;
|
|
123
|
+
@property({type: Boolean, attribute: false}) showLabels = true;
|
|
124
|
+
@property({type: String}) priority: Priority = Priority.regular;
|
|
125
|
+
@property({type: Number, attribute: 'face-diameter'})
|
|
126
|
+
faceDiameter: number | undefined;
|
|
127
|
+
|
|
128
|
+
/** Clip ids are looked up in the whole document, so each instance needs its own. */
|
|
129
|
+
private _idPrefix = `attitude-${Math.random().toString(36).slice(2, 9)}`;
|
|
130
|
+
private _frame: RadialFrame | undefined;
|
|
131
|
+
private _hostSizePinned = false;
|
|
132
|
+
private _resizeController = new ResizeController(this, {});
|
|
133
|
+
|
|
134
|
+
override firstUpdated(changed: PropertyValues): void {
|
|
135
|
+
super.firstUpdated(changed);
|
|
136
|
+
observeInnerBox(this._resizeController, this.renderRoot);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
override updated(changed: PropertyValues): void {
|
|
140
|
+
super.updated(changed);
|
|
141
|
+
this._hostSizePinned = applyPinnedHostSize(
|
|
142
|
+
this,
|
|
143
|
+
this._frame,
|
|
144
|
+
this._hostSizePinned
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private get safeRoll(): number {
|
|
149
|
+
return isNumber(this.roll) ? this.roll : 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Pitch within half a turn either way, so a full turn draws like none. */
|
|
153
|
+
private get wrappedPitch(): number {
|
|
154
|
+
return isNumber(this.pitch) ? normalizeSignedAngle(this.pitch) : 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Ladder distance for a pitch angle, in SVG units.
|
|
159
|
+
*
|
|
160
|
+
* TODO(designer): Figma 20108:141712 spaces the rungs 64 apart and gives
|
|
161
|
+
* them no value. The default reads one rung as 10°.
|
|
162
|
+
*/
|
|
163
|
+
private ladderUnits(degrees: number): number {
|
|
164
|
+
const interval =
|
|
165
|
+
isNumber(this.pitchTickInterval) && this.pitchTickInterval > 0
|
|
166
|
+
? clamp(
|
|
167
|
+
this.pitchTickInterval,
|
|
168
|
+
MIN_PITCH_TICK_INTERVAL,
|
|
169
|
+
MAX_PITCH_TICK_INTERVAL
|
|
170
|
+
)
|
|
171
|
+
: DEFAULT_PITCH_TICK_INTERVAL;
|
|
172
|
+
return (degrees * RUNG_SPACING) / interval;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Turn of the roll scale, in degrees clockwise. Its zero mark points at the
|
|
177
|
+
* sky, so it turns with the horizon and swaps sides once the pitch passes
|
|
178
|
+
* vertical, where the vehicle is upside down and the sky is below.
|
|
179
|
+
*/
|
|
180
|
+
private get rollScaleRotation(): number {
|
|
181
|
+
const upsideDown = Math.abs(this.wrappedPitch) > 90;
|
|
182
|
+
return -this.safeRoll + (upsideDown ? 180 : 0);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Moves the horizon from ladder coordinates onto the face: slid by the pitch, tilted against the roll. */
|
|
186
|
+
private get horizonTransform(): string {
|
|
187
|
+
return `rotate(${-this.safeRoll}) translate(0 ${this.ladderUnits(this.wrappedPitch)})`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The ground, the horizon lines and the ladder, in ladder coordinates:
|
|
192
|
+
* `y = 0` is the horizon and one rung is `RUNG_SPACING` up or down.
|
|
193
|
+
*
|
|
194
|
+
* The ladder repeats every full turn of pitch. Half a turn from each
|
|
195
|
+
* horizon lies the inverted one, with the ground above it. Only the part
|
|
196
|
+
* the face can show is drawn.
|
|
197
|
+
*/
|
|
198
|
+
private renderHorizon(): SVGTemplateResult {
|
|
199
|
+
const halfTurn = this.ladderUnits(180);
|
|
200
|
+
const turn = 2 * halfTurn;
|
|
201
|
+
const centre = -this.ladderUnits(this.wrappedPitch);
|
|
202
|
+
const top = centre - FACE_RADIUS;
|
|
203
|
+
const bottom = centre + FACE_RADIUS;
|
|
204
|
+
|
|
205
|
+
const ground: SVGTemplateResult[] = [];
|
|
206
|
+
for (let n = Math.ceil(top / turn - 0.5); n * turn <= bottom; n++) {
|
|
207
|
+
const y = Math.max(n * turn, top);
|
|
208
|
+
const height = Math.min(n * turn + halfTurn, bottom) - y;
|
|
209
|
+
if (height > 0) {
|
|
210
|
+
ground.push(
|
|
211
|
+
svg`<rect class="ground" x=${-FACE_RADIUS} y=${y} width=${FACE_RADIUS * 2} height=${height} />`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const horizons: SVGTemplateResult[] = [];
|
|
217
|
+
for (let m = Math.ceil(top / halfTurn); m * halfTurn <= bottom; m++) {
|
|
218
|
+
const y = m * halfTurn;
|
|
219
|
+
horizons.push(
|
|
220
|
+
svg`<line class="horizon-line" x1=${-FACE_RADIUS} x2=${FACE_RADIUS} y1=${y} y2=${y} />`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Rungs count out from each upright horizon and stop short of the inverted
|
|
225
|
+
// one, so the ladder repeats with the ground whatever the interval.
|
|
226
|
+
const rungs: SVGTemplateResult[] = [];
|
|
227
|
+
for (
|
|
228
|
+
let n = Math.floor((top + halfTurn) / turn);
|
|
229
|
+
n * turn - halfTurn <= bottom;
|
|
230
|
+
n++
|
|
231
|
+
) {
|
|
232
|
+
const origin = n * turn;
|
|
233
|
+
const first = Math.max(top, origin - halfTurn) - origin;
|
|
234
|
+
const last = Math.min(bottom, origin + halfTurn) - origin;
|
|
235
|
+
for (
|
|
236
|
+
let j = Math.ceil(first / RUNG_SPACING);
|
|
237
|
+
j * RUNG_SPACING <= last;
|
|
238
|
+
j++
|
|
239
|
+
) {
|
|
240
|
+
const offset = j * RUNG_SPACING;
|
|
241
|
+
if (j === 0 || halfTurn - Math.abs(offset) < 0.5) {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
const y = origin + offset;
|
|
245
|
+
rungs.push(
|
|
246
|
+
svg`<line class="rung" x1=${-RUNG_HALF_WIDTH} x2=${RUNG_HALF_WIDTH} y1=${y} y2=${y} vector-effect="non-scaling-stroke" />`
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return svg`
|
|
252
|
+
<g class="horizon" transform=${this.horizonTransform}>
|
|
253
|
+
${ground} ${rungs} ${horizons}
|
|
254
|
+
</g>
|
|
255
|
+
`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The part of the face edge the roll scale band does not already draw. It
|
|
260
|
+
* turns with the band so the two keep meeting at the band's ends.
|
|
261
|
+
*/
|
|
262
|
+
private renderFaceOutline(): SVGTemplateResult {
|
|
263
|
+
const r = FACE_RADIUS;
|
|
264
|
+
return svg`<path class="face-outline" transform="rotate(${this.rollScaleRotation})" d="M ${-r} 0 A ${r} ${r} 0 0 0 ${r} 0" vector-effect="non-scaling-stroke" />`;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private renderVesselSymbol(): SVGTemplateResult {
|
|
268
|
+
return svg`
|
|
269
|
+
<g class="vessel-symbol">
|
|
270
|
+
<path transform="translate(-120 -4)" d=${VESSEL_SYMBOL_PATH} />
|
|
271
|
+
<circle r="4" />
|
|
272
|
+
</g>
|
|
273
|
+
`;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The roll pointer. It is fixed to the vehicle like the symbol, so it stays
|
|
278
|
+
* straight up and the roll scale turns under it.
|
|
279
|
+
*
|
|
280
|
+
* TODO(designer): Figma 20108:142406 wraps this arrow in a rotator tool and
|
|
281
|
+
* keeps the scale still. The review chose the fixed pointer.
|
|
282
|
+
*/
|
|
283
|
+
private renderRollPointer(): SVGTemplateResult {
|
|
284
|
+
return svg`
|
|
285
|
+
<g class="roll-pointer">
|
|
286
|
+
${hdgArrow(HdgArrowStyle.arrowHead, 0, this.priority)}
|
|
287
|
+
</g>
|
|
288
|
+
`;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
override render() {
|
|
292
|
+
const frame = computeRadialFrame({
|
|
293
|
+
basePadding: 24,
|
|
294
|
+
labelWidthPx: this.showLabels
|
|
295
|
+
? estimateLabelWidthPx(ROLL_SCALE_MARKS.map((mark) => mark.text))
|
|
296
|
+
: 0,
|
|
297
|
+
containerPx: measureContainerPx(this),
|
|
298
|
+
faceDiameter: this.faceDiameter,
|
|
299
|
+
});
|
|
300
|
+
this._frame = frame;
|
|
301
|
+
|
|
302
|
+
const areas: WatchArea[] = [
|
|
303
|
+
{
|
|
304
|
+
startAngle: -90,
|
|
305
|
+
endAngle: 90,
|
|
306
|
+
roundOutsideCut: true,
|
|
307
|
+
roundInsideCut: true,
|
|
308
|
+
},
|
|
309
|
+
];
|
|
310
|
+
const showLabels = this.showLabels && !frame.labelsHidden;
|
|
311
|
+
const tickmarks: Tickmark[] = ROLL_SCALE_MARKS.filter(
|
|
312
|
+
(mark) => showLabels || mark.type !== TickmarkType.textOnly
|
|
313
|
+
).map((mark) => ({...mark, text: showLabels ? mark.text : undefined}));
|
|
314
|
+
|
|
315
|
+
const faceClip = `${this._idPrefix}-face`;
|
|
316
|
+
const enhanced = this.priority === Priority.enhanced;
|
|
317
|
+
|
|
318
|
+
return html`
|
|
319
|
+
<div class="container ${enhanced ? 'enhanced' : ''}">
|
|
320
|
+
<svg viewBox=${frame.viewBox}>
|
|
321
|
+
<defs>
|
|
322
|
+
<clipPath id=${faceClip}>
|
|
323
|
+
<circle r=${FACE_RADIUS} />
|
|
324
|
+
</clipPath>
|
|
325
|
+
</defs>
|
|
326
|
+
<g clip-path="url(#${faceClip})">${this.renderHorizon()}</g>
|
|
327
|
+
${this.renderFaceOutline()}
|
|
328
|
+
</svg>
|
|
329
|
+
<obc-watch
|
|
330
|
+
.arcFrame=${frame}
|
|
331
|
+
.rotation=${this.rollScaleRotation}
|
|
332
|
+
.watchCircleType=${WatchCircleType.single}
|
|
333
|
+
.areas=${areas}
|
|
334
|
+
.tickmarks=${tickmarks}
|
|
335
|
+
.priority=${this.priority}
|
|
336
|
+
></obc-watch>
|
|
337
|
+
<svg viewBox=${frame.viewBox}>
|
|
338
|
+
${this.renderVesselSymbol()} ${this.renderRollPointer()}
|
|
339
|
+
</svg>
|
|
340
|
+
</div>
|
|
341
|
+
`;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
static override styles = unsafeCSS(componentStyle);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
declare global {
|
|
348
|
+
interface HTMLElementTagNameMap {
|
|
349
|
+
'obc-attitude': ObcAttitude;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
@@ -79,6 +79,28 @@ describe('tickmark label positioning', () => {
|
|
|
79
79
|
}
|
|
80
80
|
});
|
|
81
81
|
|
|
82
|
+
it('keeps a text-only label upright on a turning card', () => {
|
|
83
|
+
const labelOf = (rotation: number | undefined) =>
|
|
84
|
+
[
|
|
85
|
+
tickmark(90, {
|
|
86
|
+
size: TickmarkType.textOnly,
|
|
87
|
+
style: TickmarkStyle.regular,
|
|
88
|
+
scale: 1,
|
|
89
|
+
text: '90',
|
|
90
|
+
inside: false,
|
|
91
|
+
textRadius: 184,
|
|
92
|
+
rotation,
|
|
93
|
+
maxDigits: 2,
|
|
94
|
+
}),
|
|
95
|
+
].flat()[0];
|
|
96
|
+
|
|
97
|
+
const turning = labelOf(30);
|
|
98
|
+
expect(turning.strings.join('')).toContain('transform="rotate(');
|
|
99
|
+
expect(turning.values).toContain(-30);
|
|
100
|
+
// A fixed card keeps the side-anchored label it always had.
|
|
101
|
+
expect(labelOf(undefined).strings.join('')).not.toContain('rotate(');
|
|
102
|
+
});
|
|
103
|
+
|
|
82
104
|
it('does not distort geometry for a valid scale', () => {
|
|
83
105
|
// A settled scale must be untouched: the outside zero tick at -90° sits on
|
|
84
106
|
// the negative-x axis, so its x is finite and negative and y is ~0.
|
|
@@ -130,6 +130,18 @@ export function tickmark(
|
|
|
130
130
|
innerRadius = 328 / 2 + rOff;
|
|
131
131
|
outerRadius = 336 / 2 + rOff;
|
|
132
132
|
} else {
|
|
133
|
+
if (text && rotation !== undefined) {
|
|
134
|
+
return [
|
|
135
|
+
turningLabelSvg(text, {
|
|
136
|
+
rad,
|
|
137
|
+
inside,
|
|
138
|
+
scale: safeScale,
|
|
139
|
+
textRadius,
|
|
140
|
+
rotation,
|
|
141
|
+
maxDigits,
|
|
142
|
+
}),
|
|
143
|
+
];
|
|
144
|
+
}
|
|
133
145
|
return [
|
|
134
146
|
textSvg(text ?? '', {
|
|
135
147
|
angle,
|
|
@@ -177,19 +189,51 @@ export function tickmark(
|
|
|
177
189
|
}),
|
|
178
190
|
];
|
|
179
191
|
} else {
|
|
180
|
-
const newRadius =
|
|
181
|
-
textRadius + ((4 / safeScale + 5) * (inside ? -1 : 1) * maxDigits) / 2;
|
|
182
|
-
const textX = Math.sin(rad) * newRadius;
|
|
183
|
-
const textY = -Math.cos(rad) * newRadius;
|
|
184
192
|
return [
|
|
185
193
|
tick,
|
|
186
|
-
|
|
194
|
+
turningLabelSvg(text, {
|
|
195
|
+
rad,
|
|
196
|
+
inside,
|
|
197
|
+
scale: safeScale,
|
|
198
|
+
textRadius,
|
|
199
|
+
rotation,
|
|
200
|
+
maxDigits,
|
|
201
|
+
}),
|
|
187
202
|
];
|
|
188
203
|
}
|
|
189
204
|
}
|
|
190
205
|
return tick;
|
|
191
206
|
}
|
|
192
207
|
|
|
208
|
+
/**
|
|
209
|
+
* A label on a card that turns: centred on its mark, far enough out to clear
|
|
210
|
+
* the ring at any angle, and turned back so the text stays upright.
|
|
211
|
+
*/
|
|
212
|
+
function turningLabelSvg(
|
|
213
|
+
text: string,
|
|
214
|
+
{
|
|
215
|
+
rad,
|
|
216
|
+
inside,
|
|
217
|
+
scale,
|
|
218
|
+
textRadius,
|
|
219
|
+
rotation,
|
|
220
|
+
maxDigits,
|
|
221
|
+
}: {
|
|
222
|
+
rad: number;
|
|
223
|
+
inside: boolean;
|
|
224
|
+
scale: number;
|
|
225
|
+
textRadius: number;
|
|
226
|
+
rotation: number;
|
|
227
|
+
maxDigits: number;
|
|
228
|
+
}
|
|
229
|
+
) {
|
|
230
|
+
const newRadius =
|
|
231
|
+
textRadius + ((4 / scale + 5) * (inside ? -1 : 1) * maxDigits) / 2;
|
|
232
|
+
const textX = Math.sin(rad) * newRadius;
|
|
233
|
+
const textY = -Math.cos(rad) * newRadius;
|
|
234
|
+
return svg`<text x=${textX} y=${textY} class="label rotate ${inside ? 'inside' : ''}" transform="rotate(${-rotation})" transform-origin="${textX} ${textY}">${text}</text>`;
|
|
235
|
+
}
|
|
236
|
+
|
|
193
237
|
function textSvg(
|
|
194
238
|
text: string,
|
|
195
239
|
{
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
clampPercent,
|
|
5
5
|
degToRad,
|
|
6
6
|
normalizeAngle,
|
|
7
|
+
normalizeSignedAngle,
|
|
7
8
|
radToDeg,
|
|
8
9
|
} from './math.js';
|
|
9
10
|
|
|
@@ -63,6 +64,30 @@ describe('normalizeAngle', () => {
|
|
|
63
64
|
});
|
|
64
65
|
});
|
|
65
66
|
|
|
67
|
+
describe('normalizeSignedAngle', () => {
|
|
68
|
+
it('wraps into [-180, 180)', () => {
|
|
69
|
+
expect(normalizeSignedAngle(0)).toBe(0);
|
|
70
|
+
expect(normalizeSignedAngle(10)).toBe(10);
|
|
71
|
+
expect(normalizeSignedAngle(-10)).toBe(-10);
|
|
72
|
+
expect(normalizeSignedAngle(190)).toBe(-170);
|
|
73
|
+
expect(normalizeSignedAngle(-190)).toBe(170);
|
|
74
|
+
expect(normalizeSignedAngle(370)).toBe(10);
|
|
75
|
+
expect(normalizeSignedAngle(-730)).toBe(-10);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('puts the half turn on the negative end', () => {
|
|
79
|
+
expect(normalizeSignedAngle(180)).toBe(-180);
|
|
80
|
+
expect(normalizeSignedAngle(-180)).toBe(-180);
|
|
81
|
+
expect(normalizeSignedAngle(540)).toBe(-180);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('never returns negative zero and passes NaN through', () => {
|
|
85
|
+
expect(Object.is(normalizeSignedAngle(-0), 0)).toBe(true);
|
|
86
|
+
expect(Object.is(normalizeSignedAngle(360), 0)).toBe(true);
|
|
87
|
+
expect(normalizeSignedAngle(NaN)).toBeNaN();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
66
91
|
describe('degToRad / radToDeg', () => {
|
|
67
92
|
it('converts the cardinal angles', () => {
|
|
68
93
|
expect(degToRad(180)).toBe(Math.PI);
|
package/src/svghelpers/math.ts
CHANGED
|
@@ -40,6 +40,16 @@ export function normalizeAngle(deg: number): number {
|
|
|
40
40
|
return ((deg % 360) + 360) % 360;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Degrees wrapped into `[-180, 180)`, for a deviation either side of zero.
|
|
45
|
+
*
|
|
46
|
+
* @param deg - Any finite angle, negative and over-turned included; a non-finite angle yields NaN.
|
|
47
|
+
* @returns The same direction in `[-180, 180)`; a half turn is `-180`.
|
|
48
|
+
*/
|
|
49
|
+
export function normalizeSignedAngle(deg: number): number {
|
|
50
|
+
return normalizeAngle(deg + 180) - 180;
|
|
51
|
+
}
|
|
52
|
+
|
|
43
53
|
/**
|
|
44
54
|
* @param deg - Angle in degrees.
|
|
45
55
|
* @returns The angle in radians, as `(deg * Math.PI) / 180`.
|