Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ Run `npm run lint && npm run check && npm run build && npm test` in this directo
## Compliance carve-outs

- **Hardcoded colors (computed):** `src/explore/view/potentialColor.ts` generates RGB/RGBA CSS by blending `ElectricFieldMapperColors` profile values; this is not an independent hardcoded palette.

## Accessibility

Follow [Baton/ACCESSIBILITY.md](https://github.com/OpenLyceum/Baton/blob/main/ACCESSIBILITY.md): PDOM names, per-screen `*ScreenSummaryContent`, and keyboard-help content. Document any intentional deviations under Compliance carve-outs.
12 changes: 12 additions & 0 deletions biome-plugins/no-math-round.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Flags Math.round(). Prefer roundSymmetric from scenerystack/dot so positive and
// negative values round the same way (see phetsims/dot#35).
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-math-round: <reason>
language js

`Math.round($value)` as $call where {
register_diagnostic(
span = $call,
message = "Use roundSymmetric(value) from scenerystack/dot instead of Math.round(), which rounds positive and negative numbers differently.",
severity = "warn"
)
}
18 changes: 18 additions & 0 deletions biome-plugins/no-native-timer.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Flags bare setTimeout / setInterval (and window.* forms). Sim code should step
// from the model/view step(dt) chain, or use stepTimer from scenerystack/axon when a
// delayed callback is truly needed. Method calls like stepTimer.setTimeout are allowed.
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-native-timer: <reason>
language js

or {
`setTimeout($...)`,
`setInterval($...)`,
`window.setTimeout($...)`,
`window.setInterval($...)`
} as $call where {
register_diagnostic(
span = $call,
message = "Do not call setTimeout/setInterval in sim code; step from model/view step(dt), or use stepTimer from scenerystack/axon for a delayed callback.",
severity = "warn"
)
}
13 changes: 13 additions & 0 deletions biome-plugins/no-window-inner-size.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Flags window.innerWidth / window.innerHeight. Prefer Sim.dimensionProperty (or the
// screen's layoutBounds / visibleBounds) so layout tracks the sim display, not the
// browser chrome.
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-window-inner-size: <reason>
language js

or { `window.innerWidth`, `window.innerHeight` } as $hit where {
register_diagnostic(
span = $hit,
message = "Use Sim.dimensionProperty (or layoutBounds/visibleBounds) instead of window.innerWidth/innerHeight.",
severity = "warn"
)
}
35 changes: 35 additions & 0 deletions biome-plugins/require-property-suffix.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Flags axon Property instances whose binding name does not end in "Property".
// Covers same-line const/let/var and this.field for the common *Property constructors.
// Class fields (`public readonly foo = new NumberProperty(...)`, bare `foo = ...`) are
// not reliably matched by Biome GritQL — rename those in review or with a codemod.
// Type-only annotations are out of scope (PhET's rule is type-checker based).
// Suppress a deliberate use with: // biome-ignore lint/plugin/require-property-suffix: <reason>
// GritQL regexes here must use non-capturing groups (?:...); a plain (...) silently never matches.
language js

or {
`const $name = new $ctor($...)`,
`let $name = new $ctor($...)`,
`var $name = new $ctor($...)`,
`this.$name = new $ctor($...)`
} as $stmt where {
$ctor <: or {
`Property`,
`BooleanProperty`,
`NumberProperty`,
`StringProperty`,
`DerivedProperty`,
`DerivedStringProperty`,
`DynamicProperty`,
`TinyProperty`,
`EnumerationProperty`,
`StringUnionProperty`,
`ProfileColorProperty`
},
not $name <: r"(?s)(?:.*(?:Property|PROPERTY)|property|_property)$",
register_diagnostic(
span = $name,
message = "Axon Property bindings must end in \"Property\" (e.g. massProperty, not mass).",
severity = "warn"
)
}
10 changes: 9 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"assist": {
"actions": {
"source": {
"organizeImports": "on"
"organizeImports": "on",
"useSortedPackageJson": "on"
}
}
},
Expand Down Expand Up @@ -82,7 +83,9 @@
"style": {
"noCommonJs": "error",
"noDefaultExport": "off",
"noEnum": "error",
"noExportedImports": "warn",
"noNamespace": "error",
"noParameterAssign": "error",
"preset": "recommended",
"useBlockStatements": "error",
Expand All @@ -98,6 +101,7 @@
"strictCase": false
}
},
"useAsConstAssertion": "warn",
"useForOf": "warn",
"useImportType": "error",
"useNamingConvention": {
Expand Down Expand Up @@ -170,6 +174,10 @@
"plugins": [
"./biome-plugins/no-native-tofixed.grit",
"./biome-plugins/no-math-random.grit",
"./biome-plugins/no-math-round.grit",
"./biome-plugins/no-native-timer.grit",
"./biome-plugins/no-window-inner-size.grit",
"./biome-plugins/require-property-suffix.grit",
"./biome-plugins/no-hardcoded-text.grit",
{
"path": "./biome-plugins/no-inline-color.grit",
Expand Down
57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
{
"name": "electric-field-mapper",
"private": true,
"version": "0.0.0",
"type": "module",
"private": true,
"description": "Map electric fields from movable charges with vector arrows, field lines, and a measuring probe.",
"license": "AGPL-3.0-or-later",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/ElectricFieldMapper.git"
},
"keywords": [
"electric field",
"field lines",
Expand All @@ -20,33 +14,45 @@
"pwa",
"interactive"
],
"homepage": "https://openlyceum.github.io/ElectricFieldMapper",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/ElectricFieldMapper.git"
},
"license": "AGPL-3.0-or-later",
"type": "module",
"scripts": {
"start": "vite",
"dev": "vite",
"build": "tsc && vite build",
"build:single": "tsc && vite build --mode single",
"preview": "vite preview",
"lint": "biome check --error-on-warnings .",
"format": "biome format --write .",
"fix": "biome check --write .",
"check": "tsc --noEmit && tsc -p tsconfig.scripts.json --noEmit && tsc -p tsconfig.test.json --noEmit",
"check:unused": "knip",
"check:architecture": "depcruise src",
"check:unused": "knip",
"clean": "rm -rf dist",
"dev": "vite",
"fix": "biome check --write .",
"format": "biome format --write .",
"icons": "node scripts/generate-icons.ts",
"knip": "knip",
"lint": "biome check --error-on-warnings .",
"prepare": "test \"$(git rev-parse --show-toplevel 2>/dev/null)\" = \"$PWD\" && git config core.hooksPath .githooks || true",
"preview": "vite preview",
"release": "npm run check && npm run lint && npm run check:architecture && npm run build && npm test && npm version patch && git push && git push --tags",
"watch": "tsc --watch",
"start": "vite",
"test": "vitest run",
"test:watch": "vitest",
"test:architecture": "node --test tests/*.node-test.ts",
"test:fuzz": "node scripts/test-fuzz.ts",
"test:fuzz:quick": "node scripts/test-fuzz.ts 10",
"test:fuzz:long": "node scripts/test-fuzz.ts 300",
"test:fuzz:quick": "node scripts/test-fuzz.ts 10",
"test:soak": "npm run test:soak:headless && npm run test:soak:browser",
"test:soak:headless": "vitest run --config vitest.soak.config.ts",
"test:soak:browser": "npx playwright test tests/soak/browser.spec.ts",
"icons": "node scripts/generate-icons.ts",
"clean": "rm -rf dist",
"prepare": "test \"$(git rev-parse --show-toplevel 2>/dev/null)\" = \"$PWD\" && git config core.hooksPath .githooks || true",
"knip": "knip",
"test:architecture": "node --test tests/*.node-test.ts"
"test:soak:headless": "vitest run --config vitest.soak.config.ts",
"test:watch": "vitest",
"watch": "tsc --watch"
},
"overrides": {
"brace-expansion": "~5.0.12",
"lodash": "~4.18.1",
"three": "~0.125.2"
},
"dependencies": {
"scenerystack": "^3.0.0"
Expand All @@ -69,11 +75,6 @@
"engines": {
"node": ">=26"
},
"overrides": {
"lodash": "~4.18.1",
"three": "~0.125.2",
"brace-expansion": "~5.0.12"
},
"devEngines": {
"runtime": {
"name": "node",
Expand Down
6 changes: 3 additions & 3 deletions src/explore/model/ExploreModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
StringProperty,
type TReadOnlyProperty,
} from "scenerystack/axon";
import { Bounds2, Vector2, Vector2Property } from "scenerystack/dot";
import { Bounds2, roundSymmetric, Vector2, Vector2Property } from "scenerystack/dot";
import type { TModel } from "scenerystack/joist";
import { CHARGE_PRESETS, type ChargePreset, isChargePreset } from "../../common/model/ChargePresets.js";
import type { FieldDisplayPreferences } from "../../common/model/FieldDisplayOptions.js";
Expand Down Expand Up @@ -216,7 +216,7 @@ export class ExploreModel implements TModel {

public snapPosition(point: Point): Vector2 {
const snap = (value: number, min: number, max: number) => {
const rounded = Math.round(value / GRID_SPACING_M) * GRID_SPACING_M;
const rounded = roundSymmetric(value / GRID_SPACING_M) * GRID_SPACING_M;
// Visible edges need not lie on the grid. Clamp to whole grid points inside the margin.
const firstGridPoint = Math.ceil((min + GRID_SPACING_M) / GRID_SPACING_M) * GRID_SPACING_M;
const lastGridPoint = Math.floor((max - GRID_SPACING_M) / GRID_SPACING_M) * GRID_SPACING_M;
Expand Down Expand Up @@ -278,7 +278,7 @@ export class ExploreModel implements TModel {
const spacing = GRID_SPACING_M / GRID_MINOR_LINES_PER_MAJOR;
const bounds = this.fieldBoundsProperty.value;
const snap = (value: number, min: number, max: number): number => {
const rounded = Math.round(Math.round(value / spacing) * spacing * 1e6) / 1e6;
const rounded = roundSymmetric(roundSymmetric(value / spacing) * spacing * 1e6) / 1e6;
return Math.max(min, Math.min(max, rounded));
};
return new Vector2(snap(point.x, bounds.minX, bounds.maxX), snap(point.y, bounds.minY, bounds.maxY));
Expand Down
7 changes: 4 additions & 3 deletions src/explore/model/FieldPhysics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { roundSymmetric } from "scenerystack/dot";
import type { VoltageScale } from "../../common/model/FieldDisplayOptions.js";
import type { Point } from "../../common/model/Point.js";
import { FIELD_PHYSICS_SETTINGS } from "../../ElectricFieldMapperConstants.js";
Expand Down Expand Up @@ -264,7 +265,7 @@ function symmetricPairFieldLines(charges: readonly Charge[], bounds: FieldBounds
return null;
}
const lines: Point[][] = [];
const lineCount = Math.round(count * Math.abs(a.q));
const lineCount = roundSymmetric(count * Math.abs(a.q));
for (const charge of [a, b]) {
const orientation = charge === a ? 1 : -1;
const inward = { x: (orientation * dx) / separation, y: (orientation * dy) / separation };
Expand Down Expand Up @@ -311,7 +312,7 @@ export function automaticFieldLines(
const arrivals = sinks.map(() => 0);

for (const sourceCharge of charges.filter((charge) => charge.q > 0)) {
const lineCount = Math.round(count * sourceCharge.q);
const lineCount = roundSymmetric(count * sourceCharge.q);
for (let i = 0; i < lineCount; i++) {
const angle = (2 * Math.PI * i) / lineCount;
const seed = {
Expand All @@ -331,7 +332,7 @@ export function automaticFieldLines(
}

for (const [sinkIndex, charge] of sinks.entries()) {
const lineCount = Math.round(count * -charge.q);
const lineCount = roundSymmetric(count * -charge.q);
const needed = Math.max(0, lineCount - (arrivals[sinkIndex] ?? 0));
if (needed === 0) {
continue;
Expand Down
15 changes: 9 additions & 6 deletions src/explore/view/ExploreScreenSummaryContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ export class ExploreScreenSummaryContent extends ScreenSummaryContent {
states.valuesShownStringProperty,
states.valuesHiddenStringProperty,
);
const fieldLineCount = new DerivedProperty([model.changeCountProperty], () => model.seedPoints.length);
const equipotentialCount = new DerivedProperty([model.changeCountProperty], () => model.equipotentialSeeds.length);
const fieldLineCountProperty = new DerivedProperty([model.changeCountProperty], () => model.seedPoints.length);
const equipotentialCountProperty = new DerivedProperty(
[model.changeCountProperty],
() => model.equipotentialSeeds.length,
);
const details = new PatternStringProperty(a11y.currentDetailsStringProperty, {
chargeCount: model.charges.lengthProperty,
sensorCount: model.sensors.lengthProperty,
voltmeterState,
measuringTapeState,
voltageMapState,
valuesState,
fieldLineCount,
equipotentialCount,
fieldLineCount: fieldLineCountProperty,
equipotentialCount: equipotentialCountProperty,
});
super({
playAreaContent: a11y.screenSummary.playAreaStringProperty,
Expand All @@ -61,8 +64,8 @@ export class ExploreScreenSummaryContent extends ScreenSummaryContent {
measuringTapeState,
voltageMapState,
valuesState,
fieldLineCount,
equipotentialCount,
fieldLineCountProperty,
equipotentialCountProperty,
]) {
property.dispose();
}
Expand Down
6 changes: 4 additions & 2 deletions src/explore/view/FieldLinesInfoDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export class FieldLinesInfoDialog extends Dialog {
fill: ElectricFieldMapperColors.textColorProperty,
maxWidth: INFO_DIALOG_LINE_WRAP,
});
const spokenBody = new DerivedProperty([ui.infoBodyStringProperty], (markup) => markup.replace(/<\/?b>/g, ""));
const spokenBodyProperty = new DerivedProperty([ui.infoBodyStringProperty], (markup) =>
markup.replace(/<\/?b>/g, ""),
);
const body = new RichText(ui.infoBodyStringProperty, {
font: new PhetFont(FIELD_LINES_INFO_DIALOG_SETTINGS.BODY_FONT_SIZE),
fill: ElectricFieldMapperColors.textColorProperty,
lineWrap: INFO_DIALOG_LINE_WRAP,
accessibleParagraph: spokenBody,
accessibleParagraph: spokenBodyProperty,
});
super(body, {
title,
Expand Down
3 changes: 2 additions & 1 deletion src/explore/view/potentialColor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { roundSymmetric } from "scenerystack/dot";
import type { Color } from "scenerystack/scenery";
import ElectricFieldMapperColors from "../../ElectricFieldMapperColors.js";
import { POTENTIAL_SATURATION, potentialColorFraction } from "../model/FieldPhysics.js";
Expand Down Expand Up @@ -29,5 +30,5 @@ export function potentialCSS(potential: number, alpha = 1, saturation = POTENTIA
ElectricFieldMapperColors.potentialNegativeColorProperty.value,
saturation,
);
return `rgba(${Math.round(r)},${Math.round(g)},${Math.round(b)},${alpha})`;
return `rgba(${roundSymmetric(r)},${roundSymmetric(g)},${roundSymmetric(b)},${alpha})`;
}
Loading
Loading