Renders an indenticon as a SVG string. The response can either be used in the DOM or saved as a file with the *.svg
extension. This method does not have any dependencies to the DOM and can be used to generate icons server-side e.g. by using Node.js.
jdenticon.toSvg(hash|value, size[, config])
Availability | Browser | Node.js |
---|---|---|
jdenticon.toSvg() | Yes | Yes |
In the following example an identicon is rendered as an svg string that is inserted into the page.
Usage browser
<div id="identiconPlaceholder"> </div> <script src="https://cdn.jsdelivr.net/npm/jdenticon@3.2.0/dist/jdenticon.min.js" integrity="sha384-yBhgDqxM50qJV5JPdayci8wCfooqvhFYbIKhv0hTtLvfeeyJMJCscRfFNKIxt43M" crossorigin="anonymous"> </script> <script> var placeholder = document.getElementById("identiconPlaceholder"); placeholder.innerHTML = jdenticon.toSvg("icon value", 200); </script>
Below is an example how an SVG file can be generated using Node.js.
Usage Node.js
const jdenticon = require("jdenticon"); const fs = require("fs"); const svg = jdenticon.toSvg("icon value", 200); fs.writeFileSync("./testicon.svg", svg);
hash|value
is considered a hash string if the string is hexadecimal and contains at least 11 characters. It is otherwise considered a value that will be hashed using SHA1.
size
defines the width and height, icons are always square, of the icon in pixels, including its padding.
config
From v2.2.0 specifies an optional configuration object. If no configuration is specified, the global configuration is used. For backward compability this parameter can also be a padding value in the range 0.0 to 0.5.
The following priority order is used to resolve the icon configuration when the icon is drawn:
config
parameter to toSvg()
jdenticon.configure()
.jdenticon_config
global variable.