Skip to content

Add electromagnet to circuit diagram tools#14

Open
ghostleek wants to merge 1 commit into
mainfrom
feat/electromag
Open

Add electromagnet to circuit diagram tools#14
ghostleek wants to merge 1 commit into
mainfrom
feat/electromag

Conversation

@ghostleek
Copy link
Copy Markdown
Contributor

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.html and object_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:

  • Integrated electromagnet support into the tool selection, placement, and undo/redo workflows for both diagram and object tools. [1] [2]

Documentation and Bug Log Updates:

  • Updated dev.md to document the re-integration of the electromagnet, including a bugfix log entry describing the new symbol and its compliance with exam standards. [1] [2]

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.
@ghostleek ghostleek requested a review from Copilot April 9, 2026 00:28
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
diagrams Ready Ready Preview, Comment Apr 9, 2026 0:28am

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.md changelog/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(), but autoAlignComponent() 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();

Comment on lines 494 to 499
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)) })
};
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 449 to 454
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
};
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants