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

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

ClassPurpose
Graphics2DBackend-neutral rectangles, circles, lines, paths, text, dimensions, and clearing.
SvgGraphicsRetained SVG DOM, transformed groups, redraw, and root Element access.
CanvasGraphicsImmediate Canvas rendering, pixel-ratio sizing, and native context access.
Path2DMove, line, quadratic and cubic curves, arcs, and close commands for either backend.
Paint and TextStyleValidated fill, stroke, opacity, font, alignment, and weight values.
LinearScale and BandScaleNumeric 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.

PrimitiveInputGenerated output
BarChartBar label, value, and color recordsBand positions, zero baseline, bars, ticks, and labels
LineChartNamed Series of Point2D valuesShared axes, paths, point markers, and legend
ScatterPlotNamed Series of Point2D valuesShared axes, circles, opacity, and legend
PieChartNon-negative Slice valuesArc 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.

  1. SVG drawing: retained shapes, groups, curves, and palette redraw.
  2. Canvas drawing: native gradients, paths, arcs, and pointer marks.
  3. Bar charts: six categories, scales, grid, and zero baseline.
  4. Line charts: two series and twelve points.
  5. Scatter plots: fifty-six samples rendered to Canvas.
  6. Pie charts: four arc wedges and percentage labels.
  7. Animation and events: requestAnimationFrame, pointer tracking, pause, and keyboard resume.
  8. 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.