OMD Documentation

main.js - OMD AI Visual Generator

This main.js file powers the OMD AI Visual Generator, an interactive web page that uses AI to create mathematical visualizations from natural language prompts.

Core Functionality: The OMDVisualGenerator Class

The entire application is encapsulated within the OMDVisualGenerator class, which manages the UI, handles user input, and orchestrates the process of generating and rendering visuals.

Initialization

When the DOM is loaded, a new OMDVisualGenerator instance is created. The constructor calls:

Event Listeners

The application responds to the following user actions:

Visual Generation Process

The core workflow is handled by the generateVisual() method:

  1. Get Input: Retrieves the user's natural language request from the input field.
  2. Set Loading State: Disables the UI to prevent multiple submissions while a request is in progress.
  3. API Call: Sends a fetch request to the Netlify serverless function at /.netlify/functions/ai-omd-lookup. The user's request is passed as a URL parameter.
  4. Receive JSON: The serverless function (not detailed in this file) communicates with an AI service and returns a JSON object that conforms to the OMD specification for a particular visual (e.g., a balanceHanger or tapeDiagram).
  5. Display JSON: The raw JSON response is pretty-printed and displayed in the UI for inspection.
  6. Render Visual: The renderVisual(jsonData) method is called.

Rendering the Visual (renderVisual)

This method is responsible for turning the AI-generated JSON into an SVG image:

  1. Clear Previous Visual: Removes any existing SVG from the display area.
  2. Select OMD Class: A switch statement reads the omdType property from the JSON to determine which OMD class to instantiate (e.g., omdBalanceHanger, omdTable).
  3. Load Data: The loadFromJSON(jsonData) method of the instantiated OMD object is called, configuring the object based on the AI-provided data.
  4. Create SVG: An SVG wrapper is created, and the OMD object's internal SVG group (omdObject.svgObject) is appended to it.
  5. Display SVG: The final SVG is added to the visualResult panel in the DOM.

Manual JSON Input

For testing and debugging, users can click the "Manual JSON Input" button to reveal a text area. They can paste their own OMD-compliant JSON and click "Render" to see it visualized, skipping the AI step entirely.

Utility Methods

Usage

This script is designed to be used with an index.html file that provides the necessary UI layout. It allows users to:

↑ Top