Skip to content

SVG presenter โ€‹

The SVG presenter displays your rolls as scalable dice graphics, including individual values, modifier icons and totals. It is an optional package for RPG Dice Roller v6.

Standard dice artwork is included automatically. Percentile dice use the d10 shape, Fudge dice use the d6 shape with โˆ’, a blank face and +, and other face counts use a generic shape unless you provide your own artwork.

Dice results Evaluated results with arithmetic, modifiers and individual dice. Dice pool: d2; 1 result; 1 counted; total 2(d2: 2; contribution 2 2 d2)+Dice pool: d4; 1 result; 1 counted; total 4(d4: 4; contribution 4 4 d4)+Dice pool: d6; 1 result; 1 counted; total 6(d6: 6; contribution 6 6 d6)+Dice pool: d8; 1 result; 1 counted; total 8(d8: 8; contribution 8 8 d8)Dice pool: d10; 1 result; 1 counted; total 10(d10: 10; contribution 10 10 d10)+Dice pool: d12; 1 result; 1 counted; total 12(d12: 12; contribution 12 12 d12)+Dice pool: d20; 1 result; 1 counted; total 20(d20: 20; contribution 20 20 d20)+Dice pool: d3; 1 result; 1 counted; total 3(d3: 3; contribution 3 3 d3)

Install โ€‹

Add the SVG presenter to your project using your preferred package manager:

bash
npm install @dice-roller/svg-presenter

The presenter is an ES module and includes TypeScript definitions. It works in Node.js and browser applications using a build tool, without fetching artwork or configuring SVG loaders.

Display a roll โ€‹

Load the presenter using the setup that matches your application, then pass it to present():

javascript
import { Roll } from '@dice-roller/rpg-dice-roller';
import { SvgPresenter } from '@dice-roller/svg-presenter';

const roll = new Roll('2d6 + 3');
const presenter = new SvgPresenter();
const svg = roll.present(presenter);

The return value is an SVG string. To display it in a browser, insert it into a container:

html
<div id="dice-results"></div>
javascript
document.querySelector('#dice-results').innerHTML = svg;
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6 + 3Dice pool: 2d6; 2 results; 2 counted; total 10(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6)+3Total: 13

Presenting an existing roll again uses its stored results, without rolling again or changing the total.

To use SVG output each time, set the presenter in your roll options:

javascript
const roll = new Roll('2d6 + 3', { presenter: new SvgPresenter() });
const svg = roll.present();

String conversion, such as String(roll), still uses the default text presenter.

Colours and dice size โ€‹

Make the dice your own by choosing their colours and size when you create the presenter:

javascript
const presenter = new SvgPresenter({
  size: 100,
  width: 600,
  stroke: '#236b57',
  fill: '#dff3e9',
  filled: true,
});

const svg = roll.present(presenter);
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6 + 3Dice pool: 2d6; 2 results; 2 counted; total 10(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6)+3Total: 13

stroke sets the base colour for dice outlines, values, labels and maths. Set filled: true to fill the dice outlines with fill. Both colours must be six-digit hex strings, such as '#236b57'.

size sets the dice size in SVG drawing units, from 20 to 400 inclusive. Labels and spacing adjust with it. The default is 140.

Style individual dice with CSS โ€‹

When you insert the SVG inline, you can style individual dice using their classes and data-* attributes. For example, put your output inside a container with class="dice-results", then apply:

css
.dice-results [data-shape="d6"] .die-outline {
  fill: #fff0d6;
  stroke: #9b5d0a;
}

.dice-results [data-shape="d6"] .die-value {
  fill: #704200;
}
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6 + 3Dice pool: 2d6; 2 results; 2 counted; total 10(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6)+3Total: 13

The built-in artwork exposes .die-outline, .die-facets and .die-value for styling. Use data-die-type="fudge" to select a dice type, or data-shape="d6" to select its artwork. Fudge and standard six-sided dice share a shape but have different dice types.

Page CSS and DOM selectors can reach an inline SVG. They cannot reach inside an SVG displayed using an <img> element.

Width and wrapping โ€‹

width controls the drawing width, including 24 units of padding on each side. The default is 1040; the value must be finite and greater than 48. Results and notation wrap to fit.

