react-jazzicon
Version:
React component for danfinlay/jazzicon
109 lines • 5.61 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
var React = __importStar(require("react"));
var mersenne_twister_1 = __importDefault(require("mersenne-twister"));
var colorUtils_1 = require("./colorUtils");
var colors_1 = __importDefault(require("./colors"));
var Paper_1 = __importDefault(require("./Paper"));
// constants
var shapeCount = 4;
var svgns = "http://www.w3.org/2000/svg";
var wobble = 30;
var defaultDiameter = 24;
var Jazzicon = /** @class */ (function (_super) {
__extends(Jazzicon, _super);
function Jazzicon() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.genColor = function (colors) {
// @ts-ignore
var rand = _this.generator.random(); // purposefully call the generator once, before using it again on the next line
var idx = Math.floor(colors.length * _this.generator.random());
var color = colors.splice(idx, 1)[0];
return color;
};
_this.hueShift = function (colors, generator) {
var amount = generator.random() * 30 - wobble / 2;
var rotate = function (hex) { return (0, colorUtils_1.colorRotate)(hex, amount); };
return colors.map(rotate);
};
_this.genShape = function (remainingColors, diameter, i, total) {
var center = diameter / 2;
var firstRot = _this.generator.random();
var angle = Math.PI * 2 * firstRot;
var velocity = (diameter / total) * _this.generator.random() + (i * diameter) / total;
var tx = Math.cos(angle) * velocity;
var ty = Math.sin(angle) * velocity;
var translate = "translate(" + tx + " " + ty + ")";
// Third random is a shape rotation on top of all of that.
var secondRot = _this.generator.random();
var rot = firstRot * 360 + secondRot * 180;
var rotate = "rotate(" + rot.toFixed(1) + " " + center + " " + center + ")";
var transform = translate + " " + rotate;
var fill = _this.genColor(remainingColors);
return ((0, jsx_runtime_1.jsx)("rect", { x: "0", y: "0", rx: "0", ry: "0", height: diameter, width: diameter, transform: transform, fill: fill }, i));
};
return _this;
}
Jazzicon.prototype.render = function () {
var _this = this;
var _a = this.props, _b = _a.diameter, diameter = _b === void 0 ? defaultDiameter : _b, _c = _a.paperStyles, paperStyles = _c === void 0 ? {} : _c, seed = _a.seed, _d = _a.svgStyles, svgStyles = _d === void 0 ? {} : _d;
this.generator = new mersenne_twister_1.default(seed);
var remainingColors = this.hueShift(colors_1.default.slice(), this.generator);
var shapesArr = Array(shapeCount).fill(undefined);
return ((0, jsx_runtime_1.jsx)(Paper_1.default, __assign({ color: this.genColor(remainingColors), diameter: diameter, style: paperStyles }, { children: (0, jsx_runtime_1.jsx)("svg", __assign({ xmlns: svgns, x: "0", y: "0", height: diameter, width: diameter, style: svgStyles }, { children: shapesArr.map(function (s, i) {
return _this.genShape(remainingColors, diameter, i, shapeCount - 1);
}) }), void 0) }), void 0));
};
return Jazzicon;
}(React.PureComponent));
exports.default = Jazzicon;
//# sourceMappingURL=Jazzicon.js.map