Author an lm2d application

Use this skill to create a Java and TeaVM application with SVG, Canvas, charts, plots, or LMCode-authored chart data. Start from the public lm2d repository and verify real browser output rather than only compilation.

Purpose

lm2d supplies a small shared drawing API and two browser backends. SvgGraphics creates retained SVG DOM. CanvasGraphics draws immediately through HTML Canvas. Chart primitives use the shared API, so their data and layout do not depend on the chosen backend.

The repository also contains a nine-page demo book. Its cover is demo.html and each feature is a demo-topic.html page. Use page links for feature selection; do not replace the book with a pull-down.

When to use it

Install

Install Java 21, Maven, and Node 24.15 or newer. Clone both repositories, install the pinned runtime, and select the exact published compiler.

git clone https://github.com/lmctl-ai/lm2d.git
git clone https://github.com/lmctl-ai/lmcode-java.git
(cd lmcode-java && git checkout b88d3401ee10210a0a6583991d6a81427d5f112a && mvn -DskipTests install)
cd lm2d
npm install --prefix compiler @lmctl-ai/lmctl@0.2.482
export LMCODE_COMPILER_MODULE="$PWD/compiler/node_modules/@lmctl-ai/lmctl/dist/dsl.js"
mvn clean package

The Maven artifact is com.lmctl:lm2d version 0.1.0 after mvn install. TeaVM is 0.16.0. No npm drawing or chart runtime is used.

Commands

Build and run JVM tests with Maven. Run browser integration separately after installing Playwright Chromium.

mvn clean verify
npm ci
npx playwright install --with-deps chromium
npm test

Regenerate chapter HTML after editing scripts/generate-book.py, then inspect the resulting diff.

python3 scripts/generate-book.py
git diff --check

Generate into another static directory with an absolute demo.output path.

mvn -Ddemo.output=/absolute/site/path clean package

To run browser tests against a deployed book, set DEMO_URL to its demo.html URL. PLAYWRIGHT_CHROMIUM_EXECUTABLE selects an existing Chromium binary.

Inputs and outputs

Graphics2D methods use logical double coordinates. Paint contains fill, stroke, non-negative stroke width, and opacity from zero to one. TextStyle contains color, positive size, alignment, baseline, and font weight. Point2D rejects non-finite coordinates.

InputOutput or behavior
SvgGraphics container, width, heightOne namespaced SVG root appended to the container
CanvasGraphics canvasElementImmediate drawing through the canvas 2D context
BarChart title and nonempty barsCategorical bars with numeric ticks and a zero baseline
LineChart or ScatterPlot title and nonempty seriesShared linear domains, axes, markers, and legend
PieChart title and slices with positive totalNormalized arc wedges, percentages, and legend
LMCode string, float, string bar argumentsA validated Java Bar record staged for commit

Path2D supports moveTo, lineTo, quadraticCurveTo, bezierCurveTo, arc, and close. SvgGraphics.beginGroup and endGroup manage retained transforms. CanvasGraphics.setSize takes logical width, logical height, and positive device pixel ratio. Access native JSO objects through SvgGraphics.element and CanvasGraphics.context.

Safety rules

Worked examples

Create a retained SVG path and a circle. Paint.stroke produces no fill; withStroke adds an outline to an existing fill.

SvgGraphics svg = new SvgGraphics(container, 960, 540);
svg.beginGroup("translate(480 270)");
svg.circle(0, 0, 48, Paint.fill("#ffcb6b"));
svg.path(new Path2D().moveTo(-120, 80)
    .bezierCurveTo(-60, -100, 60, -100, 120, 80),
    Paint.stroke("#61d6c5", 4));
svg.endGroup();

Build a chart with ordinary Java values and render it to either backend.

List<BarChart.Bar> bars = List.of(
    new BarChart.Bar("Mon", 34, "#61d6c5"),
    new BarChart.Bar("Tue", 52, "#ffcb6b"));
BarChart chart = new BarChart("Weekly activity", bars);
chart.render(svg);

Provide LMCode chart data through the published schema. Register Lm2dHost before Program.load, resolve the entry by its annotation, call it with no arguments, and require host.used before replacing the chart.

@release
fn releaseChart() -> void implements ChartHandler {
  lm2d_clear();
  lm2d_bar("Core", 42.0, "#61d6c5");
  lm2d_bar("SVG", 68.0, "#ffcb6b");
  lm2d_use();
}

For animation, store previousTimestamp and elapsed in Java, cap large frame deltas, redraw, and schedule the next requestAnimationFrame. Pause by skipping state updates and drawing while continuing or cancelling the callback schedule.

Errors and exit codes

Java constructors and drawing methods throw IllegalArgumentException for invalid values. SvgGraphics.endGroup throws IllegalStateException when no group is open. LMCode missing annotations and missing lm2d_use throw IllegalStateException. The demo catches handler failures and keeps the previous chart visible.

Maven returns nonzero for LMCode compiler diagnostics, Java compilation, TeaVM compilation, or JVM test failures. npm test returns nonzero on browser errors, HTTP errors, unexpected requests, incorrect DOM or pixels, interaction failures, and mobile overflow. If LMCODE_COMPILER_MODULE is missing, the compiler bridge exits with an explicit error. A missing local lmcode-java artifact requires the pinned install step.

How to verify

  1. Run python3 scripts/generate-book.py and confirm no unexpected source diff.
  2. Run mvn clean verify and require all 21 JVM tests to pass.
  3. Run npm ci, install Chromium, and run npm test. Require the cover and all eight chapters to pass with zero browser errors.
  4. Inspect SVG elements and Canvas pixels, not only screenshots or JavaScript compilation.
  5. Click pointer and LMCode controls, pause and resume animation, and confirm failed LMCode leaves the previous chart unchanged.
  6. Check narrow mobile layout and ensure no third-party runtime requests occur.
  7. For deployment, rerun the same browser suite with DEMO_URL pointing to the public demo.html page.