@formicoidea/labre-framework-wardley 0.23.0 → 0.23.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.
@@ -1,143 +1,143 @@
1
- import { EdgelessCRUDIdentifier } from '@formicoidea/labre-core/blocks/surface';
2
- import type { WardleyBackgroundElementModel } from '@formicoidea/labre-core/model';
3
- import { rotatePoint } from '@formicoidea/labre-core/global/gfx';
4
- import type { PointerEventState } from '@formicoidea/labre-core/std';
5
- import {
6
- GfxElementModelView,
7
- GfxViewInteractionExtension,
8
- } from '@formicoidea/labre-core/std/gfx';
9
-
10
- import {
11
- getWardleyLabelHits,
12
- hitTestWardleyLabel,
13
- type WardleyLabelField,
14
- } from './label-layout';
15
-
16
- export class WardleyView extends GfxElementModelView<WardleyBackgroundElementModel> {
17
- static override type: string = 'wardley';
18
-
19
- /** The in-place `<input>` used to edit a label, or null when idle. */
20
- private _labelEditor: HTMLInputElement | null = null;
21
-
22
- override onCreated(): void {
23
- super.onCreated();
24
- this.on('dblclick', e => this._onDblClick(e));
25
- }
26
-
27
- override onDestroyed(): void {
28
- this._closeLabelEditor();
29
- super.onDestroyed();
30
- }
31
-
32
- /** Double-click on a label → edit its text in place. */
33
- private _onDblClick(e: PointerEventState): void {
34
- if (this.model.isLocked()) return;
35
-
36
- const [mx, my] = this.gfx.viewport.toModelCoord(e.x, e.y);
37
- const [bx, by, w, h] = this.model.deserializedXYWH;
38
-
39
- // Convert the model-space point into element-local coordinates, undoing the
40
- // element rotation around its center.
41
- let lx = mx - bx;
42
- let ly = my - by;
43
- const rot = this.model.rotate ?? 0;
44
- if (rot) {
45
- const center: [number, number] = [bx + w / 2, by + h / 2];
46
- const [ux, uy] = rotatePoint([mx, my], center, -rot);
47
- lx = ux - bx;
48
- ly = uy - by;
49
- }
50
-
51
- const hit = hitTestWardleyLabel(getWardleyLabelHits(this.model, w, h), lx, ly);
52
- if (!hit) return;
53
-
54
- this._openLabelEditor(hit.field, e);
55
- }
56
-
57
- private _openLabelEditor(field: WardleyLabelField, e: PointerEventState): void {
58
- this._closeLabelEditor();
59
-
60
- const input = document.createElement('input');
61
- input.value = String(this.model[field] ?? '');
62
- Object.assign(input.style, {
63
- position: 'fixed',
64
- left: `${e.raw.clientX}px`,
65
- top: `${e.raw.clientY}px`,
66
- transform: 'translate(-50%, -50%)',
67
- zIndex: '10000',
68
- minWidth: '140px',
69
- padding: '3px 8px',
70
- font: '14px Inter, sans-serif',
71
- color: 'var(--affine-text-primary-color, #1f2328)',
72
- background: 'var(--affine-background-overlay-panel-color, #ffffff)',
73
- border: '1px solid var(--affine-primary-color, #1e96eb)',
74
- borderRadius: '6px',
75
- boxShadow: 'var(--affine-shadow-2, 0 2px 8px rgba(0,0,0,0.18))',
76
- outline: 'none',
77
- });
78
- document.body.append(input);
79
- this._labelEditor = input;
80
-
81
- // Mark the element as "editing" so the global edgeless key handlers
82
- // (delete, escape, etc.) don't act on it while the user types.
83
- this.gfx.selection.set({ elements: [this.model.id], editing: true });
84
-
85
- input.focus();
86
- input.select();
87
-
88
- const commit = () => {
89
- // Guard against re-entrancy: removing the input fires `blur`, which would
90
- // otherwise call `commit` a second time.
91
- if (this._labelEditor !== input) return;
92
- const value = input.value;
93
- this._closeLabelEditor();
94
- this.gfx.std.store.captureSync();
95
- this.gfx.std
96
- .get(EdgelessCRUDIdentifier)
97
- .updateElement(this.model.id, { [field]: value });
98
- };
99
-
100
- input.addEventListener('keydown', ev => {
101
- ev.stopPropagation();
102
- if (ev.key === 'Enter') {
103
- ev.preventDefault();
104
- commit();
105
- } else if (ev.key === 'Escape') {
106
- ev.preventDefault();
107
- this._closeLabelEditor();
108
- }
109
- });
110
- input.addEventListener('blur', commit);
111
- }
112
-
113
- private _closeLabelEditor(): void {
114
- if (!this._labelEditor) return;
115
- const input = this._labelEditor;
116
- this._labelEditor = null;
117
- input.remove();
118
- if (this.isConnected) {
119
- this.gfx.selection.set({ elements: [this.model.id], editing: false });
120
- }
121
- }
122
- }
123
-
124
- /**
125
- * Resize gating: the resize handles are hidden unless `model.resizeEnabled` is
126
- * true. `beforeResize` is re-evaluated every time the allowed handles are
127
- * computed (manager.ts), so toggling the field from the toolbar updates the
128
- * handles reactively. Moving/selecting stays available throughout.
129
- */
130
- export const WardleyInteraction = GfxViewInteractionExtension<WardleyView>(
131
- WardleyView.type,
132
- {
133
- handleResize({ model }) {
134
- return {
135
- beforeResize({ set }) {
136
- if (!model.resizeEnabled) {
137
- set({ allowedHandlers: [] });
138
- }
139
- },
140
- };
141
- },
142
- }
143
- );
1
+ import { EdgelessCRUDIdentifier } from '@formicoidea/labre-core/blocks/surface';
2
+ import type { WardleyBackgroundElementModel } from '@formicoidea/labre-core/model';
3
+ import { rotatePoint } from '@formicoidea/labre-core/global/gfx';
4
+ import type { PointerEventState } from '@formicoidea/labre-core/std';
5
+ import {
6
+ GfxElementModelView,
7
+ GfxViewInteractionExtension,
8
+ } from '@formicoidea/labre-core/std/gfx';
9
+
10
+ import {
11
+ getWardleyLabelHits,
12
+ hitTestWardleyLabel,
13
+ type WardleyLabelField,
14
+ } from './label-layout';
15
+
16
+ export class WardleyView extends GfxElementModelView<WardleyBackgroundElementModel> {
17
+ static override type: string = 'wardley';
18
+
19
+ /** The in-place `<input>` used to edit a label, or null when idle. */
20
+ private _labelEditor: HTMLInputElement | null = null;
21
+
22
+ override onCreated(): void {
23
+ super.onCreated();
24
+ this.on('dblclick', e => this._onDblClick(e));
25
+ }
26
+
27
+ override onDestroyed(): void {
28
+ this._closeLabelEditor();
29
+ super.onDestroyed();
30
+ }
31
+
32
+ /** Double-click on a label → edit its text in place. */
33
+ private _onDblClick(e: PointerEventState): void {
34
+ if (this.model.isLocked()) return;
35
+
36
+ const [mx, my] = this.gfx.viewport.toModelCoord(e.x, e.y);
37
+ const [bx, by, w, h] = this.model.deserializedXYWH;
38
+
39
+ // Convert the model-space point into element-local coordinates, undoing the
40
+ // element rotation around its center.
41
+ let lx = mx - bx;
42
+ let ly = my - by;
43
+ const rot = this.model.rotate ?? 0;
44
+ if (rot) {
45
+ const center: [number, number] = [bx + w / 2, by + h / 2];
46
+ const [ux, uy] = rotatePoint([mx, my], center, -rot);
47
+ lx = ux - bx;
48
+ ly = uy - by;
49
+ }
50
+
51
+ const hit = hitTestWardleyLabel(getWardleyLabelHits(this.model, w, h), lx, ly);
52
+ if (!hit) return;
53
+
54
+ this._openLabelEditor(hit.field, e);
55
+ }
56
+
57
+ private _openLabelEditor(field: WardleyLabelField, e: PointerEventState): void {
58
+ this._closeLabelEditor();
59
+
60
+ const input = document.createElement('input');
61
+ input.value = String(this.model[field] ?? '');
62
+ Object.assign(input.style, {
63
+ position: 'fixed',
64
+ left: `${e.raw.clientX}px`,
65
+ top: `${e.raw.clientY}px`,
66
+ transform: 'translate(-50%, -50%)',
67
+ zIndex: '10000',
68
+ minWidth: '140px',
69
+ padding: '3px 8px',
70
+ font: '14px Inter, sans-serif',
71
+ color: 'var(--affine-text-primary-color, #1f2328)',
72
+ background: 'var(--affine-background-overlay-panel-color, #ffffff)',
73
+ border: '1px solid var(--affine-primary-color, #1e96eb)',
74
+ borderRadius: '6px',
75
+ boxShadow: 'var(--affine-shadow-2, 0 2px 8px rgba(0,0,0,0.18))',
76
+ outline: 'none',
77
+ });
78
+ document.body.append(input);
79
+ this._labelEditor = input;
80
+
81
+ // Mark the element as "editing" so the global edgeless key handlers
82
+ // (delete, escape, etc.) don't act on it while the user types.
83
+ this.gfx.selection.set({ elements: [this.model.id], editing: true });
84
+
85
+ input.focus();
86
+ input.select();
87
+
88
+ const commit = () => {
89
+ // Guard against re-entrancy: removing the input fires `blur`, which would
90
+ // otherwise call `commit` a second time.
91
+ if (this._labelEditor !== input) return;
92
+ const value = input.value;
93
+ this._closeLabelEditor();
94
+ this.gfx.std.store.captureSync();
95
+ this.gfx.std
96
+ .get(EdgelessCRUDIdentifier)
97
+ .updateElement(this.model.id, { [field]: value });
98
+ };
99
+
100
+ input.addEventListener('keydown', ev => {
101
+ ev.stopPropagation();
102
+ if (ev.key === 'Enter') {
103
+ ev.preventDefault();
104
+ commit();
105
+ } else if (ev.key === 'Escape') {
106
+ ev.preventDefault();
107
+ this._closeLabelEditor();
108
+ }
109
+ });
110
+ input.addEventListener('blur', commit);
111
+ }
112
+
113
+ private _closeLabelEditor(): void {
114
+ if (!this._labelEditor) return;
115
+ const input = this._labelEditor;
116
+ this._labelEditor = null;
117
+ input.remove();
118
+ if (this.isConnected) {
119
+ this.gfx.selection.set({ elements: [this.model.id], editing: false });
120
+ }
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Resize gating: the resize handles are hidden unless `model.resizeEnabled` is
126
+ * true. `beforeResize` is re-evaluated every time the allowed handles are
127
+ * computed (manager.ts), so toggling the field from the toolbar updates the
128
+ * handles reactively. Moving/selecting stays available throughout.
129
+ */
130
+ export const WardleyInteraction = GfxViewInteractionExtension<WardleyView>(
131
+ WardleyView.type,
132
+ {
133
+ handleResize({ model }) {
134
+ return {
135
+ beforeResize({ set }) {
136
+ if (!model.resizeEnabled) {
137
+ set({ allowedHandlers: [] });
138
+ }
139
+ },
140
+ };
141
+ },
142
+ }
143
+ );
package/src/gradient.ts CHANGED
@@ -1,137 +1,137 @@
1
- /**
2
- * Curve-driven gradient backgrounds (Slice C). Each analytic background is a
3
- * smooth mathematical curve (piecewise asymmetric Gaussian bells); the gradient
4
- * opacity at each evolution position X follows that curve, normalised between
5
- * its own min and max — i.e. the gradient is strongest where the curve peaks and
6
- * fades to nothing at its minimum. Validated against the reference images at
7
- * `../wardley-mockups/gradient-backgrounds.html`.
8
- */
9
-
10
- const bell = (x: number, mu: number, s: number) =>
11
- Math.exp(-0.5 * ((x - mu) / s) ** 2);
12
- const asym = (x: number, mu: number, sL: number, sR: number) =>
13
- Math.exp(-0.5 * ((x - mu) / (x < mu ? sL : sR)) ** 2);
14
-
15
- // Opportunity — differential value (green): early peak + long decay.
16
- const fDiff = (x: number) => asym(x, 0.175, 0.24, 0.36);
17
- const DIFF_DOM: readonly [number, number] = [0, 0.86];
18
- // Opportunity — operational value (red): bump centred on commodity.
19
- const fOper = (x: number) => asym(x, 0.85, 0.1, 0.075);
20
- const OPER_DOM: readonly [number, number] = [0.62, 1];
21
- // Benefit / investment (signed): big positive bell − small negative bell.
22
- const fBen = (x: number) => asym(x, 0.49, 0.17, 0.24) - 0.42 * bell(x, 0.1, 0.075);
23
-
24
- // Evolution-gradient — Simon Wardley's classic evolution presentation: a
25
- // symmetric grey "U", strong at both evolution extremes (uncharted /
26
- // industrialised), fading to white through the build middle.
27
- const fGrey = (x: number) => {
28
- const left = Math.max(0, (0.26 - x) / 0.26);
29
- const right = Math.max(0, (x - 0.64) / 0.36);
30
- return Math.max(left, right);
31
- };
32
-
33
- function rangeOf(fn: (x: number) => number, x0: number, x1: number) {
34
- let lo = Infinity;
35
- let hi = -Infinity;
36
- for (let i = 0; i <= 240; i++) {
37
- const v = fn(x0 + ((x1 - x0) * i) / 240);
38
- if (v < lo) lo = v;
39
- if (v > hi) hi = v;
40
- }
41
- return { lo, hi };
42
- }
43
- const RG = rangeOf(fDiff, DIFF_DOM[0], DIFF_DOM[1]);
44
- const RR = rangeOf(fOper, OPER_DOM[0], OPER_DOM[1]);
45
- const RB = rangeOf(fBen, 0, 1);
46
-
47
- const clamp01 = (v: number) => Math.max(0, Math.min(1, v));
48
- const norm = (v: number, lo: number, hi: number) => (hi > lo ? (v - lo) / (hi - lo) : 0);
49
-
50
- export const GRADIENT_GREEN = '#1f9e4d';
51
- export const GRADIENT_RED = '#d6455d';
52
- const GRADIENT_GREY = '#7c8389';
53
- /** Validated peak opacity for the green/red variants. */
54
- const GRADIENT_MAX_OPACITY = 0.45;
55
- /** Peak opacity for the grey evolution-gradient variant. */
56
- const GREY_MAX_OPACITY = 0.38;
57
- /** Benefit/investment zero-line height (fraction of plot height from bottom). */
58
- export const BENEFIT_ZERO_FRAC = 0.3;
59
-
60
- function rgba(hex: string, alpha: number) {
61
- const r = parseInt(hex.slice(1, 3), 16);
62
- const g = parseInt(hex.slice(3, 5), 16);
63
- const b = parseInt(hex.slice(5, 7), 16);
64
- return `rgba(${r},${g},${b},${alpha})`;
65
- }
66
-
67
- /**
68
- * Add stops to a horizontal gradient (offset 0..1 spanning the plot width) for
69
- * a function-driven opacity profile within [x0, x1] (zero outside).
70
- */
71
- function addStops(
72
- grad: CanvasGradient,
73
- hex: string,
74
- opacityFn: (x: number) => number,
75
- x0: number,
76
- x1: number,
77
- maxOp: number = GRADIENT_MAX_OPACITY
78
- ) {
79
- const eps = 0.001;
80
- if (x0 > eps) grad.addColorStop(Math.max(0, x0 - eps), rgba(hex, 0));
81
- const N = 48;
82
- for (let i = 0; i <= N; i++) {
83
- const x = x0 + ((x1 - x0) * i) / N;
84
- grad.addColorStop(clamp01(x), rgba(hex, clamp01(opacityFn(x)) * maxOp));
85
- }
86
- if (x1 < 1 - eps) grad.addColorStop(Math.min(1, x1 + eps), rgba(hex, 0));
87
- }
88
-
89
- /**
90
- * Paint the curve-driven gradient over the plot rectangle [px0,px1]×[py0,py1]
91
- * in element-local coordinates. `classic` paints nothing.
92
- */
93
- export function paintGradientBackground(
94
- ctx: CanvasRenderingContext2D,
95
- variant: 'opportunity' | 'benefit' | 'evolution-gradient',
96
- px0: number,
97
- px1: number,
98
- py0: number,
99
- py1: number
100
- ) {
101
- const w = px1 - px0;
102
- const h = py1 - py0;
103
-
104
- if (variant === 'evolution-gradient') {
105
- const grey = ctx.createLinearGradient(px0, 0, px1, 0);
106
- addStops(grey, GRADIENT_GREY, fGrey, 0, 1, GREY_MAX_OPACITY);
107
- ctx.fillStyle = grey;
108
- ctx.fillRect(px0, py0, w, h);
109
- return;
110
- }
111
-
112
- if (variant === 'opportunity') {
113
- const green = ctx.createLinearGradient(px0, 0, px1, 0);
114
- addStops(green, GRADIENT_GREEN, x => norm(fDiff(x), RG.lo, RG.hi), DIFF_DOM[0], DIFF_DOM[1]);
115
- ctx.fillStyle = green;
116
- ctx.fillRect(px0, py0, w, h);
117
-
118
- const red = ctx.createLinearGradient(px0, 0, px1, 0);
119
- addStops(red, GRADIENT_RED, x => norm(fOper(x), RR.lo, RR.hi), OPER_DOM[0], OPER_DOM[1]);
120
- ctx.fillStyle = red;
121
- ctx.fillRect(px0, py0, w, h);
122
- return;
123
- }
124
-
125
- // benefit: green where the curve is positive, red where negative.
126
- const maxPos = RB.hi;
127
- const maxNeg = -RB.lo;
128
- const green = ctx.createLinearGradient(px0, 0, px1, 0);
129
- addStops(green, GRADIENT_GREEN, x => Math.max(0, fBen(x)) / maxPos, 0, 1);
130
- ctx.fillStyle = green;
131
- ctx.fillRect(px0, py0, w, h);
132
-
133
- const red = ctx.createLinearGradient(px0, 0, px1, 0);
134
- addStops(red, GRADIENT_RED, x => Math.max(0, -fBen(x)) / maxNeg, 0, 1);
135
- ctx.fillStyle = red;
136
- ctx.fillRect(px0, py0, w, h);
137
- }
1
+ /**
2
+ * Curve-driven gradient backgrounds (Slice C). Each analytic background is a
3
+ * smooth mathematical curve (piecewise asymmetric Gaussian bells); the gradient
4
+ * opacity at each evolution position X follows that curve, normalised between
5
+ * its own min and max — i.e. the gradient is strongest where the curve peaks and
6
+ * fades to nothing at its minimum. Validated against the reference images at
7
+ * `../wardley-mockups/gradient-backgrounds.html`.
8
+ */
9
+
10
+ const bell = (x: number, mu: number, s: number) =>
11
+ Math.exp(-0.5 * ((x - mu) / s) ** 2);
12
+ const asym = (x: number, mu: number, sL: number, sR: number) =>
13
+ Math.exp(-0.5 * ((x - mu) / (x < mu ? sL : sR)) ** 2);
14
+
15
+ // Opportunity — differential value (green): early peak + long decay.
16
+ const fDiff = (x: number) => asym(x, 0.175, 0.24, 0.36);
17
+ const DIFF_DOM: readonly [number, number] = [0, 0.86];
18
+ // Opportunity — operational value (red): bump centred on commodity.
19
+ const fOper = (x: number) => asym(x, 0.85, 0.1, 0.075);
20
+ const OPER_DOM: readonly [number, number] = [0.62, 1];
21
+ // Benefit / investment (signed): big positive bell − small negative bell.
22
+ const fBen = (x: number) => asym(x, 0.49, 0.17, 0.24) - 0.42 * bell(x, 0.1, 0.075);
23
+
24
+ // Evolution-gradient — Simon Wardley's classic evolution presentation: a
25
+ // symmetric grey "U", strong at both evolution extremes (uncharted /
26
+ // industrialised), fading to white through the build middle.
27
+ const fGrey = (x: number) => {
28
+ const left = Math.max(0, (0.26 - x) / 0.26);
29
+ const right = Math.max(0, (x - 0.64) / 0.36);
30
+ return Math.max(left, right);
31
+ };
32
+
33
+ function rangeOf(fn: (x: number) => number, x0: number, x1: number) {
34
+ let lo = Infinity;
35
+ let hi = -Infinity;
36
+ for (let i = 0; i <= 240; i++) {
37
+ const v = fn(x0 + ((x1 - x0) * i) / 240);
38
+ if (v < lo) lo = v;
39
+ if (v > hi) hi = v;
40
+ }
41
+ return { lo, hi };
42
+ }
43
+ const RG = rangeOf(fDiff, DIFF_DOM[0], DIFF_DOM[1]);
44
+ const RR = rangeOf(fOper, OPER_DOM[0], OPER_DOM[1]);
45
+ const RB = rangeOf(fBen, 0, 1);
46
+
47
+ const clamp01 = (v: number) => Math.max(0, Math.min(1, v));
48
+ const norm = (v: number, lo: number, hi: number) => (hi > lo ? (v - lo) / (hi - lo) : 0);
49
+
50
+ export const GRADIENT_GREEN = '#1f9e4d';
51
+ export const GRADIENT_RED = '#d6455d';
52
+ const GRADIENT_GREY = '#7c8389';
53
+ /** Validated peak opacity for the green/red variants. */
54
+ const GRADIENT_MAX_OPACITY = 0.45;
55
+ /** Peak opacity for the grey evolution-gradient variant. */
56
+ const GREY_MAX_OPACITY = 0.38;
57
+ /** Benefit/investment zero-line height (fraction of plot height from bottom). */
58
+ export const BENEFIT_ZERO_FRAC = 0.3;
59
+
60
+ function rgba(hex: string, alpha: number) {
61
+ const r = parseInt(hex.slice(1, 3), 16);
62
+ const g = parseInt(hex.slice(3, 5), 16);
63
+ const b = parseInt(hex.slice(5, 7), 16);
64
+ return `rgba(${r},${g},${b},${alpha})`;
65
+ }
66
+
67
+ /**
68
+ * Add stops to a horizontal gradient (offset 0..1 spanning the plot width) for
69
+ * a function-driven opacity profile within [x0, x1] (zero outside).
70
+ */
71
+ function addStops(
72
+ grad: CanvasGradient,
73
+ hex: string,
74
+ opacityFn: (x: number) => number,
75
+ x0: number,
76
+ x1: number,
77
+ maxOp: number = GRADIENT_MAX_OPACITY
78
+ ) {
79
+ const eps = 0.001;
80
+ if (x0 > eps) grad.addColorStop(Math.max(0, x0 - eps), rgba(hex, 0));
81
+ const N = 48;
82
+ for (let i = 0; i <= N; i++) {
83
+ const x = x0 + ((x1 - x0) * i) / N;
84
+ grad.addColorStop(clamp01(x), rgba(hex, clamp01(opacityFn(x)) * maxOp));
85
+ }
86
+ if (x1 < 1 - eps) grad.addColorStop(Math.min(1, x1 + eps), rgba(hex, 0));
87
+ }
88
+
89
+ /**
90
+ * Paint the curve-driven gradient over the plot rectangle [px0,px1]×[py0,py1]
91
+ * in element-local coordinates. `classic` paints nothing.
92
+ */
93
+ export function paintGradientBackground(
94
+ ctx: CanvasRenderingContext2D,
95
+ variant: 'opportunity' | 'benefit' | 'evolution-gradient',
96
+ px0: number,
97
+ px1: number,
98
+ py0: number,
99
+ py1: number
100
+ ) {
101
+ const w = px1 - px0;
102
+ const h = py1 - py0;
103
+
104
+ if (variant === 'evolution-gradient') {
105
+ const grey = ctx.createLinearGradient(px0, 0, px1, 0);
106
+ addStops(grey, GRADIENT_GREY, fGrey, 0, 1, GREY_MAX_OPACITY);
107
+ ctx.fillStyle = grey;
108
+ ctx.fillRect(px0, py0, w, h);
109
+ return;
110
+ }
111
+
112
+ if (variant === 'opportunity') {
113
+ const green = ctx.createLinearGradient(px0, 0, px1, 0);
114
+ addStops(green, GRADIENT_GREEN, x => norm(fDiff(x), RG.lo, RG.hi), DIFF_DOM[0], DIFF_DOM[1]);
115
+ ctx.fillStyle = green;
116
+ ctx.fillRect(px0, py0, w, h);
117
+
118
+ const red = ctx.createLinearGradient(px0, 0, px1, 0);
119
+ addStops(red, GRADIENT_RED, x => norm(fOper(x), RR.lo, RR.hi), OPER_DOM[0], OPER_DOM[1]);
120
+ ctx.fillStyle = red;
121
+ ctx.fillRect(px0, py0, w, h);
122
+ return;
123
+ }
124
+
125
+ // benefit: green where the curve is positive, red where negative.
126
+ const maxPos = RB.hi;
127
+ const maxNeg = -RB.lo;
128
+ const green = ctx.createLinearGradient(px0, 0, px1, 0);
129
+ addStops(green, GRADIENT_GREEN, x => Math.max(0, fBen(x)) / maxPos, 0, 1);
130
+ ctx.fillStyle = green;
131
+ ctx.fillRect(px0, py0, w, h);
132
+
133
+ const red = ctx.createLinearGradient(px0, 0, px1, 0);
134
+ addStops(red, GRADIENT_RED, x => Math.max(0, -fBen(x)) / maxNeg, 0, 1);
135
+ ctx.fillStyle = red;
136
+ ctx.fillRect(px0, py0, w, h);
137
+ }