From 46c98796de9cf94f764e100f776cae2dfb0e98e6 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 11 Nov 2026 08:07:36 -0800 Subject: [PATCH] News country badges are painted at the scene's pixel ratio A badge's count image is sized by the renderer's own resolution scale (its quality port's pixelRatio, 1 while that is browser recommended), as the other icon sets are, not by the device pixel ratio. --- src/layers/news/index.js | 24 ++++++++++--- src/layers/news/layer.test.mjs | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/layers/news/index.js b/src/layers/news/index.js index 7e60d3dd0..4204ba169 100644 --- a/src/layers/news/index.js +++ b/src/layers/news/index.js @@ -2,7 +2,12 @@ import { isPointerFree } from '../../data/inputOwnership.js'; import { inputFor as defaultInputFor } from '../../input/dispatcher.js'; import { fromDegrees } from '../../geo/ellipsoid.js'; import { cssColor } from '../../render/color.js'; -import { findCamera, findDraw, findFrame } from '../../render/rendererOf.js'; +import { + findCamera, + findDraw, + findFrame, + rendererOf, +} from '../../render/rendererOf.js'; import { hitTestWorldOverlay } from '../../overlays/worldOverlay.js'; import { isOwnedByOtherLayer, @@ -610,6 +615,18 @@ export function createNewsLayer({ } } + /** + * Drawing-buffer pixels per CSS pixel: the renderer's own resolution + * scale (its quality port's `pixelRatio`, 1 while that is 0, browser + * recommended), not the device's. + */ + function scenePixelRatio() { + const ratio = viewer + ? rendererOf(viewer)?.quality?.get?.()?.pixelRatio + : undefined; + return Number.isFinite(ratio) && ratio > 0 ? ratio : 1; + } + // News placed no closer than a country: one badge at its centre with the // number of reports, in place of that many markers piled there. function renderCountryBadges(list, drawn, labels) { @@ -621,10 +638,7 @@ export function createNewsLayer({ countryBadges.set(group.key, group); const position = fromDegrees(group.lon, group.lat); const size = COUNTRY_BADGE_PX; - const sprite = countSprite( - group.count, - size * (globalThis.devicePixelRatio || 1), - ); + const sprite = countSprite(group.count, size * scenePixelRatio()); const id = `${COUNTRY_PREFIX}${group.key}`; if (sprite) list.place('icons', { diff --git a/src/layers/news/layer.test.mjs b/src/layers/news/layer.test.mjs index 796eb6c3b..c5312fc48 100644 --- a/src/layers/news/layer.test.mjs +++ b/src/layers/news/layer.test.mjs @@ -48,6 +48,7 @@ function fixture( centre = () => ({ lat: 49, lon: 36 }), credits = [], now = () => NOW, + quality, } = {}, ) { const calls = { feed: [], hotspots: 0, outline: 0 }; @@ -112,6 +113,7 @@ function fixture( const { draw, unbind } = bindFakeDraw(viewer, { camera: cameraPort.port, frame: { requestRender() {}, on: () => () => {} }, + ...(quality ? { quality } : {}), }); // The items of the layer's draw set named `part` (`points`, `icons`, …). const items = (part) => [ @@ -919,6 +921,66 @@ test("a country's own news is one badge with a count, not markers piled on its c assert.ok(ids().includes('news:report:own-1~0')); }); +test("a country badge is painted at the scene's pixel ratio, not the device's", async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + // A canvas that paints nothing and names its size in its data URL. + const context = new Proxy( + {}, + { get: () => () => ({ width: 0 }), set: () => true }, + ); + const originalDocument = globalThis.document; + const originalRatio = globalThis.devicePixelRatio; + globalThis.document = { + createElement: () => ({ + width: 0, + height: 0, + getContext: () => context, + toDataURL() { + return `data:image/png;test,${this.width}`; + }, + }), + }; + globalThis.devicePixelRatio = 3; + t.after(() => { + if (originalDocument === undefined) delete globalThis.document; + else globalThis.document = originalDocument; + if (originalRatio === undefined) delete globalThis.devicePixelRatio; + else globalThis.devicePixelRatio = originalRatio; + }); + const anchor = { + lat: 49, + lon: 36, + place: 'Ukraine', + precision: 'country', + mappable: true, + }; + const { layer, items } = fixture(t, { + height: 1_000_000, + quality: { get: () => ({ pixelRatio: 2 }) }, + source: { + getFeed: async () => ({ + reports: [ + report('own-1', { relevance: 'minor', pins: [anchor] }), + report('own-2', { relevance: 'minor', pins: [anchor] }), + ], + nextBefore: null, + }), + }, + }); + layer.enable(); + await layer.update(); + layer.setParams({ filters: { section: 'local' } }); + await flush(); + const badge = items('icons').find( + (item) => String(item.id) === 'news:country:Ukraine', + ); + assert.ok(badge, 'the badge is an icon'); + // 34 CSS px at 2 drawing-buffer pixels each: a 68 px disc on an 84 px + // canvas (64/52 of it). At the device's 3 it would be 126. + assert.equal(badge.image, 'data:image/png;test,84'); + assert.deepEqual(badge.size, [42, 42]); +}); + test('Local reports are listed an hour at most while they cannot be read again', async (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }); let at = NOW;