gcode-preview for LiaScript: Visualize G-Code for 3D Printing and CNC in Open Courses

gcode-preview for LiaScript: Visualize G-Code for 3D Printing and CNC in Open Courses

G-Code is the language of digital fabrication. Every 3D printer, CNC milling machine, laser cutter, and lathe is controlled by sequences of G-Code instructions — yet most learners never get to see what those instructions actually describe spatially.

The gcode-preview template changes that. It integrates the gcode-preview npm package directly into LiaScript courses, so learners can visualize G-Code paths, layer by layer, in the browser — with no server, no software to install, and no file uploads.


What is G-Code?

G-Code (also called RS-274) is the dominant programming language for numerically controlled machine tools. A G-Code program is a sequence of commands that describe how a tool should move through space: which coordinates to reach, at what speed, whether to extrude material or only travel, and how to handle layers.

A minimal example:

G0 X0 Y0 Z0.2    ; rapid move to start position
G1 X42 Y42 E10   ; extrude while moving to (42,42)
G0 X100 Y100 Z20.2
G1 X42 Y42 E10

In 3D printing education, CNC training, and maker contexts, understanding the connection between the G-Code text and the resulting physical path is a core competency — and exactly what this template makes visible and interactive.


Quick Start

Add a single import line to your LiaScript course header:

<!--
import: https://raw.githubusercontent.com/LiaTemplates/gcode-preview/main/README.md
-->

All macros are now available in your document.


Visualizing G-Code: @GCODE.preview

The simplest macro adds a 3D viewer directly below a code block. Add @GCODE.preview to the opening fence of any gcode block:

``` gcode @GCODE.preview
G0 X0 Y0 Z0.2
G1 X42 Y42 E10
G0 X100 Y100 Z20.2
G1 X42 Y42 E10
```

The viewer renders the toolpath as a 3D scene — extrusion moves in one color, travel moves in another. You can rotate and zoom with the mouse.

Try it live:


Customizing the Viewer: @GCODE.previewWithParams

For full control over the visualization, use @GCODE.previewWithParams. You can set colors, line widths, render modes, and more:

``` gcode @GCODE.previewWithParams(style="width: 100%; height: 300px;" backgroundColor="#1a1a2e" renderTravel="true" renderTubes="true" extrusionColor="#e94560" travelColor="#0f3460" lineWidth="3")
G0 X0 Y0 Z0.2
G1 X42 Y42 E10
G0 X100 Y100 Z20.2
G1 X42 Y42 E10
```

Live Editing: @GCODE.eval and @GCODE.evalWithParams

The @GCODE.eval and @GCODE.evalWithParams macros go at the end of the code block (not in the fence opener). Learners can edit the G-Code, run it, and immediately see the result — the output panel is resizable.

``` gcode
G0 X0 Y0 Z0.2
G1 X42 Y42 E10
```
@GCODE.evalWithParams(extrusionColor="green" backgroundColor="#222" style="width: 100%;" animate="false")

This is particularly powerful in a teaching context: students modify a single coordinate, re-run, and observe exactly how the toolpath changes — a tight feedback loop that static diagrams cannot provide.


Loading External G-Code Files: @GCODE.load

Real G-Code files from slicers and CAM software can be large — sometimes thousands of lines. The @GCODE.load macros let you link to any .gcode file (from your course repository or an absolute URL) without embedding it inline:

@[GCODE.load.preview](./example/eiffel-tower.gcode)

@[GCODE.load.eval](./example/eiffel-tower.gcode)

The template repo includes an Eiffel Tower example — a complex multi-layer print that dramatically shows the layered visualization:


Supported Parameters

The viewer accepts a rich set of configuration options, all passable via @GCODE.previewWithParams or @GCODE.evalWithParams:

ParameterTypeDescription
backgroundColorstringCanvas background color (CSS color or hex)
extrusionColorstringColor for extrusion moves
travelColorstringColor for travel (non-extrusion) moves
topLayerColorstringHighlight color for the topmost layer
lastSegmentColorstringColor for the last printed segment
renderTravelbooleanShow travel moves (default: false)
renderTubesbooleanRender paths as 3D tubes
renderExtrusionbooleanShow extrusion moves
animatebooleanAnimate the toolpath layer by layer
lineWidthnumberWidth of rendered lines
lineHeightnumberHeight of rendered lines
startLayernumberFirst layer to display
endLayernumberLast layer to display
allowDragNDropbooleanAllow dropping a local .gcode file onto the viewer
buildVolume{ x, y, z }Show build volume bounding box
initialCameraPositionnumber[]Starting camera coordinates
toolColorsRecord<number, string>Per-tool colors for multi-material prints
stylestringCSS style string for the viewer container

Use Cases

3D printing courses — Show students the difference between infill patterns, support structures, and perimeters. Let them write a simple G-Code shape from scratch and visualize the result before ever touching a printer.

CNC machining training — Explain cutting paths, approach moves, and tool changes with interactive visualizations. The color-coded travel vs. extrusion moves map directly to air moves vs. cutting passes.

Technical education and TVET — Many vocational training programs cover CNC or additive manufacturing. gcode-preview brings the machine behavior into the classroom without requiring physical machines.

Maker spaces and workshops — Participants can paste G-Code exported from their slicer directly into an @GCODE.eval block and inspect the toolpath before printing.

CAD/CAM introductions — Compare hand-written G-Code with slicer output to build intuition for what slicers actually produce.


Technical Facts

Runs in browserYes — no server, no backend
Interactive 3D viewerYes — rotate, zoom, pan
Live editingYes, with @GCODE.eval
Load external filesYes, via @GCODE.load
Drag & dropSupported via allowDragNDrop parameter
External APINo
Offline capableAfter first load (assets cached)
LicenseUnlicense
MaintainedYes

Try It

Try in LiaScript Open in LiveEditor View on GitHub
  • MicroBit-Simulator — simulate BBC micro:bit and MicroPython programs in the browser
  • AVR8js — Arduino-compatible AVR microcontroller simulator
  • JSXGraph — interactive mathematical visualizations and geometry
  • DigiSim — digital logic circuit simulator
  • jscad — 3D CAD modeling with JSCAD in the browser

Related Posts

AlaSQL for LiaScript: Teach SQL and Query CSV Data in the Browser

Use the AlaSQL template to run SQL queries directly inside LiaScript courses — with support for CSV data, JSON, and JavaScript arrays. No server, no setup.

Read More

Mermaid for LiaScript: Create Flowcharts and Diagrams in Plain Text

Use the Mermaid template to draw flowcharts, sequence diagrams, Gantt charts, ER diagrams, and more in plain text — directly inside your LiaScript courses, no server needed.

Read More

MicroBit-Simulator for LiaScript: Teach MicroPython Without a Physical Device

Simulate a BBC micro:bit and run MicroPython code directly inside your LiaScript course — no hardware, no server, just a browser.

Read More