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.
This commit is contained in:
Sameh Khamis committed 2026-11-11 08:07:36 -08:00
1 parent 3143d106ce
commit 46c98796de
2 files changed
+81 -5

No files matched your search

+19 -5
View File
@@ -2,7 +2,12 @@ import { isPointerFree } from '../../data/inputOwnership.js';
import { inputFor as defaultInputFor } from '../../input/dispatcher.js'; import { inputFor as defaultInputFor } from '../../input/dispatcher.js';
import { fromDegrees } from '../../geo/ellipsoid.js'; import { fromDegrees } from '../../geo/ellipsoid.js';
import { cssColor } from '../../render/color.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 { hitTestWorldOverlay } from '../../overlays/worldOverlay.js';
import { import {
isOwnedByOtherLayer, 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 // 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. // number of reports, in place of that many markers piled there.
function renderCountryBadges(list, drawn, labels) { function renderCountryBadges(list, drawn, labels) {
@@ -621,10 +638,7 @@ export function createNewsLayer({
countryBadges.set(group.key, group); countryBadges.set(group.key, group);
const position = fromDegrees(group.lon, group.lat); const position = fromDegrees(group.lon, group.lat);
const size = COUNTRY_BADGE_PX; const size = COUNTRY_BADGE_PX;
const sprite = countSprite( const sprite = countSprite(group.count, size * scenePixelRatio());
group.count,
size * (globalThis.devicePixelRatio || 1),
);
const id = `${COUNTRY_PREFIX}${group.key}`; const id = `${COUNTRY_PREFIX}${group.key}`;
if (sprite) if (sprite)
list.place('icons', { list.place('icons', {
+62
View File
@@ -48,6 +48,7 @@ function fixture(
centre = () => ({ lat: 49, lon: 36 }), centre = () => ({ lat: 49, lon: 36 }),
credits = [], credits = [],
now = () => NOW, now = () => NOW,
quality,
} = {}, } = {},
) { ) {
const calls = { feed: [], hotspots: 0, outline: 0 }; const calls = { feed: [], hotspots: 0, outline: 0 };
@@ -112,6 +113,7 @@ function fixture(
const { draw, unbind } = bindFakeDraw(viewer, { const { draw, unbind } = bindFakeDraw(viewer, {
camera: cameraPort.port, camera: cameraPort.port,
frame: { requestRender() {}, on: () => () => {} }, frame: { requestRender() {}, on: () => () => {} },
...(quality ? { quality } : {}),
}); });
// The items of the layer's draw set named `part` (`points`, `icons`, …). // The items of the layer's draw set named `part` (`points`, `icons`, …).
const items = (part) => [ 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')); 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) => { test('Local reports are listed an hour at most while they cannot be read again', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] }); t.mock.timers.enable({ apis: ['setTimeout'] });
let at = NOW; let at = NOW;