Skip to content

Commit

Permalink
prep for service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
OrionReed committed Dec 1, 2024
1 parent 0f3ee9f commit a58343a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
41 changes: 11 additions & 30 deletions src/distanceField/distance-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,10 @@ export class DistanceField extends HTMLElement {
}

private renderDistanceField() {
const imageData = this.offscreenCtx.getImageData(0, 0, this.resolution, this.resolution);

for (let row = 0; row < this.resolution; row++) {
for (let col = 0; col < this.resolution; col++) {
const index = (col * this.resolution + row) * 4;
const distance = this.fields.getDistance(row, col);
const color = this.fields.getColor(row, col);

const maxDistance = 10;
const normalizedDistance = Math.sqrt(distance) / maxDistance;
const baseColor = {
r: (color * 7) % 256,
g: (color * 13) % 256,
b: (color * 19) % 256,
};

imageData.data[index] = baseColor.r * (1 - normalizedDistance);
imageData.data[index + 1] = baseColor.g * (1 - normalizedDistance);
imageData.data[index + 2] = baseColor.b * (1 - normalizedDistance);
imageData.data[index + 3] = 255;
}
}
// Get the computed ImageData from Fields
const imageData = this.fields.generateImageData();

// Put the ImageData onto the offscreen canvas
this.offscreenCtx.putImageData(imageData, 0, 0);

// Draw scaled version to main canvas
Expand Down Expand Up @@ -164,7 +145,8 @@ export class DistanceField extends HTMLElement {

handleGeometryUpdate = (event: Event) => {
const geometry = event.target as HTMLElement;
const index = Array.from(document.querySelectorAll('fc-geometry')).indexOf(geometry as FolkGeometry);
// TODO: store as array from getgo
const index = Array.from(this.geometries).indexOf(geometry as FolkGeometry);
if (index === -1) return;

const rect = geometry.getBoundingClientRect();
Expand All @@ -176,16 +158,15 @@ export class DistanceField extends HTMLElement {
];

if (index < this.fields.shapes.length) {
this.updateShape(index, points);
this.fields.updateShape(index, this.transformPoints(points));
} else {
this.addShape(points);
this.fields.addShape(this.transformPoints(points));
}
};

updateShape(index: number, points: Vector2[]) {
// Transform each point from screen coordinates to field coordinates
const transformedPoints = points.map((point) => this.transformToFieldCoordinates(point));
this.fields.updateShape(index, transformedPoints);
this.renderDistanceField();
};

private transformPoints(points: Vector2[]): Vector2[] {
return points.map((point) => this.transformToFieldCoordinates(point));
}
}
37 changes: 33 additions & 4 deletions src/distanceField/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ export class Fields {
return this.colorField[x][y];
}

addShape(points: Vector2[]) {
const color = Math.floor(Math.random() * 255);
this.shapes.push({ points, color });
addShape(points: Vector2[], color?: number) {
const shapeColor = color ?? Math.floor(Math.random() * 255);
this.shapes.push({ points, color: shapeColor });
this.updateFields();
console.log(this.shapes);
}

removeShape(index: number) {
Expand Down Expand Up @@ -245,4 +244,34 @@ export class Fields {
this.updateFields();
}
}

/**
* Generates ImageData for rendering, encapsulating all computational logic.
*/
public generateImageData(): ImageData {
const imageData = new ImageData(this.resolution, this.resolution);

for (let row = 0; row < this.resolution; row++) {
for (let col = 0; col < this.resolution; col++) {
const index = (col * this.resolution + row) * 4;
const distance = this.getDistance(row, col);
const color = this.getColor(row, col);

const maxDistance = 10;
const normalizedDistance = Math.sqrt(distance) / maxDistance;
const baseColor = {
r: (color * 7) % 256,
g: (color * 13) % 256,
b: (color * 19) % 256,
};

imageData.data[index] = baseColor.r * (1 - normalizedDistance);
imageData.data[index + 1] = baseColor.g * (1 - normalizedDistance);
imageData.data[index + 2] = baseColor.b * (1 - normalizedDistance);
imageData.data[index + 3] = 255;
}
}

return imageData;
}
}

0 comments on commit a58343a

Please sign in to comment.