Skip to content

Commit 24bf9fb

Browse files
committed
store draw-grid settings
1 parent 8bdb0d6 commit 24bf9fb

File tree

1 file changed

+68
-25
lines changed

1 file changed

+68
-25
lines changed

apps/vps-web/src/app/custom-nodes/classes/draw-grid-node.tsx

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ import { BaseRectNode } from './rect-node-class';
99

1010
import './style.css';
1111

12+
interface GridDrawSettings {
13+
fadeRadius: number;
14+
fadePower: number;
15+
gamma: number;
16+
gridSize: number;
17+
}
18+
1219
class GridDraw {
1320
private grid: HTMLDivElement[][] = [];
1421
public gridValues: number[][] = []; // Store intensity values (0 to 1)
1522
public gridColors: [number, number, number][][] = []; // Store RGB colors
1623
private isDrawing = false;
17-
private GRID_SIZE = 28; //64;
24+
//private GRID_SIZE = 28; //64;
1825
//private readonly MIN_CELL_SIZE = 2; // Minimum cell size in pixels
1926
private readonly BG_COLOR: [number, number, number] = [255, 255, 255]; // White background
2027
private gridContainer: HTMLDivElement;
@@ -24,10 +31,11 @@ class GridDraw {
2431
private rectNode: BaseRectNode;
2532

2633
// Configurable brush parameters
27-
private brushParams = {
34+
private brushParams: GridDrawSettings = {
2835
fadeRadius: 5, // 11x11 brush (5 cells in each direction from center)
2936
fadePower: 1.0,
3037
gamma: 2.0,
38+
gridSize: 28,
3139
};
3240

3341
private readonly COLOR_PRESETS: [number, number, number][] = [
@@ -58,9 +66,14 @@ class GridDraw {
5866
[148, 103, 189], // d3 purple
5967
];
6068

61-
constructor(gridContainer: HTMLDivElement, rectNode: BaseRectNode) {
69+
constructor(
70+
gridContainer: HTMLDivElement,
71+
rectNode: BaseRectNode,
72+
settings: GridDrawSettings
73+
) {
6274
this.rectNode = rectNode;
6375
this.gridContainer = gridContainer as HTMLDivElement;
76+
this.brushParams = settings;
6477

6578
if (!this.gridContainer) {
6679
console.error('Grid container not found!');
@@ -77,23 +90,23 @@ class GridDraw {
7790
this.gridContainer.innerHTML = '';
7891

7992
// Set grid template
80-
this.gridContainer.style.gridTemplateColumns = `repeat(${this.GRID_SIZE}, 1fr)`;
81-
this.gridContainer.style.gridTemplateRows = `repeat(${this.GRID_SIZE}, 1fr)`;
93+
this.gridContainer.style.gridTemplateColumns = `repeat(${this.brushParams.gridSize}, 1fr)`;
94+
this.gridContainer.style.gridTemplateRows = `repeat(${this.brushParams.gridSize}, 1fr)`;
8295

8396
// Initialize grid arrays
84-
this.gridValues = Array(this.GRID_SIZE)
97+
this.gridValues = Array(this.brushParams.gridSize)
8598
.fill(0)
86-
.map(() => Array(this.GRID_SIZE).fill(0));
87-
this.grid = Array(this.GRID_SIZE)
99+
.map(() => Array(this.brushParams.gridSize).fill(0));
100+
this.grid = Array(this.brushParams.gridSize)
88101
.fill(null)
89-
.map(() => Array(this.GRID_SIZE).fill(null));
90-
this.gridColors = Array(this.GRID_SIZE)
102+
.map(() => Array(this.brushParams.gridSize).fill(null));
103+
this.gridColors = Array(this.brushParams.gridSize)
91104
.fill(null)
92-
.map(() => Array(this.GRID_SIZE).fill([...this.BG_COLOR]));
105+
.map(() => Array(this.brushParams.gridSize).fill([...this.BG_COLOR]));
93106

94107
// Create grid cells
95-
for (let y = 0; y < this.GRID_SIZE; y++) {
96-
for (let x = 0; x < this.GRID_SIZE; x++) {
108+
for (let y = 0; y < this.brushParams.gridSize; y++) {
109+
for (let x = 0; x < this.brushParams.gridSize; x++) {
97110
const cell = document.createElement('div');
98111
cell.className = 'draw-grid-cell';
99112
cell.setAttribute('data-disable-interaction', 'true');
@@ -157,9 +170,9 @@ class GridDraw {
157170
}">
158171
</div>
159172
<div class="control-group">
160-
<label>Grid size (${this.GRID_SIZE.toFixed(0)})</label>
173+
<label>Grid size (${this.brushParams.gridSize.toFixed(0)})</label>
161174
<input type="range" id="GRID_SIZE" min="16" max="64" step="1" value="${
162-
this.GRID_SIZE
175+
this.brushParams.gridSize
163176
}">
164177
</div>
165178
<button id="clearButton" class="control-button">Clear Grid</button>
@@ -232,6 +245,7 @@ class GridDraw {
232245
fadeRadiusSlider.previousElementSibling!.textContent = `Brush Size (${
233246
this.brushParams.fadeRadius * 2 + 1
234247
}x${this.brushParams.fadeRadius * 2 + 1})`;
248+
this.onStorageChange?.(this.brushParams);
235249
});
236250

237251
fadePowerSlider.addEventListener('input', (e) => {
@@ -241,21 +255,26 @@ class GridDraw {
241255
fadePowerSlider.previousElementSibling!.textContent = `Fade Power (${this.brushParams.fadePower.toFixed(
242256
1
243257
)})`;
258+
this.onStorageChange?.(this.brushParams);
244259
});
245260

246261
gammaSlider.addEventListener('input', (e) => {
247262
this.brushParams.gamma = parseFloat((e.target as HTMLInputElement).value);
248263
gammaSlider.previousElementSibling!.textContent = `Color Intensity (${this.brushParams.gamma.toFixed(
249264
1
250265
)})`;
266+
this.onStorageChange?.(this.brushParams);
251267
});
252268

253269
GRID_SIZE.addEventListener('input', (e) => {
254-
this.GRID_SIZE = parseInt((e.target as HTMLInputElement).value);
255-
GRID_SIZE.previousElementSibling!.textContent = `Grid size (${this.GRID_SIZE.toFixed(
270+
this.brushParams.gridSize = parseInt(
271+
(e.target as HTMLInputElement).value
272+
);
273+
GRID_SIZE.previousElementSibling!.textContent = `Grid size (${this.brushParams.gridSize.toFixed(
256274
1
257275
)})`;
258276
this.setupGrid();
277+
this.onStorageChange?.(this.brushParams);
259278
});
260279
this.clearButton.addEventListener('click', () => this.clearGrid());
261280

@@ -353,8 +372,8 @@ class GridDraw {
353372

354373
public clearGrid() {
355374
// Reset all grid values and colors
356-
for (let y = 0; y < this.GRID_SIZE; y++) {
357-
for (let x = 0; x < this.GRID_SIZE; x++) {
375+
for (let y = 0; y < this.brushParams.gridSize; y++) {
376+
for (let x = 0; x < this.brushParams.gridSize; x++) {
358377
this.gridValues[y][x] = 0;
359378
this.gridColors[y][x] = [...this.BG_COLOR];
360379
this.updateCellColor(x, y);
@@ -368,13 +387,18 @@ class GridDraw {
368387
private handleDraw(e: MouseEvent | Touch) {
369388
const rect = this.gridContainer.getBoundingClientRect();
370389
const x = Math.floor(
371-
((e.clientX - rect.left) / rect.width) * this.GRID_SIZE
390+
((e.clientX - rect.left) / rect.width) * this.brushParams.gridSize
372391
);
373392
const y = Math.floor(
374-
((e.clientY - rect.top) / rect.height) * this.GRID_SIZE
393+
((e.clientY - rect.top) / rect.height) * this.brushParams.gridSize
375394
);
376395

377-
if (x >= 0 && x < this.GRID_SIZE && y >= 0 && y < this.GRID_SIZE) {
396+
if (
397+
x >= 0 &&
398+
x < this.brushParams.gridSize &&
399+
y >= 0 &&
400+
y < this.brushParams.gridSize
401+
) {
378402
// Set center cell to current color
379403
this.gridValues[y][x] = 1;
380404
this.gridColors[y][x] = [...this.currentColor] as [
@@ -402,9 +426,9 @@ class GridDraw {
402426

403427
if (
404428
newX >= 0 &&
405-
newX < this.GRID_SIZE &&
429+
newX < this.brushParams.gridSize &&
406430
newY >= 0 &&
407-
newY < this.GRID_SIZE
431+
newY < this.brushParams.gridSize
408432
) {
409433
const distance = Math.sqrt(dx * dx + dy * dy);
410434
const maxDistance = Math.sqrt(
@@ -488,6 +512,8 @@ class GridDraw {
488512
}
489513

490514
onDrawCell: undefined | (() => void) = undefined;
515+
onStorageChange: undefined | ((storageObject: GridDrawSettings) => void) =
516+
undefined;
491517
}
492518

493519
export class DrawGridNode extends BaseRectNode {
@@ -576,6 +602,13 @@ export class DrawGridNode extends BaseRectNode {
576602
//}, 50);
577603
};
578604

605+
onStorageChange = (storageObject: GridDrawSettings) => {
606+
if (this.node && this.node.nodeInfo) {
607+
(this.node.nodeInfo as any).drawGridSettings = storageObject;
608+
this.updated();
609+
}
610+
};
611+
579612
render = (node: FlowNode<NodeInfo>) => {
580613
//const nodeInfo = node.nodeInfo as any;
581614
console.log('render rect-node', node.width, node.height);
@@ -590,9 +623,19 @@ export class DrawGridNode extends BaseRectNode {
590623
this.rectElement = element;
591624
}}
592625
renderElement={(element: HTMLElement) => {
593-
this.drawGrid = new GridDraw(element as HTMLDivElement, this);
626+
this.drawGrid = new GridDraw(
627+
element as HTMLDivElement,
628+
this,
629+
(node.nodeInfo as any)?.drawGridSettings ?? {
630+
fadeRadius: 5, // 11x11 brush (5 cells in each direction from center)
631+
fadePower: 1.0,
632+
gamma: 2.0,
633+
gridSize: 28,
634+
}
635+
);
594636

595637
this.drawGrid.onDrawCell = this.onDrawCell;
638+
this.drawGrid.onStorageChange = this.onStorageChange;
596639
}}
597640
class={`draw-grid text-center whitespace-nowrap`}
598641
data-disable-interaction={true}

0 commit comments

Comments
 (0)