javascript
const roll = new Roll('6d6 + 3');
const presenter = new SvgPresenter({ size: 90, width: 420 });
const svg = roll.present(presenter);
Dice results Evaluated results with arithmetic, modifiers and individual dice. 6d6 + 3Dice pool: 6d6; 6 results; 6 counted; total 22(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6+d6: 3; contribution 3 3 d6+d6: 1; contribution 1 1 d6+d6: 2; contribution 2 2 d6+d6: 6; contribution 6 6 d6)+3Total: 25

SVG drawing units are coordinates inside the image. CSS can scale the whole SVG to fit its container, but changing its CSS width does not recalculate where the results wrap. Set the presenterโ€™s width to change the layout itself.

Keep an expression on one line โ€‹

Set noWrap: true to keep each roll expression on one line. The SVG sizes itself to its contents, and width no longer constrains the layout:

javascript
const presenter = new SvgPresenter({ noWrap: true });
const svg = roll.present(presenter);
Dice results Evaluated results with arithmetic, modifiers and individual dice. 6d6 + 3Dice pool: 6d6; 6 results; 6 counted; total 22(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6+d6: 3; contribution 3 3 d6+d6: 1; contribution 1 1 d6+d6: 2; contribution 2 2 d6+d6: 6; contribution 6 6 d6)+3Total: 25

This shows the same roll as the wrapped example. On narrow screens, scroll horizontally to see the whole expression.

To give your own output the same scrolling behaviour:

css
.dice-results {
  overflow-x: auto;
}

.dice-results > svg {
  display: block;
  max-width: none;
}

Histories still place each roll separately. Descriptions, labels and modifier icons retain their own layout around the dice.

Labels, values and totals โ€‹

You can hide parts of the output independently. For example, show just the dice and maths:

javascript
const presenter = new SvgPresenter({
  size: 100,
  width: 600,
  showDiceLabels: false,
  showModifierLabels: false,
  showNotation: false,
  showTotals: false,
});
Dice results Evaluated results with arithmetic, modifiers and individual dice. Dice pool: 2d6; 2 results; 2 counted; total 10(d6: 4; contribution 4 4 +d6: 6; contribution 6 6 )+3

These visibility options all default to true:

OptionWhat it shows
showValuesValues on the dice, including Fudge symbols.
showDescriptionsPool and group descriptions alongside their results.
showDiceLabelsDie notation beneath each die.
showVariableLabelsVariable names such as $bonus and $$condition beneath their resolved values.
showModifierLabelsModifier labels beneath dice and group members, and calculated keep/drop captions beneath pools and groups.
showModifierIconsSymbols for modifier effects.
showNotationThe supplied notation above each roll.
showTotalsIndividual roll totals and the history total.

Hiding visible information does not remove the underlying results, metadata or tooltips. Dropped results remain faded and crossed out.

Modifier icons โ€‹

Modifier icons show effects such as exploding, rerolling, min/max, mapping, target successes and failures, and critical successes and failures. They represent effects on the results, rather than every modifier in the notation. The map icon is derived from the stored mapping result rather than a flag. Keep and sort do not add icons of their own.

Explode

Triggered an additional roll.

Compound

Explosion results are combined into one value.

Penetrate

Explosion rolls have one subtracted.

Matching batch explosion

The matching dice triggered another batch.

Matching batch failure

The matching batch reset the pool score to zero.

Re-roll

The original result was replaced.

Unique

A duplicate was re-rolled for uniqueness.

Minimum

Raised to the specified minimum.

Maximum

Capped at the specified maximum.

Map

Transformed by a mapping expression.

Success

Matched a success target.

Failure

Matched a failure target.

Critical success

Matched the critical success condition.

Critical failure

Matched the critical failure condition.

Dropped

Faded and crossed out; excluded from the total.

By default, icons sit at the corners of the dice:

javascript
const roll = new Roll('2d6!1 + 4d6kh3cs=6cf=2');
const presenter = new SvgPresenter();
const svg = roll.present(presenter);
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6!1 + 4d6kh3cs=6cf=2Dice pool: 2d6!1; 3 results; 3 counted; total 13(d6: 4; contribution 4 4 d6+d6: 6; contribution 6; explode 6 Explosion triggeredd6!+d6: 3; contribution 3 3 d6)+Dice pool: 4d6kh3cs=6cf=2; 4 results; 3 counted; total 12(d6: 1; contribution 0; drop 1 d6drop+d6: 2; contribution 2; critical-failure 2 Critical failured6CF+d6: 6; contribution 6; critical-success 6 Critical successd6CS+d6: 4; contribution 4 4 d6)Total: 25

