termlings 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/termlings.js CHANGED
@@ -406,17 +406,21 @@ function renderSVG(dna2, pixelSize = 10, frame = 0, background = "auto", padding
406
406
  const resolvedBg = background === "auto" ? toHex(...bgRgb2) : background;
407
407
  const cols = 9;
408
408
  const rows = grid.length;
409
+ const maxRows = 12;
409
410
  const pad = padding;
410
- const w = (cols + pad * 2) * pixelSize;
411
- const h = (rows + pad * 2) * pixelSize;
411
+ const side = Math.max(cols, maxRows) + pad * 2;
412
+ const w = side * pixelSize;
413
+ const h = side * pixelSize;
414
+ const ox = Math.floor((side - cols) / 2);
415
+ const oy = Math.floor((side - rows) / 2);
412
416
  const half = Math.round(pixelSize / 2);
413
417
  const quarter = Math.round(pixelSize / 4);
414
418
  const rects = [];
415
419
  for (let y = 0; y < rows; y++) {
416
420
  for (let x = 0; x < cols; x++) {
417
421
  const cell = grid[y][x];
418
- const rx = (x + pad) * pixelSize;
419
- const ry = (y + pad) * pixelSize;
422
+ const rx = (x + ox) * pixelSize;
423
+ const ry = (y + oy) * pixelSize;
420
424
  if (cell === "f") {
421
425
  rects.push(`<rect x="${rx}" y="${ry}" width="${pixelSize}" height="${pixelSize}" fill="${faceHex}"/>`);
422
426
  } else if (cell === "l") {
@@ -516,9 +520,13 @@ function renderLayeredSVG(dna2, pixelSize = 10, bw2 = false, padding = 0) {
516
520
  const legVariant = LEGS[traits2.legs];
517
521
  const legFrameCount2 = legVariant.length;
518
522
  const totalRows = hatRows.length + 1 + 1 + 2 + 2 + 1;
523
+ const maxRows = 12;
519
524
  const pad = padding;
520
- const w = (cols + pad * 2) * pixelSize;
521
- const h = (totalRows + pad * 2) * pixelSize;
525
+ const side = Math.max(cols, maxRows) + pad * 2;
526
+ const w = side * pixelSize;
527
+ const h = side * pixelSize;
528
+ const ox = Math.floor((side - cols) / 2);
529
+ const oy = Math.floor((side - totalRows) / 2);
522
530
  function px(cell, rx, ry) {
523
531
  if (cell === "_") return "";
524
532
  if (cell === "f") return `<rect x="${rx}" y="${ry}" width="${pixelSize}" height="${pixelSize}" fill="${faceHex}"/>`;
@@ -537,7 +545,7 @@ function renderLayeredSVG(dna2, pixelSize = 10, bw2 = false, padding = 0) {
537
545
  let out = "";
538
546
  for (let y = 0; y < rows.length; y++) {
539
547
  for (let x = 0; x < cols; x++) {
540
- out += px(rows[y][x], (x + pad) * pixelSize, (startY + y + pad) * pixelSize);
548
+ out += px(rows[y][x], (x + ox) * pixelSize, (startY + y + oy) * pixelSize);
541
549
  }
542
550
  }
543
551
  return out;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "termlings",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Open-source pixel creatures for web and the terminal.",
package/src/index.ts CHANGED
@@ -487,9 +487,13 @@ export function renderSVG(dna: string, pixelSize = 10, frame = 0, background: st
487
487
 
488
488
  const cols = 9;
489
489
  const rows = grid.length;
490
+ const maxRows = 12; // max possible: 5-row hat + 7 body
490
491
  const pad = padding;
491
- const w = (cols + pad * 2) * pixelSize;
492
- const h = (rows + pad * 2) * pixelSize;
492
+ const side = Math.max(cols, maxRows) + pad * 2;
493
+ const w = side * pixelSize;
494
+ const h = side * pixelSize;
495
+ const ox = Math.floor((side - cols) / 2);
496
+ const oy = Math.floor((side - rows) / 2);
493
497
 
494
498
  const half = Math.round(pixelSize / 2);
495
499
  const quarter = Math.round(pixelSize / 4);
@@ -497,8 +501,8 @@ export function renderSVG(dna: string, pixelSize = 10, frame = 0, background: st
497
501
  for (let y = 0; y < rows; y++) {
498
502
  for (let x = 0; x < cols; x++) {
499
503
  const cell = grid[y][x];
500
- const rx = (x + pad) * pixelSize;
501
- const ry = (y + pad) * pixelSize;
504
+ const rx = (x + ox) * pixelSize;
505
+ const ry = (y + oy) * pixelSize;
502
506
  if (cell === "f") {
503
507
  rects.push(`<rect x="${rx}" y="${ry}" width="${pixelSize}" height="${pixelSize}" fill="${faceHex}"/>`);
504
508
  } else if (cell === "l") {
@@ -634,9 +638,13 @@ export function renderLayeredSVG(dna: string, pixelSize = 10, bw = false, paddin
634
638
 
635
639
  // hat + face + eyes + 2 mouth + 2 body + 1 legs
636
640
  const totalRows = hatRows.length + 1 + 1 + 2 + 2 + 1;
641
+ const maxRows = 12; // max possible: 5-row hat + 7 body
637
642
  const pad = padding;
638
- const w = (cols + pad * 2) * pixelSize;
639
- const h = (totalRows + pad * 2) * pixelSize;
643
+ const side = Math.max(cols, maxRows) + pad * 2;
644
+ const w = side * pixelSize;
645
+ const h = side * pixelSize;
646
+ const ox = Math.floor((side - cols) / 2);
647
+ const oy = Math.floor((side - totalRows) / 2);
640
648
 
641
649
  function px(cell: Pixel, rx: number, ry: number): string {
642
650
  if (cell === "_") return "";
@@ -657,7 +665,7 @@ export function renderLayeredSVG(dna: string, pixelSize = 10, bw = false, paddin
657
665
  let out = "";
658
666
  for (let y = 0; y < rows.length; y++) {
659
667
  for (let x = 0; x < cols; x++) {
660
- out += px(rows[y][x], (x + pad) * pixelSize, (startY + y + pad) * pixelSize);
668
+ out += px(rows[y][x], (x + ox) * pixelSize, (startY + y + oy) * pixelSize);
661
669
  }
662
670
  }
663
671
  return out;