lm2d
lm2d is a Java and TeaVM library for SVG, HTML Canvas, charts, and plots. It calls browser APIs directly, without a JavaScript drawing or chart library. This page is a compact reference for building an application from the public repository.
Start here
- Open the eight-chapter demo book.
- Read the authoring and verification skill.
- Clone the MIT-licensed source.
- Read the LMCode chart handlers and their typed schema.
Build
Use Java 21, Maven, and Node 24.15 or newer. The pinned LMCode runtime commit and compiler version make the generated application reproducible.
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 static book appears under target/demo. Set demo.output to an absolute path to generate it directly into another static site.
Core API
| Class | Purpose |
|---|---|
| Graphics2D | Backend-neutral rectangles, circles, lines, paths, text, dimensions, and clearing. |
| SvgGraphics | Retained SVG DOM, transformed groups, redraw, and root Element access. |
| CanvasGraphics | Immediate Canvas rendering, pixel-ratio sizing, and native context access. |
| Path2D | Move, line, quadratic and cubic curves, arcs, and close commands for either backend. |
| Paint and TextStyle | Validated fill, stroke, opacity, font, alignment, and weight values. |
| LinearScale and BandScale | Numeric mapping, nice ticks, categorical bands, padding, and centers. |
Minimal drawing
SvgGraphics svg = new SvgGraphics(container, 960, 540);
svg.rectangle(10, 10, 180, 90, Paint.fill("#61d6c5"));
svg.path(new Path2D().moveTo(240, 100)
.quadraticCurveTo(330, 0, 420, 100),
Paint.stroke("#ffcb6b", 4));
CanvasGraphics canvas = new CanvasGraphics(canvasElement);
canvas.setSize(960, 540, Window.current().getDevicePixelRatio());
canvas.circle(480, 270, 80, Paint.fill("#a99cf5"));
SvgGraphics.element exposes the root SVG Element. CanvasGraphics.context exposes TeaVM's CanvasRenderingContext2D, so native gradients, compositing, images, transforms, and pixel operations remain available.
Charts and plots
All chart models render through Graphics2D. Change the graphics object to change the backend without changing the data.
| Primitive | Input | Generated output |
|---|---|---|
| BarChart | Bar label, value, and color records | Band positions, zero baseline, bars, ticks, and labels |
| LineChart | Named Series of Point2D values | Shared axes, paths, point markers, and legend |
| ScatterPlot | Named Series of Point2D values | Shared axes, circles, opacity, and legend |
| PieChart | Non-negative Slice values | Arc wedges, percentage labels, and legend |
List<BarChart.Bar> bars = List.of(
new BarChart.Bar("Mon", 34, "#61d6c5"),
new BarChart.Bar("Tue", 52, "#ffcb6b"));
new BarChart("Weekly activity", bars).render(svg);
Demo book
There is no pull-down selector. Each page selects and explains one feature, with previous and next links.
- SVG drawing: retained shapes, groups, curves, and palette redraw.
- Canvas drawing: native gradients, paths, arcs, and pointer marks.
- Bar charts: six categories, scales, grid, and zero baseline.
- Line charts: two series and twelve points.
- Scatter plots: fifty-six samples rendered to Canvas.
- Pie charts: four arc wedges and percentage labels.
- Animation and events: requestAnimationFrame, pointer tracking, pause, and keyboard resume.
- LMCode charts: annotated handlers and failure-atomic chart replacement.
LMCode host functions
LMCode sees typed functions and Java owns the chart data. Call lm2d_clear to reset staged bars, lm2d_bar with a string label, float value, and string color to append, and lm2d_use to commit a defensive copy. Empty commits and repeated commits are rejected.
@release
fn releaseChart() -> void implements ChartHandler {
lm2d_clear();
lm2d_bar("Core", 42.0, "#61d6c5");
lm2d_bar("SVG", 68.0, "#ffcb6b");
lm2d_use();
}
Limits and verification
lm2d is a focused library, not a D3 port. Charts currently have fixed margins, generated numeric ticks, no built-in animation or data loading, and no accessibility table fallback. Applications own responsive relayout and text-measurement policy.
mvn clean verify
npm ci
npx playwright install --with-deps chromium
npm test
The JVM suite has 21 tests. Browser integration visits all nine pages and checks SVG DOM, Canvas pixels, pointer events, animation, LMCode failure safety, mobile overflow, external requests, and runtime errors. GitHub Actions runs both suites from public dependencies.