Use modifierIconPosition: 'below' to move them underneath. This example shows the same stored roll:

javascript
const presenter = new SvgPresenter({
  modifierIconPosition: 'below',
});
const svg = roll.present(presenter);
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6!1 + 4d6kh3cs=6cf=2Dice pool: 2d6!1; 3 results; 3 counted; total 13(d6: 4; contribution 4 4 d6+d6: 6; contribution 6; explode 6 Explosion triggeredd6!+d6: 3; contribution 3 3 d6)+Dice pool: 4d6kh3cs=6cf=2; 4 results; 3 counted; total 12(d6: 1; contribution 0; drop 1 d6drop+d6: 2; contribution 2; critical-failure 2 Critical failured6CF+d6: 6; contribution 6; critical-success 6 Critical successd6CS+d6: 4; contribution 4 4 d6)Total: 25

showModifierIcons and showModifierLabels are independent. Once and limited variants share their modifier family's icon; labels and tooltips retain the details. Group member flags appear on the member rather than repeating on all its nested dice.

You can change individual icon colours using CSS variables on your output container:

css
.dice-results {
  --modifier-explode-color: #9b5d0a;
  --modifier-critical-success-color: #96700b;
}

/* Use one colour for every modifier icon. */
.dice-results.monochrome {
  --modifier-icon-color: currentColor;
}

The icon families are explode, explode-matching-batch, explode-matching-batch-failure, compound, penetrate, re-roll, unique, min, max, map, target-success, target-failure, critical-success and critical-failure.

Each supports --modifier-{name}-color; compound's rear burst uses the explosion colour.

Calculated keep and drop counts โ€‹

When a keep or drop count comes from an expression, the presenter shows the evaluated count in a caption beneath the dice pool or roll group it applies to. For example, with $keep set to 2, this roll shows Keep highest 3:

javascript
const roll = new Roll('5d6kh($keep + 1)', { data: { keep: 2 } });
const svg = roll.present(new SvgPresenter());
Dice results Evaluated results with arithmetic, modifiers and individual dice. 5d6kh($keep + 1)Dice pool: 5d6kh($keep+1); 5 results; 3 counted; total 13; kh($keep+1) โ†’ 3(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6+d6: 3; contribution 3 3 d6+d6: 1; contribution 0; drop 1 d6drop+d6: 2; contribution 0; drop 2 d6drop)kh($keep+1) โ†’ 3Keep highest 3Total: 13

A roll group can use a dice roll for the count, as in {2d6, 2d8, 2d10}dl(d2):

Dice results Evaluated results with arithmetic, modifiers and individual dice. {2d6, 2d8, 2d10}dl(d2)Roll group: {2d6, 2d8, 2d10}dl(d2); 3 members; 1 counted; total 12; dl(d2) โ†’ 2{Group member: 10; contribution 0; dropDice pool: 2d6; 2 results; 2 counted; total 10; excluded by enclosing group(d6: 4; contribution 0 4 d6+d6: 6; contribution 0 6 d6)drop,Group member: 2; contribution 0; dropDice pool: 2d8; 2 results; 2 counted; total 2; excluded by enclosing group(d8: 1; contribution 0 1 d8+d8: 1; contribution 0 1 d8)drop,Group member: 12; contribution 12; Dice pool: 2d10; 2 results; 2 counted; total 12(d10: 6; contribution 6 6 d10+d10: 6; contribution 6 6 d10)}dl(d2) โ†’ 2Drop lowest 2Total: 12

Dropped dice or group members are still faded and crossed out. Set showModifierLabels: false to hide the caption. The pool or group tooltip still shows the calculation, such as kh($keep+1) โ†’ 3.

Mapped results โ€‹

The map modifier displays a DNA icon on each mapped die or immediate group member. By default, dice show their mapped values, while group members show their original expressions followed by their mapped values.

