p5.plotsvg
Version:
A Plotter-Oriented SVG Exporter for p5.js
188 lines (113 loc) • 6.41 kB
Markdown
<!-- Originally generated by documentation.js; later edited by hand. -->
## p5.plotSvg Documentation
### Table of Contents
* [beginRecordSVG](#beginrecordsvg)
* [pauseRecordSVG](#pauserecordsvg)
* [endRecordSVG](#endrecordsvg)
* [setSVGDocumentSize](#setsvgdocumentsize)
* [setSvgResolutionDPI](#setsvgresolutiondpi)
* [setSvgResolutionDPCM](#setsvgresolutiondpcm)
* [setSvgDefaultStrokeWeight](#setsvgdefaultstrokeweight)
* [setSvgDefaultStrokeColor](#setsvgdefaultstrokecolor)
* [setSvgBackgroundColor](#setSvgBackgroundColor)
* [setSvgIndent](#setsvgindent)
* [setSvgFlattenTransforms](#setsvgflattentransforms)
* [setSvgCoordinatePrecision](#setsvgcoordinateprecision)
* [setSvgTransformPrecision](#setsvgtransformprecision)
* [setSvgPointRadius](#setsvgpointradius)
* [beginSvgGroup](#beginsvggroup)
* [endSvgGroup](#endsvggroup)
* [getDefaultStrokeColor](#getdefaultstrokecolor)
---
## beginRecordSVG
Begins recording SVG output for a p5.js sketch.
Initializes recording state, validates and sets the output filename,
and overrides p5.js drawing functions to capture drawing commands for SVG export.
#### Parameters
* `p5Instance` **[object][26]** A reference to the current p5.js sketch (e.g. `this`).
* `fn` **[string][27]?** Optional filename for the output SVG file.
Defaults to "output.svg" if not provided or invalid.
## pauseRecordSVG
Pauses or unpauses recording of SVG output for a p5.js sketch,
depending on whether the `bPause` argument is true or false.
#### Parameters
* `bPause`
## endRecordSVG
Ends recording of SVG output for a p5.js sketch.
Calls the export function to generate the SVG output
and restores the original p5.js functions.
## setSVGDocumentSize
Sets the dimensions of the SVG document in pixels/dots.
Note that graphics are not scaled to fit this size; they may extend beyond the specified dimensions.
If this is not set, the system will default to the main canvas dimensions (i.e. from `createCanvas()`).
#### Parameters
* `w` **[number][28]** The SVG document width in pixels/dots. Must be a positive number.
* `h` **[number][28]** The SVG document height in pixels/dots. Must be a positive number.
## setSvgResolutionDPI
Sets the resolution for the exported SVG file in dots per inch (DPI).
This value is used to determine the scaling of units (pixels to physical dimensions) in the SVG output. The default is 96 dpi.
#### Parameters
* `dpi` **[number][28]** The resolution in dots per inch. Must be a positive number.
## setSvgResolutionDPCM
Sets the resolution for the exported SVG file in dots per centimeter (DPCM).
This value is used to determine the scaling of units (pixels to physical dimensions) in the SVG output. The default resolution is 243.84 dpcm (equivalent to 96 dpi).
#### Parameters
* `dpcm` **[number][28]** The resolution in dots per centimeter. Must be a positive number.
## setSvgDefaultStrokeWeight
Sets the default stroke weight for SVG elements.
#### Parameters
* `wei` **[number][28]** The stroke weight to set.
## setSvgDefaultStrokeColor
Sets the default stroke color for SVG elements.
#### Parameters
* `col` **[string][27]** The stroke color to set, in valid CSS color format.
## setSvgBackgroundColor
Sets an optional background color (as a CSS style) for the SVG. This is independent of the `background()` color of the p5 sketch.
This color does not interfere with plotter output and is purely for visualization. Note that this color may not be visible in all SVG viewers. If this function is not called, no background color style is specified in the SVG.
#### Parameters
* `col` **[string][27]** The background color to set, in valid CSS color format.
## setSvgIndent
Sets the type and amount of indentation used for formatting SVG output.
The function allows for spaces, tabs, or no indentation.
#### Parameters
* `itype` **[string][27]** The type of indentation to use. Valid values are
'SVG\_INDENT\_SPACES', 'SVG\_INDENT\_TABS', or 'SVG\_INDENT\_NONE'.
* `inum` **[number][28]?** Optional number of spaces or tabs to use for indentation.
Must be a non-negative integer if provided. Defaults to 2 for spaces and 1 for tabs.
## setSvgFlattenTransforms
Set whether or not to use a stack to encode matrix transforms.
* `setSvgFlattenTransforms(true)` -- larger SVG files, greater fidelity to original
* `setSvgFlattenTransforms(false)` -- smaller SVG files, potentially less fidelity
#### Parameters
* `b`
## setSvgCoordinatePrecision
Sets the output precision for graphics coordinates in SVGs by adjusting
the number of decimal digits used when formatting values. Default is 4 digits.
#### Parameters
* `p` **[number][28]** The desired number of decimal digits for coordinates.
Must be a non-negative integer. If an invalid value is provided, a warning is issued.
## setSvgTransformPrecision
Sets the output precision for matrix-transform values in SVGs by adjusting
the number of decimal digits used when formatting rotations, translations, etc. Default is 6 digits.
#### Parameters
* `p` **[number][28]** The desired number of decimal digits for matrix values.
Must be a non-negative integer. If an invalid value is provided, a warning is issued.
## setSvgPointRadius
Sets the radius for points (rendered as tiny circles) in the SVG output. Default is 0.25 pixels.
#### Parameters
* `radius` **[number][28]** The desired radius for points, specified as a positive number.
If an invalid value (non-positive or non-number) is provided, a warning is issued.
## beginSvgGroup
Begins a new user-defined grouping of SVG elements.
Optionally associates a group name to the SVG group.
Be sure to call `endSvgGroup()` later or the SVG file will report errors.
#### Parameters
* `gname` **[string][27]?** Optional group name used as an ID for the SVG group.
## endSvgGroup
Ends the current user-defined group of SVG elements.
## getDefaultStrokeColor
Retrieves the default stroke color used for SVG rendering.
Returns **[string][27]** The default stroke color (in hex, RGB, or named CSS color format).
[26]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[27]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[28]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number