UNPKG

blockies-react-svg

Version:

Blockies react SVG function component, blocky identicons, address-unique ethereum avatar, SVG base64 generation in browser or nodejs.

45 lines (42 loc) 1.91 kB
import { b as buildOpts, c as createImageData } from './blockies-a884f18e.mjs'; const store = Object.create(null); const _btoa = btoa || (window === null || window === void 0 ? void 0 : window.btoa) || ((str) => Buffer.from(str, 'binary').toString('base64')); function makeBlockiesUrl(address, size = 8, caseSensitive = false, scale = 10) { if (!address) throw new Error('Address is required'); if (!caseSensitive) address = address.toLowerCase(); if (store[`${size}:${address}`]) { return store[`${size}:${address}`]; } const opts = buildOpts({ seed: address, size, scale }); const imageData = createImageData(opts.size); const width = size * scale; const svgMarkup = ` <svg width="${width}" height="${width}" viewBox="0 0 ${width} ${width}" xmlns="http://www.w3.org/2000/svg"> <rect width="${width}" height="${width}" fill="${opts.bgcolor}" /> <g fill="${opts.color}"> ${imageData === null || imageData === void 0 ? void 0 : imageData.map((value, i) => { if (value === 1) { const row = (i % size) * scale; const col = Math.floor(i / size) * scale; return `<rect width="${scale}" height="${scale}" x="${row}" y="${col}" />`; } }).join('')} </g> <g fill="${opts.spotcolor}"> ${imageData === null || imageData === void 0 ? void 0 : imageData.map((value, i) => { if (value === 2) { const row = (i % size) * scale; const col = Math.floor(i / size) * scale; return `<rect width="${scale}" height="${scale}" x="${row}" y="${col}" />`; } }).join('')} </g> </svg>`; const base64Url = `data:image/svg+xml;base64,${_btoa(svgMarkup)}`; store[`${size}:${address}`] = base64Url; return base64Url; } export { makeBlockiesUrl as default }; //# sourceMappingURL=makeBlockiesUrl.mjs.map