javascript
const roll = new Roll('2d6map($$value + d8)');
const svg = roll.present(new SvgPresenter());
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6map($$value + d8)Dice pool: 2d6map($$value+d8); 2 results; 2 counted; total 12(d6: 5; mapped by $$value+d8 = 5; contribution 5; initial value 4 5 Mapped resultd6+d6: 7; mapped by $$value+d8 = 7; contribution 7; initial value 6 7 Mapped resultd6)Total: 12

Set expandInlineRolls: true to display the evaluated mapping expressions, including any nested dice. The following example presents the same stored roll without rolling again:

javascript
const svg = roll.present(new SvgPresenter({ expandInlineRolls: true }));
Dice results Evaluated results with arithmetic, modifiers and individual dice. 2d6map($$value + d8)Dice pool: 2d6map($$value+d8); 2 results; 2 counted; total 12(Mapped value: 5d6: 5; mapped by $$value+d8 = 5; contribution 5; initial value 4 4 Mapped resultd6โ†ฆ($$value: 44$$value+Dice pool: d8; 1 result; 1 counted; total 1(d8: 1; contribution 1 1 d8))=5+Mapped value: 7d6: 7; mapped by $$value+d8 = 7; contribution 7; initial value 6 6 Mapped resultd6โ†ฆ($$value: 66$$value+Dice pool: d8; 1 result; 1 counted; total 1(d8: 1; contribution 1 1 d8))=7)Total: 12

Hover over an expanded mapped result to highlight the original roll and its mapping details. Nested maps, pools and conditional expressions retain their own more specific highlights.

Roll histories โ€‹

Use the same presenter with Roller to display a history, with separate rows and a combined total:

javascript
import { Roller } from '@dice-roller/rpg-dice-roller';
import { SvgPresenter } from '@dice-roller/svg-presenter';

const roller = new Roller({
  presenter: new SvgPresenter({ size: 80, width: 600 }),
});

roller.roll('d20 + 5');
roller.roll('2d6 + 3');

const svg = roller.present();
Dice results Evaluated results with arithmetic, modifiers and individual dice. 1. d20 + 5Dice pool: d20; 1 result; 1 counted; total 14(d20: 14; contribution 14 14 d20)+5Total: 192. 2d6 + 3Dice pool: 2d6; 2 results; 2 counted; total 9(d6: 6; contribution 6 6 d6+d6: 3; contribution 3 3 d6)+3Total: 12History total: 31

You can also present an evaluated expression directly with presenter.present(roll.result).

Comparisons and ternaries โ€‹

Comparisons show the evaluated operands and a numeric outcome beneath them. For example, (2d6 + 4) > 10 shows true ยท 1 when the left-hand side exceeds 10, or false ยท 0 otherwise.

Ternaries show the evaluated condition in an IF card and the selected branch in a THEN or ELSE card. The cards stack when the available width is too narrow. A $$condition reference displays the stored numeric value of the condition.

javascript
const roll = new Roll('((d20) >= 10) ? (2d8 + $$condition) : (d4)');
const svg = roll.present(new SvgPresenter());
Dice results Evaluated results with arithmetic, modifiers and individual dice. ((d20) >= 10) ? (2d8 + $$condition) : (d4)Conditional: condition 1 (true); then branch selected; result 4; else branch not evaluatedCondition: 1; true (non-zero)IF ยท trueComparison: >=; true ยท 1(Dice pool: d20; 1 result; 1 counted; total 14(d20: 14; contribution 14 14 d20))โ‰ฅ10true ยท 1โ†’Selected branch; result 4THENDice pool: 2d8; 2 results; 2 counted; total 3(d8: 2; contribution 2 2 d8+d8: 1; contribution 1 1 d8)+$$condition: 11$$conditionTotal: 4

Set showVariableLabels: false to hide the name beneath a variableโ€™s resolved value. The value and its tooltip remain visible.

Hover highlights โ€‹

Hover over the inline examples to see dice and modifier tooltips. Pools, group members and roll groups highlight the most specific region beneath the pointer. Matching mathematical parentheses and function names highlight together in supporting browsers.

Try this example: hover the dice or the spaces between them for a pool highlight; hover the + between the first two pools for a group member highlight; hover the braces or comma for the whole roll group. Hover either parenthesis in (d10 + 2) or the max function name to highlight the matching mathematical delimiters.

