UNPKG

@prescott/geo-pattern

Version:

Create beautiful generative geometric background images from a string.

62 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestedSquaresComposer = void 0; const svg_1 = require("../svg"); const base_1 = require("./base"); class NestedSquaresComposer extends base_1.StructureComposer { constructor(seed, preset) { super(seed, preset); this.name = "nested-squares"; this.blockSize = this.map(this.seed.read(0, 1), 0, 15, 4, 12); this.squareSize = this.blockSize * 7; this.width = this.height = (this.squareSize + this.blockSize) * 6 + this.blockSize * 6; } generate() { const nodes = []; let i = 0; for (let y = 0; y <= 5; y++) { for (let x = 0; x <= 5; x++) { this.drawInnerSquare(nodes, x, y, i); this.drawOuterSquare(nodes, x, y, i); i++; } } return nodes; } drawInnerSquare(nodes, x, y, i) { const value = this.seed.read(i, 1); const opacity = this.opacity(value); const fill = this.fillColor(value); const styles = { fill: "none", stroke: fill, style: `opacity: ${opacity}; stroke-width: ${this.blockSize}px`, }; nodes.push(new svg_1.SVGNode("rect", { x: x * this.squareSize + x * this.blockSize * 2 + this.blockSize / 2, y: y * this.squareSize + y * this.blockSize * 2 + this.blockSize / 2, width: this.squareSize, height: this.squareSize, ...styles, })); } drawOuterSquare(nodes, x, y, i) { const value = this.seed.read(39 - i, 1); const opacity = this.opacity(value); const fill = this.fillColor(value); const styles = { fill: "none", stroke: fill, style: `opacity: ${opacity}; stroke-width: ${this.blockSize}px`, }; nodes.push(new svg_1.SVGNode("rect", { x: x * this.squareSize + x * this.blockSize * 2 + this.blockSize / 2 + this.blockSize * 2, y: y * this.squareSize + y * this.blockSize * 2 + this.blockSize / 2 + this.blockSize * 2, width: this.blockSize * 3, height: this.blockSize * 3, ...styles, })); } } exports.NestedSquaresComposer = NestedSquaresComposer; //# sourceMappingURL=nested-squares.js.map