Skip to content

Latest commit

 

History

History
289 lines (181 loc) · 11.1 KB

File metadata and controls

289 lines (181 loc) · 11.1 KB

p5.plotSvg Documentation

Table of Contents


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 A reference to the current p5.js sketch (e.g. this).
  • fn string? Optional filename for the output SVG file. The explicit use of null will prevent a file from being saved. Behavior:
    • beginRecordSvg(this, "file.svg"); // saves to "file.svg"
    • beginRecordSvg(this); // saves to "output.svg" (default)
    • beginRecordSvg(this, null); // DOES NOT save any file!

pauseRecordSvg

Pauses or unpauses recording of SVG output for a p5.js sketch, depending on whether the boolean bPause argument is true or false.

Parameters

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. Returns the complete text of the SVG file as a string.

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 The SVG document width in pixels/dots. Must be a positive number.
  • h number 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 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 approximately 37.79527559 dpcm (equivalent to 96 dpi).

Parameters

  • dpcm number The resolution in dots per centimeter. Must be a positive number.

setSvgInkscapeCompatibility

Enables or disables Inkscape-compatible layer attributes for SVG groups. When enabled (which is the default), Inkscape headers will be added to the SVG, and SVG groups created with beginSvgGroup() will include inkscape:groupmode="layer" and auto-numbered inkscape:label attributes. Disabling this mode produces 'vanilla' SVG files.

Parameters

  • bEnabled boolean Enable or disable Inkscape layer compatibility. The default is true: groups will include Inkscape layer attributes, and the SVG file will have Inkscape hints in its headers.

setSvgDefaultStrokeWeight

Sets the default stroke weight for SVG elements.

Parameters

  • wei number The stroke weight to set.

setSvgDefaultStrokeColor

Sets the default stroke color for SVG elements.

Parameters

  • col string 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 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 The type of indentation to use. Valid values are 'SVG_INDENT_SPACES', 'SVG_INDENT_TABS', or 'SVG_INDENT_NONE'.
  • inum number? 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 boolean Whether or not to flatten geometric transforms

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 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 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" (which are rendered as tiny circles) in the SVG output. Default is 0.25 pixels.

Parameters

  • radius number 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.

setSvgGroupByStrokeColor

Sets whether or not to group SVG elements by stroke color. When true, elements with the same stroke color, at the same level, will be grouped together.

Parameters

  • bEnabled boolean Enable or disables grouping of elements by stroke color. The default is false.

setSvgMergeNamedGroups

Sets whether or not to merge user-defined SVG groups that have the same name. Useful for grouping paths that might be computed at different times, but which are part of the same compositional design element, and should be plotted with the same drawing tool. The default is true, meaning that groups with the same name (which are at the same hierarchical level) will be merged. Note: There may be unexpected groupings if setSvgGroupByStrokeColor() is also set to true.

Parameters

  • bEnabled boolean Whether or not groups with the same name should be merged.

setSvgExportPolylinesAsPaths

Sets whether all polylines should be exported as <path> elements instead of the default case (which is <polyline> or <polygon>).

Parameters

  • b boolean true to export polylines as <path> elements; false to keep the default behavior.

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? 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, the default stroke color (in hex, RGB, or named CSS color format).

isRecordingSVG

Retrieves whether or not SVG recording is active. Returns boolean true if SVG recording is active, false otherwise.