Dice results Evaluated results with arithmetic, modifiers and individual dice. {2d6 + d8, (d10 + 2) * 2} + max(d4, d6)Roll group: {2d6+d8, (d10+2)*2}; 2 members; 2 counted; total 29{Group member: 11; contribution 11; Dice pool: 2d6; 2 results; 2 counted; total 10(d6: 4; contribution 4 4 d6+d6: 6; contribution 6 6 d6)+Dice pool: d8; 1 result; 1 counted; total 1(d8: 1; contribution 1 1 d8),Group member: 18; contribution 18; (Dice pool: d10; 1 result; 1 counted; total 7(d10: 7; contribution 7 7 d10)+2)ร—2}+max(Dice pool: d4; 1 result; 1 counted; total 4(d4: 4; contribution 4 4 d4),Dice pool: d6; 1 result; 1 counted; total 6(d6: 6; contribution 6 6 d6))Total: 35

Customise the highlights with CSS variables:

css
.dice-results {
  --result-hover-color: #7962b5;
  --result-hover-fill-opacity: 0.08;
  --result-hover-stroke-opacity: 0.5;
  --math-hover-color: #bda0ff;
}

Set both hover opacity variables to 0 to hide region highlights while retaining tooltips. Highlight effects use CSS :hover and :has() and need an interactive SVG.

Metadata โ€‹

Each die's enclosing <g> includes metadata you can use in CSS or JavaScript:

AttributeMeaning
data-die-typeCore factory name, such as standard, percentile, fudge, range or face.
data-shapeArtwork key, such as d20, d6 or generic.
data-facesNumber of faces, including repeated faces.
data-notationDie notation, such as d20 or dF.
data-min, data-maxLowest and highest possible values.
data-value, data-initial-valueFinal and original rolled values.
data-calculation-valueScoring value before exclusion.
data-use-in-totalThe die's own inclusion state.
data-excludedEffective exclusion, including a dropped enclosing group member.
data-flagsSpace-separated modifier flags.
data-mapped-valueMapped value, when a mapping result is present.

For example, find the Fudge dice inside your container:

javascript
const dice = document.querySelectorAll('.dice-results [data-die-type="fudge"]');
for (const die of dice) {
  console.log(Number(die.dataset.value));
}

Pools have g[data-pool="true"] wrappers with notation, quantity, result count, counted count and value. Groups use g[data-group="true"] with notation, member count, counted count and value. These counts describe the stored results, including generated and dropped dice, rather than discarded reroll attempts. A pool's own counted results can still be excluded by an enclosing group.

Custom artwork โ€‹

Override only the shapes you want to change using artwork. All other shapes keep their bundled defaults:

javascript
const customD6 = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <circle class="die-outline" cx="50" cy="50" r="44" />
  <text class="die-value" data-slot="value" x="50" y="50"
    text-anchor="middle" dominant-baseline="central"
    font-size="22" data-max-width="60" fill="currentColor" stroke="none"></text>
</svg>`;

const presenter = new SvgPresenter({
  artwork: { d6: customD6 },
});

Artwork keys are d2, d4, d6, d8, d10, d12, d20 and generic. You can add a key such as d13 for a dedicated shape, or override generic to change the fallback. Overriding d6 also changes the shape used for Fudge dice; d10 also covers percentile dice.

Use trusted SVG templates with a 0 0 100 100 viewBox and an empty <text data-slot="value"> element for the presenter to fill. See the artwork guide for the template format.

The package also exports defaultArtwork, SvgPresenterOptions for reusable TypeScript options, and raw icons at paths such as @dice-roller/svg-presenter/icons/d20.svg.

Appearance defaults โ€‹

OptionDefault
size140
width1040
noWrapfalse
expandInlineRollsfalse
stroke'#7962b5'
fill'#eee7fb'
filledfalse
showValuestrue
showDescriptionstrue
showDiceLabelstrue
showVariableLabelstrue
showModifierLabelstrue
showModifierIconstrue
showNotationtrue
showTotalstrue
modifierIconPosition'corner'
artworkBundled dice shapes.

See the SVG package README for further reference.

MIT Licensed ยท Credits