Add electromagnet to circuit diagram tools#14
Conversation
Re-integrate an electromagnet component into both circuit tools and record the change in dev.md. Adds UI controls (tool buttons + coil-count inputs, 1–6, default 2) and shows/hides the coil field when the electromagnet tool is active. Updates sizing, node/connection logic, component serialization (coils property), and input handling so electromagnets can be placed like other components. Implements drawElectromagnet / drawElectromagnetObject to render the hatched steel rod above stacked rectangular coil/cell elements with horizontal wire leads and proper draw order. Notes the dev log entry describing the re-integration and why the first attempt was replaced with the exam-compliant symbol.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Re-integrates an electromagnet component into both circuit diagram tools (symbol + object), including UI controls for coil count and updated rendering/geometry to match exam symbol conventions.
Changes:
- Added an Electromagnet tool button to both tools, with coil-count input (1–6).
- Implemented electromagnet sizing, node/bounds logic, and SVG rendering (hatched rod + stacked cells + wire leads).
- Updated
dev.mdchangelog/bug log to reflect the electromagnet re-integration.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| public/tools/object_circuit.html | Adds electromagnet tool UI, coil parameter storage, geometry sizing, and SVG drawing for object-style electromagnet. |
| public/tools/circuit_diagram_creator.html | Adds electromagnet tool UI, coil parameter storage, sizing/bounds updates, and SVG drawing for symbol-style electromagnet. |
| dev.md | Documents the electromagnet re-integration and updates bug log entry B003 to Fixed. |
Comments suppressed due to low confidence (1)
public/tools/object_circuit.html:576
- Electromagnet placement now goes through
addComponent()+autoAlignComponent(), butautoAlignComponent()does not currently have any logic for electromagnets. As a result, when placing near an existing vertical wire the electromagnet won’t auto-rotate to match the wire direction (unlike other 2-terminal parts), which can make connections harder to snap. Consider adding an electromagnet case that aligns like other straight-through components (0°/90° based on nearby wire directions).
if (['bulb', 'battery', 'switch-open', 'switch-closed', 'electromagnet'].includes(state.tool)) {
const type = state.tool.startsWith('switch') ? 'switch' : state.tool;
addComponent(type, p);
const comp = state.components[state.components.length - 1];
if (type === 'switch') comp.variant = state.tool;
render();
| x: p.x, | ||
| y: p.y, | ||
| rotation: state.rotation, | ||
| variant: type | ||
| variant: type, | ||
| ...(type === 'electromagnet' && { coils: Math.max(1, Math.min(6, parseInt(ui.electromagnetCoils.value) || 2)) }) | ||
| }; |
There was a problem hiding this comment.
Coil parsing uses parseInt(value) || 2, which treats 0 as falsy and silently changes it to 2 (instead of clamping to the min of 1). This makes invalid inputs behave unexpectedly. Consider parsing to a number first (with an explicit radix) and only falling back to the default when the result is NaN/non-finite, then clamp to 1–6.
| y: p.y, | ||
| rotation: state.rotation, | ||
| variant: type, | ||
| cells: type === 'battery' ? Math.max(1, Math.min(8, Number(ui.batteryCells.value) || 2)) : undefined | ||
| cells: type === 'battery' ? Math.max(1, Math.min(8, Number(ui.batteryCells.value) || 2)) : undefined, | ||
| coils: type === 'electromagnet' ? Math.max(1, Math.min(6, Number(ui.electromagnetCoils.value) || 2)) : undefined | ||
| }; |
There was a problem hiding this comment.
Coil parsing uses Number(value) || 2, which treats 0 as falsy and changes it to 2 (instead of clamping to the min of 1). If a user types 0 (or clears then types), this yields surprising results. Prefer falling back to the default only when the parsed value is NaN/non-finite (e.g., via Number.isFinite) and then clamp to 1–6.
This pull request re-integrates the electromagnet component into both the circuit diagram and object circuit tools, following the required exam symbol conventions. The update introduces a configurable electromagnet symbol with a hatched steel rod and stacked cell rectangles, supporting 1–6 coils, and ensures consistent rendering and placement logic across both tools.
Electromagnet Re-integration and Symbol Design:
Added the electromagnet tool to both
circuit_diagram_creator.htmlandobject_circuit.html, allowing users to place electromagnets in their circuits. The symbol now matches exam requirements: a hatched steel rod above stacked cell rectangles, with 1–6 configurable coils. [1] [2] [3] [4]Implemented new UI controls in both tools for setting the number of electromagnet coils, and updated component creation logic to store and use this parameter. [1] [2] [3] [4]
Rendering and Placement Logic:
Updated the component sizing, placement, and connection logic to support the electromagnet's variable coil count and unique geometry in both tools. [1] [2] [3] [4] [5] [6] [7]
Added rendering functions for the electromagnet symbol in both SVG renderers, including detailed drawing of the rod, hatching, coil cells, and wire leads. [1] [2] [3] [4]
Tool and Placement Workflow:
Documentation and Bug Log Updates:
dev.mdto document the re-integration of the electromagnet, including a bugfix log entry describing the new symbol and its compliance with exam standards. [1] [2]