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.
Install โ
Add the SVG presenter to your project using your preferred package manager:
npm install @dice-roller/svg-presenterThe 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():
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:
<div id="dice-results"></div>document.querySelector('#dice-results').innerHTML = svg;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:
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:
const presenter = new SvgPresenter({
size: 100,
width: 600,
stroke: '#236b57',
fill: '#dff3e9',
filled: true,
});
const svg = roll.present(presenter);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:
.dice-results [data-shape="d6"] .die-outline {
fill: #fff0d6;
stroke: #9b5d0a;
}
.dice-results [data-shape="d6"] .die-value {
fill: #704200;
}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.
const roll = new Roll('6d6 + 3');
const presenter = new SvgPresenter({ size: 90, width: 420 });
const svg = roll.present(presenter);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:
const presenter = new SvgPresenter({ noWrap: true });
const svg = roll.present(presenter);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:
.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:
const presenter = new SvgPresenter({
size: 100,
width: 600,
showDiceLabels: false,
showModifierLabels: false,
showNotation: false,
showTotals: false,
});These visibility options all default to true:
| Option | What it shows |
|---|---|
showValues | Values on the dice, including Fudge symbols. |
showDescriptions | Pool and group descriptions alongside their results. |
showDiceLabels | Die notation beneath each die. |
showVariableLabels | Variable names such as $bonus and $$condition beneath their resolved values. |
showModifierLabels | Modifier labels beneath dice and group members, and calculated keep/drop captions beneath pools and groups. |
showModifierIcons | Symbols for modifier effects. |
showNotation | The supplied notation above each roll. |
showTotals | Individual 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:
const roll = new Roll('2d6!1 + 4d6kh3cs=6cf=2');
const presenter = new SvgPresenter();
const svg = roll.present(presenter);Use modifierIconPosition: 'below' to move them underneath. This example shows the same stored roll:
const presenter = new SvgPresenter({
modifierIconPosition: 'below',
});
const svg = roll.present(presenter);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:
.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:
const roll = new Roll('5d6kh($keep + 1)', { data: { keep: 2 } });
const svg = roll.present(new SvgPresenter());A roll group can use a dice roll for the count, as in {2d6, 2d8, 2d10}dl(d2):
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.
const roll = new Roll('2d6map($$value + d8)');
const svg = roll.present(new SvgPresenter());Set expandInlineRolls: true to display the evaluated mapping expressions, including any nested dice. The following example presents the same stored roll without rolling again:
const svg = roll.present(new SvgPresenter({ expandInlineRolls: true }));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:
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();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.
const roll = new Roll('((d20) >= 10) ? (2d8 + $$condition) : (d4)');
const svg = roll.present(new SvgPresenter());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.
Customise the highlights with CSS variables:
.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:
| Attribute | Meaning |
|---|---|
data-die-type | Core factory name, such as standard, percentile, fudge, range or face. |
data-shape | Artwork key, such as d20, d6 or generic. |
data-faces | Number of faces, including repeated faces. |
data-notation | Die notation, such as d20 or dF. |
data-min, data-max | Lowest and highest possible values. |
data-value, data-initial-value | Final and original rolled values. |
data-calculation-value | Scoring value before exclusion. |
data-use-in-total | The die's own inclusion state. |
data-excluded | Effective exclusion, including a dropped enclosing group member. |
data-flags | Space-separated modifier flags. |
data-mapped-value | Mapped value, when a mapping result is present. |
For example, find the Fudge dice inside your container:
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:
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 โ
| Option | Default |
|---|---|
size | 140 |
width | 1040 |
noWrap | false |
expandInlineRolls | false |
stroke | '#7962b5' |
fill | '#eee7fb' |
filled | false |
showValues | true |
showDescriptions | true |
showDiceLabels | true |
showVariableLabels | true |
showModifierLabels | true |
showModifierIcons | true |
showNotation | true |
showTotals | true |
modifierIconPosition | 'corner' |
artwork | Bundled dice shapes. |
See the SVG package README for further reference.