@esmirnov.ai/sperm-lab 0.0.0-stage → 0.4.2
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/LICENSE +21 -0
- package/README.md +188 -2
- package/dist/core/constants.d.ts +2 -0
- package/dist/core/constants.js +2 -0
- package/dist/core/defects.d.ts +214 -0
- package/dist/core/defects.js +57 -0
- package/dist/core/factors.d.ts +192 -0
- package/dist/core/factors.js +115 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.js +3 -0
- package/dist/core/simulation.d.ts +19 -0
- package/dist/core/simulation.js +382 -0
- package/dist/core/types.d.ts +116 -0
- package/dist/core/types.js +1 -0
- package/dist/react/geometry.d.ts +26 -0
- package/dist/react/geometry.js +98 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +2 -0
- package/dist/react/renderer.d.ts +16 -0
- package/dist/react/renderer.js +165 -0
- package/dist/react/sperm-lab.d.ts +17 -0
- package/dist/react/sperm-lab.js +524 -0
- package/dist/styles.css +211 -0
- package/docs/API.md +234 -0
- package/docs/DEPLOY.md +76 -0
- package/docs/MODEL.md +164 -0
- package/package.json +84 -4
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { getCellGeometry } from "./geometry.js";
|
|
2
|
+
export { getCellAnchor, getCellGeometry, hitTestCell } from "./geometry.js";
|
|
3
|
+
export function paintSimulation(context, world, width, height, palette, display) {
|
|
4
|
+
context.clearRect(0, 0, width, height);
|
|
5
|
+
if (![width, height].every(Number.isFinite) || width <= 0 || height <= 0)
|
|
6
|
+
return;
|
|
7
|
+
context.lineCap = "round";
|
|
8
|
+
context.lineJoin = "round";
|
|
9
|
+
if (display.trails) {
|
|
10
|
+
context.strokeStyle = palette.sage;
|
|
11
|
+
context.lineWidth = .7;
|
|
12
|
+
context.globalAlpha = .12;
|
|
13
|
+
for (const cell of world.cells) {
|
|
14
|
+
if (cell.trail.length < 2)
|
|
15
|
+
continue;
|
|
16
|
+
context.beginPath();
|
|
17
|
+
let previous;
|
|
18
|
+
for (const point of cell.trail) {
|
|
19
|
+
if (!previous || Math.abs(point.x - previous.x) > .3 || Math.abs(point.y - previous.y) > .3)
|
|
20
|
+
context.moveTo(point.x * width, point.y * height);
|
|
21
|
+
else
|
|
22
|
+
context.lineTo(point.x * width, point.y * height);
|
|
23
|
+
previous = point;
|
|
24
|
+
}
|
|
25
|
+
context.stroke();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
context.globalAlpha = 1;
|
|
29
|
+
for (const cell of world.cells)
|
|
30
|
+
paintCell(context, cell, world.time, width, height, palette, display);
|
|
31
|
+
}
|
|
32
|
+
function headOutline(context, rx, ry, asymmetric) {
|
|
33
|
+
context.beginPath();
|
|
34
|
+
context.moveTo(-rx * .95, 0);
|
|
35
|
+
context.bezierCurveTo(-rx * .9, -ry * .63, -rx * .25, -ry * (asymmetric ? 1.2 : 1), rx * .35, -ry * .95);
|
|
36
|
+
context.bezierCurveTo(rx * 1.05, -ry * .9, rx * 1.08, ry * .9, rx * .35, ry * .95);
|
|
37
|
+
context.bezierCurveTo(-rx * .25, ry * (asymmetric ? .78 : 1), -rx * .9, ry * .63, -rx * .95, 0);
|
|
38
|
+
context.closePath();
|
|
39
|
+
}
|
|
40
|
+
function paintCell(context, cell, time, width, height, palette, display) {
|
|
41
|
+
const selected = cell.id === display.selected;
|
|
42
|
+
const color = selected ? palette.accent : palette.ink;
|
|
43
|
+
const damage = palette.damage ?? "#b82735";
|
|
44
|
+
const paper = palette.paper ?? "#eff2ed";
|
|
45
|
+
const geometry = getCellGeometry(cell, time, width, height);
|
|
46
|
+
const { anchor, scale, headRx: rx, headRy: ry } = geometry;
|
|
47
|
+
const opacity = cell.alive ? .97 : .66;
|
|
48
|
+
const tailDefect = cell.tailShape !== "typical" || cell.tailCount > 1;
|
|
49
|
+
context.globalAlpha = tailDefect ? .98 : opacity;
|
|
50
|
+
context.strokeStyle = tailDefect ? damage : color;
|
|
51
|
+
for (const tail of geometry.tails) {
|
|
52
|
+
for (let segment = 1; segment < tail.length; segment++) {
|
|
53
|
+
const previous = tail[segment - 1], point = tail[segment];
|
|
54
|
+
context.lineWidth = Math.max(.8, 2.1 * scale * (1 - segment / (tail.length - 1) * .55));
|
|
55
|
+
context.beginPath();
|
|
56
|
+
context.moveTo(previous.x, previous.y);
|
|
57
|
+
context.lineTo(point.x, point.y);
|
|
58
|
+
context.stroke();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (geometry.midpiece.length) {
|
|
62
|
+
const injured = cell.midpieceDamage || cell.midpieceShape !== "typical";
|
|
63
|
+
context.globalAlpha = injured ? 1 : opacity;
|
|
64
|
+
context.strokeStyle = injured ? damage : palette.sage;
|
|
65
|
+
context.lineWidth = geometry.midpieceWidth;
|
|
66
|
+
context.beginPath();
|
|
67
|
+
geometry.midpiece.forEach((point, index) => index ? context.lineTo(point.x, point.y) : context.moveTo(point.x, point.y));
|
|
68
|
+
context.stroke();
|
|
69
|
+
if (cell.midpieceDamage) {
|
|
70
|
+
// One small pale interruption distinguishes injury without a segmented sleeve.
|
|
71
|
+
const point = geometry.midpiece[7], next = geometry.midpiece[8];
|
|
72
|
+
context.strokeStyle = paper;
|
|
73
|
+
context.lineWidth = Math.max(1.2, 1.35 * scale);
|
|
74
|
+
context.beginPath();
|
|
75
|
+
context.moveTo(point.x, point.y);
|
|
76
|
+
context.lineTo(next.x, next.y);
|
|
77
|
+
context.stroke();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const connection of geometry.connections) {
|
|
81
|
+
context.globalAlpha = .95;
|
|
82
|
+
context.strokeStyle = damage;
|
|
83
|
+
context.lineWidth = 1.7 * scale;
|
|
84
|
+
context.beginPath();
|
|
85
|
+
context.moveTo(connection[0].x, connection[0].y);
|
|
86
|
+
context.lineTo(connection[1].x, connection[1].y);
|
|
87
|
+
context.stroke();
|
|
88
|
+
}
|
|
89
|
+
context.save();
|
|
90
|
+
context.translate(anchor.x, anchor.y);
|
|
91
|
+
context.rotate(cell.angle);
|
|
92
|
+
for (const head of geometry.heads) {
|
|
93
|
+
context.save();
|
|
94
|
+
context.translate(0, head.localY);
|
|
95
|
+
const damaged = display.dna && cell.dnaDamage;
|
|
96
|
+
const abnormal = cell.headShape !== "typical" || cell.headCount === 2 || cell.tailCount === 0;
|
|
97
|
+
context.globalAlpha = abnormal ? .98 : opacity;
|
|
98
|
+
context.fillStyle = paper;
|
|
99
|
+
context.strokeStyle = abnormal ? damage : color;
|
|
100
|
+
context.lineWidth = (selected ? 1.6 : 1.25) * scale;
|
|
101
|
+
headOutline(context, rx, ry, head.asymmetric);
|
|
102
|
+
context.fill();
|
|
103
|
+
context.stroke();
|
|
104
|
+
context.save();
|
|
105
|
+
headOutline(context, rx * .95, ry * .95, head.asymmetric);
|
|
106
|
+
context.clip();
|
|
107
|
+
context.globalAlpha = opacity * .12;
|
|
108
|
+
context.fillStyle = palette.sage;
|
|
109
|
+
context.fillRect(-rx, -ry, rx * 2, ry * 2);
|
|
110
|
+
context.globalAlpha = opacity * .2;
|
|
111
|
+
context.fillStyle = paper;
|
|
112
|
+
context.beginPath();
|
|
113
|
+
context.ellipse(rx * .53, 0, rx * .55, ry * .9, 0, 0, Math.PI * 2);
|
|
114
|
+
context.fill();
|
|
115
|
+
context.globalAlpha = opacity * (damaged ? .11 : .29);
|
|
116
|
+
context.fillStyle = palette.ink;
|
|
117
|
+
context.beginPath();
|
|
118
|
+
context.ellipse(-rx * .05, 0, rx * .78, ry * .74, 0, 0, Math.PI * 2);
|
|
119
|
+
context.fill();
|
|
120
|
+
if (!damaged) {
|
|
121
|
+
context.globalAlpha = opacity * .3;
|
|
122
|
+
for (let speck = 0; speck < 12; speck++) {
|
|
123
|
+
const sx = Math.sin(speck * 2.4 + cell.id) * rx * .5 - rx * .05;
|
|
124
|
+
const sy = Math.cos(speck * 1.8 + cell.phase) * ry * .43;
|
|
125
|
+
context.beginPath();
|
|
126
|
+
context.ellipse(sx, sy, .38 * scale, .28 * scale, 0, 0, Math.PI * 2);
|
|
127
|
+
context.fill();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
context.save();
|
|
132
|
+
context.beginPath();
|
|
133
|
+
context.ellipse(-rx * .05, 0, rx * .78, ry * .74, 0, 0, Math.PI * 2);
|
|
134
|
+
context.clip();
|
|
135
|
+
context.globalAlpha = 1;
|
|
136
|
+
context.strokeStyle = damage;
|
|
137
|
+
context.lineWidth = Math.max(1.4, 1.6 * scale);
|
|
138
|
+
const halfWidth = Math.max(1.05, rx * .12);
|
|
139
|
+
const pieces = Math.max(3, Math.min(6, cell.dnaFragmentation));
|
|
140
|
+
for (let index = 0; index < pieces; index++) {
|
|
141
|
+
const column = index % 3 - 1, row = index < 3 ? -1 : 1;
|
|
142
|
+
const cx = column * rx * .54 - rx * .05, cy = row * ry * .27;
|
|
143
|
+
context.beginPath();
|
|
144
|
+
context.moveTo(cx - halfWidth, cy + ry * .035);
|
|
145
|
+
context.quadraticCurveTo(cx, cy - ry * .08, cx + halfWidth, cy - ry * .025);
|
|
146
|
+
context.stroke();
|
|
147
|
+
}
|
|
148
|
+
context.restore();
|
|
149
|
+
}
|
|
150
|
+
context.restore();
|
|
151
|
+
context.restore();
|
|
152
|
+
}
|
|
153
|
+
context.restore();
|
|
154
|
+
if (selected) {
|
|
155
|
+
context.globalAlpha = .8;
|
|
156
|
+
context.strokeStyle = palette.accent;
|
|
157
|
+
context.lineWidth = 1.2;
|
|
158
|
+
context.setLineDash([3, 3]);
|
|
159
|
+
context.beginPath();
|
|
160
|
+
context.ellipse(anchor.x, anchor.y, rx + 7, cell.headCount === 2 ? ry * 2.22 + 7 : ry + 7, cell.angle, 0, Math.PI * 2);
|
|
161
|
+
context.stroke();
|
|
162
|
+
context.setLineDash([]);
|
|
163
|
+
}
|
|
164
|
+
context.globalAlpha = 1;
|
|
165
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type CSSProperties } from "react";
|
|
2
|
+
import { getMetrics, type FactorId } from "../core/index.js";
|
|
3
|
+
type Metrics = ReturnType<typeof getMetrics>;
|
|
4
|
+
type Factors = Partial<Record<FactorId, number>>;
|
|
5
|
+
export type SpermLabMetrics = Metrics;
|
|
6
|
+
export type SpermLabProps = {
|
|
7
|
+
seed?: number;
|
|
8
|
+
count?: number;
|
|
9
|
+
initialFactors?: Factors;
|
|
10
|
+
initialFactor?: FactorId;
|
|
11
|
+
teachingPopulation?: boolean;
|
|
12
|
+
className?: string;
|
|
13
|
+
style?: CSSProperties;
|
|
14
|
+
onMetrics?: (metrics: SpermLabMetrics) => void;
|
|
15
|
+
};
|
|
16
|
+
export declare function SpermLab({ seed, count, initialFactors, initialFactor, teachingPopulation, className, style, onMetrics }?: SpermLabProps): import("react").JSX.Element;
|
|
17
|
+
export {};
|