Static Export (SVG, PNG, PDF)
JSXGraph.jl supports exporting boards as static images via save() or the direct export functions save_svg(), save_png(), save_pdf().
Requirements
Static export is provided via a package extension that depends on NodeJS_22_jll. Node.js v22 is automatically downloaded as a Julia artifact — no system installation needed. On first export, required npm packages are installed in a package-local directory (.node_deps/).
| Format | npm packages (auto-installed) |
|---|---|
| SVG | jsdom |
| PNG | jsdom, sharp |
jsdom, pdfkit, svg-to-pdfkit |
using Pkg
Pkg.add("NodeJS_22_jll")Usage
Export via save()
Load NodeJS_22_jll, then the save() function dispatches on the file extension:
using JSXGraph
using NodeJS_22_jll # activates static export extension
b = board("myboard", xlim=(-5, 5), ylim=(-5, 5)) do b
push!(b, point(1, 2; name="A"))
push!(b, circle(point(0, 0), 3; strokeColor="blue"))
end
# HTML export (always available)
save("plot.html", b)
# SVG export
save("plot.svg", b)
# PNG export
save("plot.png", b)
# PNG with 2× resolution (Retina/HiDPI)
save("plot_hd.png", b; scale=2)
# PDF export
save("plot.pdf", b)Direct export functions
save_svg("plot.svg", b)
save_png("plot.png", b)
save_png("plot_hd.png", b; scale=2)
save_pdf("plot.pdf", b)Supported Formats
| Extension | Format | Backend |
|---|---|---|
.html | Self-contained HTML page | Built-in |
.svg | Scalable Vector Graphics | NodeJS22jll + jsdom |
.png | Portable Network Graphics | NodeJS22jll + jsdom + sharp |
.pdf | PDF document | NodeJS22jll + jsdom + pdfkit + svg-to-pdfkit |
Unsupported extensions (e.g. .gif, .bmp) raise an ErrorException.
Scale Factor (PNG)
The scale keyword controls the output resolution for PNG exports:
save("plot.png", board; scale=1) # native resolution (e.g. 500×500)
save("plot.png", board; scale=2) # 2× resolution (e.g. 1000×1000)
save("plot.png", board; scale=3) # 3× resolution (e.g. 1500×1500)Higher scale values produce sharper images suitable for print or HiDPI displays.
API Reference
JSXGraph.save_svg — Function
save_svg(filename::String, board::Board)Export a board as a static SVG image file.
Requires NodeJS_22_jll to be installed. Add it with:
using Pkg; Pkg.add("NodeJS_22_jll")Then load it before calling save_svg:
using NodeJS_22_jll
save("plot.svg", board)Arguments
filename::String: Output file path (should end in.svg)board::Board: The board to export
JSXGraph.save_png — Function
save_png(filename::String, board::Board; scale::Int=1)Export a board as a PNG image file.
The scale parameter controls the resolution multiplier (e.g. scale=2 produces a 2× resolution image suitable for Retina/HiDPI displays).
Requires NodeJS_22_jll to be installed. Add it with:
using Pkg; Pkg.add("NodeJS_22_jll")Then load it before calling save_png:
using NodeJS_22_jll
save("plot.png", board)
save("plot_hd.png", board; scale=2) # 2× resolutionArguments
filename::String: Output file path (should end in.png)board::Board: The board to exportscale::Int=1: Resolution multiplier (1 = native, 2 = 2× Retina)
JSXGraph.save_pdf — Function
save_pdf(filename::String, board::Board)Export a board as a PDF document.
Requires NodeJS_22_jll to be installed. Add it with:
using Pkg; Pkg.add("NodeJS_22_jll")Then load it before calling save_pdf:
using NodeJS_22_jll
save("plot.pdf", board)Arguments
filename::String: Output file path (should end in.pdf)board::Board: The board to export
How It Works
- A Node.js script is generated that creates a virtual DOM using jsdom
- The JSXGraph library is injected into the virtual DOM
- The board is initialized with all elements
- The SVG element is extracted from the DOM
- For SVG: the raw SVG is written with proper XML headers
- For PNG:
sharpconverts the SVG to a raster image at the specified scale - For PDF:
pdfkit+svg-to-pdfkitembed the SVG in a PDF document
Node.js is provided by NodeJS_22_jll (Julia artifact, no system dependency).
Troubleshooting
"SVG/PNG/PDF export requires the NodeJS22jll package": Load it first:
using NodeJS_22_jllnpm package installation fails: Try installing manually:
cd ~/.julia/packages/JSXGraph/<hash>/.node_deps
~/.julia/artifacts/<nodejs_hash>/bin/npm install jsdom sharp pdfkit svg-to-pdfkit