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
- Use lm2d when application logic should remain Java and TeaVM should generate the browser JavaScript.
- Use SVG when shapes should remain inspectable DOM, groups need transforms, or individual elements need browser event listeners.
- Use Canvas for immediate drawing, many frame updates, native gradients, compositing, images, or pixel operations.
- Use BarChart, LineChart, ScatterPlot, and PieChart for compact fixed-layout visualizations with generated axes and labels.
- Use Lm2dHost when an annotated LMCode handler should supply bar-chart data through typed Java host functions.
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.
| Input | Output or behavior |
|---|---|
| SvgGraphics container, width, height | One namespaced SVG root appended to the container |
| CanvasGraphics canvasElement | Immediate drawing through the canvas 2D context |
| BarChart title and nonempty bars | Categorical bars with numeric ticks and a zero baseline |
| LineChart or ScatterPlot title and nonempty series | Shared linear domains, axes, markers, and legend |
| PieChart title and slices with positive total | Normalized arc wedges, percentages, and legend |
| LMCode string, float, string bar arguments | A 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
- Do not add D3, Three.js, a Canvas wrapper, or another JavaScript drawing runtime. Call the TeaVM JSO browser APIs.
- Do not use innerHTML to construct SVG. Use namespaced SVG elements through SvgGraphics.
- Validate finite coordinates, nonempty chart data, non-negative pie values, and positive dimensions before drawing.
- Clear a retained SVG scene or redraw a Canvas frame deliberately. Canvas does not retain shapes for later event hit testing.
- Convert pointer CSS coordinates to logical scene coordinates when canvas size and display size differ.
- Stage LMCode output in a temporary consumer result. Replace visible output only after Program.call returns successfully and the host reports use.
- Treat lm2d_use as a single terminal commit. Empty or repeated use is an error.
- Keep text alternatives or a data table in the host application when chart accessibility requires them.
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
- Run python3 scripts/generate-book.py and confirm no unexpected source diff.
- Run mvn clean verify and require all 21 JVM tests to pass.
- Run npm ci, install Chromium, and run npm test. Require the cover and all eight chapters to pass with zero browser errors.
- Inspect SVG elements and Canvas pixels, not only screenshots or JavaScript compilation.
- Click pointer and LMCode controls, pause and resume animation, and confirm failed LMCode leaves the previous chart unchanged.
- Check narrow mobile layout and ensure no third-party runtime requests occur.
- For deployment, rerun the same browser suite with DEMO_URL pointing to the public demo.html page.