UNPKG

identity-img

Version:

## Simple library for create unique image by hash

116 lines 4.23 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var Identity = /** @class */ (function () { function Identity(hash, options) { options = __assign(__assign({}, Identity.defaultOptions), (options || {})); if (options.randomColor) { options.bgColor = Identity.hashToColor(hash, options.bgStep, options.bgLen); options.mainColor = Identity.hashToColor(hash, options.mainStep, options.mainLen); options.nanColor = Identity.hashToColor(hash, options.nanLen, options.nanStep); } this.hash = hash; this.options = options; this.canvas = document.createElement('CANVAS'); this.ctx = this.canvas.getContext('2d'); this.canvas.width = this.options.size; this.canvas.height = this.options.size; this.hashMatrix = []; this._fillMatrix(hash); } Identity.prototype.getImage = function () { var w = this.options.size / this.options.cells; var h = this.options.size / this.options.rows; for (var row = 0; row < this.options.rows; row++) { for (var cell = 0; cell < this.options.cells; cell++) { this._addCell(cell, row, w, h); } } return this.canvas.toDataURL('image/png'); }; Identity.prototype._addCell = function (x, y, w, h) { var color = this._getColors(x, y); if (!color) { this._addRect(x, y, w, h, this.options.bgColor); } this._addRect(x, y, w, h, color); }; Identity.prototype._addRect = function (x, y, w, h, color) { this.ctx.fillStyle = color; this.ctx.beginPath(); this.ctx.rect(x * w - 0.5, y * h - 0.5, w + 0.5, h + 0.5); this.ctx.fill(); this.ctx.closePath(); }; Identity.prototype._getColors = function (x, y) { if (x < this.options.cells / 2) { return isNaN(this.hashMatrix[x][y]) ? this.options.nanColor : this.hashMatrix[x][y] % 2 ? this.options.mainColor : null; } else { return this._getColors(this.options.cells - 1 - x, y); } }; Identity.prototype._fillMatrix = function (hash) { var i = 0; for (var x = 0; x < this.options.cells; x++) { this.hashMatrix[x] = []; for (var y = 0; y < this.options.rows; y++) { if (this.hash[i] == null) { this.hash += hash; } this.hashMatrix[x][y] = parseInt(this.hash[i], 30); i++; } } }; Identity.hashToColor = function (hash, step, length) { var parse = function (part) { var arr = part.split(''); var tmp = []; do { tmp.push(arr.splice(0, length)); } while (arr.length); return tmp.reduce(function (r, i) { return Math.min(r + parseInt(i, 36), 255); }, 0); }; var color = []; for (var i = 0; i < 3; i++) { color.push(hash.slice(Math.round(-(7 / length) - length * 3 - step * i), hash.length - step * i)); } return "rgb(" + color.map(parse).join() + ")"; }; Identity.defaultOptions = { size: 90, bgColor: '#bfbfbf', nanColor: '#EFB66C', mainColor: '#1C3D29', rows: 9, cells: 9, randomColor: true, mainStep: 4, nanStep: 5, mainLen: 1.5, nanLen: 3, bgStep: 3, bgLen: 4 }; return Identity; }()); function config(options) { Identity.defaultOptions = __assign(__assign({}, Identity.defaultOptions), options); } exports.config = config; function create(hash, options) { return new Identity(hash, options).getImage(); } exports.create = create; //# sourceMappingURL=Identity.js.map