feat(cctv): green count markers that grow with the count and carry a camera icon

A count marker now says what it counts and how much:

- Its height grows with the count, from 40 px for two cameras to 58 px for
  several thousand, with clear fill around the digits.
- A small single-camera map icon sits under the top of the ring and the
  number just below the centre, so the two never touch. The icon is built
  from the same sprite as a single camera on the map.
- Thousands print as 1.7K or 12K in every UI language.
- The camera layer is green: count markers and idle single cameras share
  the colour, the active camera stays gold and live-video cameras violet.
This commit is contained in:
German Khokhlov committed 2026-11-18 20:48:33 +04:00
1 parent aabfa5e084
commit c536d22ff0
10 files changed
+806 -215

No files matched your search

+208 -38
View File
@@ -5,19 +5,112 @@ import {
} from '../../data/cctvCellCounts.js';
import { WORLD_OVERLAY_STYLE } from '../../overlays/worldOverlayTokens.js';
import {
COUNT_MARKER_PX,
COUNT_MERGE_DISTANCE_PX,
CAMERA_GREEN,
IDLE_CAMERA_ICON,
COUNT_BASE_PX,
COUNT_PX_PER_DECADE,
COUNT_MIN_HEIGHT_PX,
COUNT_MAX_HEIGHT_PX,
COUNT_TEXT_PADDING_PX,
COUNT_GLYPH_MIN_PX,
COUNT_GLYPH_MAX_PX,
COUNT_GLYPH_TOP_MIN_PX,
COUNT_GLYPH_TOP_MAX_PX,
COUNT_GLYPH_PADDING_PX,
COUNT_LABEL_DROP,
COUNT_MERGE_GAP_PX,
COUNT_MERGE_PASSES,
} from './policy.js';
const FONT = WORLD_OVERLAY_STYLE.fontTitle;
const RING = '#75e7ff';
// Reserve the camera's landscape slot even while the map sprite is loading.
const GLYPH_ASPECT = 1.5;
const RING_PX = 2;
const RIM_PX = 1;
const STROKE_INSET = RIM_PX + RING_PX / 2;
/** Apply the billboard's RGB tint to the sprite and find its painted bounds. */
function rasterizeGlyph(canvas, image, size, dpr, color) {
if (!canvas) return null;
// The artwork occupies just over half the height of the square SVG viewport.
const side = 2 * Math.ceil(size * dpr);
canvas.width = canvas.height = side;
const ctx = canvas.getContext('2d');
if (!ctx) return null;
ctx.drawImage(image, 0, 0, side, side);
const pixels = ctx.getImageData(0, 0, side, side);
const { data } = pixels;
const rgb = Number.parseInt(color.slice(1), 16);
const red = (rgb >> 16) / 255;
const green = ((rgb >> 8) & 255) / 255;
const blue = (rgb & 255) / 255;
let left = side,
top = side,
right = -1,
bottom = -1;
for (let y = 0; y < side; y++) {
for (let x = 0; x < side; x++) {
const i = (y * side + x) * 4;
if (!data[i + 3]) continue;
data[i] *= red;
data[i + 1] *= green;
data[i + 2] *= blue;
left = Math.min(left, x);
right = Math.max(right, x);
top = Math.min(top, y);
bottom = y;
}
}
if (right < 0) return null;
ctx.putImageData(pixels, 0, 0);
const width = right - left + 1;
const height = bottom - top + 1;
const scale = Math.min(size / height, (size * GLYPH_ASPECT) / width);
return {
canvas,
left,
top,
width,
height,
drawWidth: width * scale,
drawHeight: height * scale,
};
}
/** Load the map artwork once, then share each tinted device-density raster. */
export function createCameraGlyphCache(createCanvas, source, onLoad) {
const glyphs = new Map();
let image;
let ready = false;
return (size, dpr, color) => {
if (!image && typeof Image !== 'undefined') {
image = new Image();
image.onload = () => {
ready = true;
onLoad?.();
};
image.src = source;
}
if (!ready) return null;
const key = `${size}:${dpr}:${color}`;
if (!glyphs.has(key)) {
glyphs.set(key, rasterizeGlyph(createCanvas(), image, size, dpr, color));
}
return glyphs.get(key);
};
}
/** Merged camera groups and their last painted hit rectangles in CSS pixels. */
export function createCountMarkers(registerPaintLane, id, refreshView) {
export function createCountMarkers(
registerPaintLane,
id,
refreshView,
createCanvas = () => globalThis.document?.createElement('canvas'),
) {
let dpr = Math.max(1, Number(globalThis.window?.devicePixelRatio) || 1);
const markers = [];
const hits = [];
const widths = new Map();
const textMetrics = new Map();
const fonts = globalThis.document?.fonts;
let measureCtx;
let hitCount = 0;
@@ -26,30 +119,81 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
active: false,
target: 'shared',
});
const glyphFor = createCameraGlyphCache(
createCanvas,
IDLE_CAMERA_ICON,
() => {
if (!markers.length) return;
refreshGlyphs();
lane?.requestPaint();
},
);
fonts?.addEventListener?.('loadingdone', fontsLoaded);
/** Font changes can widen a pill enough to merge it with its neighbor. */
function fontsLoaded() {
widths.clear();
textMetrics.clear();
if (!markers.length) return;
refreshView();
lane?.requestPaint();
}
/** Short labels fit the circle. A longer label leaves room for rounded ends. */
function widthForText(text) {
if (text.length <= 3) return COUNT_MARKER_PX;
let width = widths.get(text);
if (width === undefined) {
measureCtx ??= document.createElement('canvas').getContext('2d');
measureCtx.font = FONT;
width = Math.max(
COUNT_MARKER_PX + 1,
Math.ceil(measureCtx.measureText(text).width) + 20,
);
widths.set(text, width);
/** Seat the glyph under the top of the ring and the count just below centre. */
function sizeFor(count) {
const text = formatCameraCount(count);
let metrics = textMetrics.get(text);
if (!metrics) {
measureCtx ??= createCanvas()?.getContext('2d');
if (measureCtx) {
measureCtx.font = FONT;
measureCtx.textBaseline = 'middle';
metrics = measureCtx.measureText(text);
} else {
metrics = { width: text.length * 8 };
}
textMetrics.set(text, metrics);
}
return width;
const diameter = COUNT_BASE_PX + COUNT_PX_PER_DECADE * Math.log10(count);
const clamped = Math.max(
COUNT_MIN_HEIGHT_PX,
Math.min(COUNT_MAX_HEIGHT_PX, diameter),
);
const height = 2 * Math.round(clamped / 2);
const grown =
(height - COUNT_MIN_HEIGHT_PX) /
(COUNT_MAX_HEIGHT_PX - COUNT_MIN_HEIGHT_PX);
const glyphHeight =
COUNT_GLYPH_MIN_PX + (COUNT_GLYPH_MAX_PX - COUNT_GLYPH_MIN_PX) * grown;
const radius = height / 2 - RIM_PX - RING_PX;
const glyphTop =
COUNT_GLYPH_TOP_MIN_PX +
(COUNT_GLYPH_TOP_MAX_PX - COUNT_GLYPH_TOP_MIN_PX) * grown -
radius;
// Width-only metrics use half the 12 px type size on each side of the baseline.
const ascent = metrics.actualBoundingBoxAscent ?? 6;
const descent = metrics.actualBoundingBoxDescent ?? 6;
const labelY = COUNT_LABEL_DROP * radius + (ascent - descent) / 2;
// Each row must clear the ring at the edge where the circle is narrowest.
const chordAt = (y) => 2 * Math.sqrt(Math.max(0, radius ** 2 - y ** 2));
const extra = Math.max(
0,
Math.ceil(
Math.max(
metrics.width + 2 * COUNT_TEXT_PADDING_PX - chordAt(labelY + descent),
glyphHeight * GLYPH_ASPECT +
2 * COUNT_GLYPH_PADDING_PX -
chordAt(glyphTop),
),
),
);
return {
text,
width: height + extra,
height,
glyphHeight,
glyphBottom: glyphTop + glyphHeight,
labelY,
};
}
/** Reuse marker slots in ground-key order, and invalidate only changed frames. */
@@ -65,9 +209,8 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
const merged = mergeCountCells(
visible,
project,
(count) => widthForText(formatCameraCount(count)),
COUNT_MARKER_PX,
COUNT_MERGE_DISTANCE_PX,
sizeFor,
COUNT_MERGE_GAP_PX,
);
if (merged.length === visible.length) break;
visible = merged;
@@ -77,8 +220,7 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
const cell = visible[i];
const marker = markers[i] ?? (markers[i] = { focus: 1 });
const last = marker.cell;
const text = formatCameraCount(cell.count);
const width = widthForText(text);
const size = sizeFor(cell.count);
if (
!last ||
last.count !== cell.count ||
@@ -98,10 +240,14 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
) {
changed = true;
}
changed ||= text !== marker.text || width !== marker.width;
changed ||=
size.text !== marker.text ||
size.width !== marker.width ||
size.glyphBottom !== marker.glyphBottom ||
size.labelY !== marker.labelY;
marker.cell = cell;
marker.text = text;
marker.width = width;
Object.assign(marker, size);
marker.glyph = glyphFor(size.glyphHeight, dpr, CAMERA_GREEN);
// Paint never grows the hit buffer, even when a pan reveals every group.
hits[i] ??= {};
}
@@ -111,6 +257,12 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
if (changed) lane?.requestPaint();
}
/** Attach the raster for the current overlay density to each visible group. */
function refreshGlyphs() {
for (const marker of markers)
marker.glyph = glyphFor(marker.glyphHeight, dpr, CAMERA_GREEN);
}
/** Draw at device density using the host's CSS-pixel transform and projection. */
function paint(frame) {
if (!markers.length) {
@@ -121,7 +273,10 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
}
hitCount = 0;
const { ctx, occluder, viewProjectionMatrix: m } = frame;
const height = COUNT_MARKER_PX;
if (dpr !== frame.dpr) {
dpr = frame.dpr;
refreshGlyphs();
}
ctx.save();
try {
ctx.font = FONT;
@@ -129,6 +284,7 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
ctx.textBaseline = 'middle';
for (let i = 0; i < markers.length; i++) {
const marker = markers[i];
const { height } = marker;
const { x: px, y: py, z: pz } = marker.pos;
const w = m[3] * px + m[7] * py + m[11] * pz + m[15];
if (!(w > 0) || (occluder && !occluder.isPointVisible(marker.pos)))
@@ -155,22 +311,36 @@ export function createCountMarkers(registerPaintLane, id, refreshView) {
ctx.globalAlpha = marker.focus;
ctx.beginPath();
ctx.roundRect(
rect.x + 2,
rect.y + 2,
rect.w - 4,
height - 4,
height / 2 - 2,
rect.x + STROKE_INSET,
rect.y + STROKE_INSET,
rect.w - 2 * STROKE_INSET,
height - 2 * STROKE_INSET,
height / 2 - STROKE_INSET,
);
ctx.strokeStyle = 'rgba(8, 14, 20, 0.85)';
ctx.lineWidth = 4;
ctx.lineWidth = RING_PX + 2 * RIM_PX;
ctx.stroke();
ctx.fillStyle = '#0e1720';
ctx.fill();
ctx.strokeStyle = RING;
ctx.lineWidth = 2;
ctx.strokeStyle = CAMERA_GREEN;
ctx.lineWidth = RING_PX;
ctx.stroke();
ctx.fillStyle = RING;
ctx.fillText(marker.text, x, y);
ctx.fillStyle = CAMERA_GREEN;
const glyph = marker.glyph;
if (glyph) {
ctx.drawImage(
glyph.canvas,
glyph.left,
glyph.top,
glyph.width,
glyph.height,
x - glyph.drawWidth / 2,
y + marker.glyphBottom - glyph.drawHeight,
glyph.drawWidth,
glyph.drawHeight,
);
}
ctx.fillText(marker.text, x, y + marker.labelY);
rect.cell = marker.cell;
hitCount++;
}
+50 -15
View File
@@ -71,11 +71,38 @@ export const VIEW_CELL_PX = 40;
export const VIEW_MIN_CELL_M = 60;
// Diameter of a grouped camera marker in CSS pixels.
export const COUNT_MARKER_PX = 34;
// Diameter at one camera before applying the size limits, in CSS pixels.
export const COUNT_BASE_PX = 38;
// Separation of two circular counts, including a four-pixel gap between rings.
export const COUNT_MERGE_DISTANCE_PX = COUNT_MARKER_PX + 4;
// Extra diameter for each tenfold increase in camera count.
export const COUNT_PX_PER_DECADE = 5;
// Minimum height of a count marker in CSS pixels.
export const COUNT_MIN_HEIGHT_PX = 40;
// Maximum height of a count marker in CSS pixels.
export const COUNT_MAX_HEIGHT_PX = 58;
// Clear fill between the count and the inside of the ring, in CSS pixels.
export const COUNT_TEXT_PADDING_PX = 6;
// Height of the camera glyph in the smallest and the largest marker.
export const COUNT_GLYPH_MIN_PX = 7.5;
export const COUNT_GLYPH_MAX_PX = 10;
// Clear fill above the glyph in the smallest and the largest marker.
export const COUNT_GLYPH_TOP_MIN_PX = 3;
export const COUNT_GLYPH_TOP_MAX_PX = 4;
// Clear fill between the glyph's top corners and the inside of the ring.
export const COUNT_GLYPH_PADDING_PX = 2;
// The middle of the digits sits this share of the inner radius below the
// marker's centre, which leaves the glyph clear of the count.
export const COUNT_LABEL_DROP = 0.2;
// Minimum gap between the outer rims of two separate markers.
export const COUNT_MERGE_GAP_PX = 4;
// Recheck moved centres and wider labels while bounding work on motion ticks.
export const COUNT_MERGE_PASSES = 3;
@@ -194,25 +221,30 @@ export const DEFAULT_CAMERA_CALIBRATION = Object.freeze({
* 2× display draws it at 60 device px, and it stays sharp there.
*/
export const CAMERA_ICON = (() => {
function cameraIcon(neutral) {
const tone = (original, gray) => (neutral ? gray : original);
const stroke = tone('#75e7ff', '#fff');
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 36 36">
<defs>
<linearGradient id="lens" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#c9f6ff"/>
<stop offset="45%" stop-color="#6fd9ff"/>
<stop offset="100%" stop-color="#1a5f78"/>
<stop offset="0%" stop-color="${tone('#c9f6ff', '#fff')}"/>
<stop offset="45%" stop-color="${tone('#6fd9ff', '#ddd')}"/>
<stop offset="100%" stop-color="${tone('#1a5f78', '#666')}"/>
</linearGradient>
</defs>
<g transform="translate(4 6)">
<rect x="0" y="8" width="20" height="9" rx="2.5" fill="#0e1720" stroke="#75e7ff" stroke-width="1.2"/>
<rect x="17" y="10" width="10" height="5" rx="1.5" fill="#132433" stroke="#75e7ff" stroke-width="1"/>
<circle cx="24" cy="12.5" r="3.1" fill="url(#lens)" stroke="#dbfbff" stroke-width="0.8"/>
<rect x="6.4" y="17" width="4.2" height="8.5" rx="1.2" fill="#10212d" stroke="#75e7ff" stroke-width="1"/>
<rect x="4.2" y="24" width="8.6" height="2.5" rx="1.1" fill="#0b151d" stroke="#4ecde7" stroke-width="0.8"/>
<rect x="0" y="8" width="20" height="9" rx="2.5" fill="${tone('#0e1720', '#181818')}" stroke="${stroke}" stroke-width="1.2"/>
<rect x="17" y="10" width="10" height="5" rx="1.5" fill="${tone('#132433', '#222')}" stroke="${stroke}" stroke-width="1"/>
<circle cx="24" cy="12.5" r="3.1" fill="url(#lens)" stroke="${tone('#dbfbff', '#fff')}" stroke-width="0.8"/>
<rect x="6.4" y="17" width="4.2" height="8.5" rx="1.2" fill="${tone('#10212d', '#202020')}" stroke="${stroke}" stroke-width="1"/>
<rect x="4.2" y="24" width="8.6" height="2.5" rx="1.1" fill="${tone('#0b151d', '#151515')}" stroke="${tone('#4ecde7', '#ccc')}" stroke-width="0.8"/>
</g>
</svg>`;
return 'data:image/svg+xml;base64,' + btoa(svg);
})();
}
export const CAMERA_ICON = cameraIcon(false);
export const IDLE_CAMERA_ICON = cameraIcon(true);
/**
* Seed camera definitions used when no live sources are available.
@@ -450,7 +482,10 @@ export const CAMERA_SEEDS = [
// Visual style constants: renderer colours, [r, g, b, a] in 0..1
// ---------------------------------------------------------------------------
export const IDLE_CAMERA_COLOR = hexColor('#6be8ff', 0.88);
/** Shared accent for idle still cameras and grouped cameras. */
export const CAMERA_GREEN = '#5dff8f';
export const IDLE_CAMERA_COLOR = hexColor(CAMERA_GREEN);
export const ACTIVE_CAMERA_COLOR = hexColor('#ffd97a', 0.95);
+13 -10
View File
@@ -4,6 +4,7 @@ import { isHeadingEstimated } from './headingConfidence.js';
import {
ACTIVE_CAMERA_COLOR,
CAMERA_ICON,
IDLE_CAMERA_ICON,
IDLE_CAMERA_COLOR,
LIVE_CAMERA_COLOR,
ACTIVE_COVERAGE_CENTER,
@@ -13,7 +14,6 @@ import {
IDLE_COVERAGE_EDGE_MUTED,
ACTIVE_COVERAGE_EDGE_DEPTHFAIL,
IDLE_COVERAGE_COLOR,
COUNT_MARKER_PX,
VIEW_CELL_PX,
VIEW_ICON_MARGIN,
VIEW_MIN_CELL_M,
@@ -33,6 +33,10 @@ import { poseProjector } from '../../geo/frustum.js';
import { horizonFrom } from '../../geo/horizon.js';
import { distance } from '../../geo/vector.js';
const IDLE_ICON_STYLE = { image: IDLE_CAMERA_ICON, color: IDLE_CAMERA_COLOR };
const ACTIVE_ICON_STYLE = { image: CAMERA_ICON, color: ACTIVE_CAMERA_COLOR };
const LIVE_ICON_STYLE = { image: CAMERA_ICON, color: LIVE_CAMERA_COLOR };
/** An RGBA colour with another alpha. */
const withAlpha = (color, alpha) => [color[0], color[1], color[2], alpha];
@@ -164,9 +168,8 @@ export function createRendering({
return {
id,
pos: copyPoint(record.position),
image: CAMERA_ICON,
...cameraIconStyleFor(record),
size: [CAMERA_ICON_PX, CAMERA_ICON_PX],
color: cameraColorFor(record),
scale: 1,
scaleByDistance: CAMERA_SCALE_BY_DISTANCE,
disableDepthTest: Infinity,
@@ -215,12 +218,12 @@ export function createRendering({
icons.put({ ...item, pos: copyPoint(record.position) });
}
/** Icon color: active wins, then live-capable video feeds, then idle stills. */
function cameraColorFor(record) {
/** Select the sprite and tint together when a camera changes state. */
function cameraIconStyleFor(record) {
if (record.camera.id === layerState._activeCameraId)
return ACTIVE_CAMERA_COLOR;
if (parts.model.isVideoFeedCamera(record.camera)) return LIVE_CAMERA_COLOR;
return IDLE_CAMERA_COLOR;
return ACTIVE_ICON_STYLE;
if (parts.model.isVideoFeedCamera(record.camera)) return LIVE_ICON_STYLE;
return IDLE_ICON_STYLE;
}
const {
@@ -326,7 +329,7 @@ export function createRendering({
marker.pos,
true,
marker.width,
COUNT_MARKER_PX,
marker.height,
marker.focus,
);
if (factor !== null) marker.focus = factor;
@@ -611,7 +614,7 @@ export function createRendering({
for (const record of layerState._records) {
const isActive = record.camera.id === activeId;
const icon = patchedCameraIcon(record.camera.id, {
color: cameraColorFor(record),
...cameraIconStyleFor(record),
scale: isActive ? 1.25 : 1.0,
});
if (icon) restyled.push(icon);
+27 -1
View File
@@ -1,7 +1,7 @@
// src/layers/cctv/rendering.test.mjs
// Icon colors under focus de-emphasis (#1200): the focus pass fades each icon
// from the same base color the coverage refresh assigns, so a video-feed camera
// stays violet while it dims instead of falling back to the idle cyan.
// stays violet while it dims instead of falling back to the idle green.
// Teardown lets go of the icon set of the viewer that drew it.
import { test } from 'node:test';
import assert from 'node:assert/strict';
@@ -114,6 +114,32 @@ test('focus de-emphasis fades each icon from its own color', () => {
}
});
test('focus uses the painted half-width and half-height of each count', () => {
const markers = [
{ width: 40, height: 40 },
{ width: 72, height: 58 },
];
const sizes = [];
const rendering = createRendering({
state: { _counts: { markers } },
services: {
focus: {
focusPassIsNeeded: () => true,
advanceSpriteFocus(_owner, { spriteHalfWidthPx, spriteHalfHeightPx }) {
sizes.push([spriteHalfWidthPx, spriteHalfHeightPx]);
return { factor: 1 };
},
focusAlphaNeedsWrite: () => false,
},
},
});
rendering.applyCctvFocusDeemphasis({});
assert.deepEqual(sizes, [
[20, 20],
[36, 29],
]);
});
test('the icons go with the viewer that drew them, whichever tears down', () => {
const first = {};
const second = {};