Skip to content
Snippets Groups Projects
Commit 8950ff83 authored by Sebastian Mohr's avatar Sebastian Mohr
Browse files

Allow to render grid from data. Seems like we dont need to compute the

grid our-self.
parent 220678f1
No related branches found
No related tags found
No related merge requests found
Pipeline #591243 skipped
...@@ -190,64 +190,84 @@ export class StampySnip extends BaseSnip { ...@@ -190,64 +190,84 @@ export class StampySnip extends BaseSnip {
return this.stampy.measurement.volume; return this.stampy.measurement.volume;
} }
get grid() {
return this.stampy.measurement.grid;
}
// TODO: Make this dynamic // TODO: Make this dynamic
get width(): number { get width(): number {
return 700; return 400;
} }
get height(): number { get height(): number {
return 700; return 400;
} }
private computeScale(): number { /** Compute the scale such that
// compute scale from given data to fit into * the max values fits inside
// the width and height of the canvas * the width and height
// TODO: Implement this */
return 150; private computeScale(width: number, height: number, max: number): number {
return Math.min(width, height) / max;
} }
public render(ctx: RenderContext): void { public render(ctx: RenderContext): void {
// Apply translate, rotate and mirror
// see base.ts
const transform = ctx.getTransform();
this.transform(ctx);
this.renderGrid(ctx);
ctx.setTransform(transform);
}
public renderGrid(ctx: RenderContext): void {
const gridWidth = 500;
const gridHeight = 500;
// Draw rect around the area // Draw rect around the area
ctx.rect(0, 0, this.width, this.height); ctx.rect(0, 0, gridWidth, gridHeight);
ctx.stroke(); ctx.stroke();
// Render sample circle ctx.translate(gridHeight / 2, gridHeight / 2);
const s = this.computeScale(); const s = this.computeScale(
this.drawCircle( gridWidth,
ctx, gridHeight,
this.width / 2, this.volume.diameter * 2.5,
this.height / 2, );
(this.volume.diameter / 2) * s,
{ // Render grid
const r = (this.volume.fovw * s) / 2;
const positions = this.grid.values;
for (let i = 0; i < positions.length; i++) {
const x = positions[i]![0]!;
const y = positions[i]![1]!;
this.drawCircle(ctx, x * s, y * s, r, {
stroke: "#000", stroke: "#000",
fill: "red", fill: "#000",
fill_alpha: 0.2, fill_alpha: 0.2,
}, });
); }
// Render sample circle
this.drawCircle(ctx, 0, 0, (this.volume.diameter / 2) * s, {
stroke: "#000",
fill: "red",
fill_alpha: 0.2,
});
// Render scale // Render scale
this.drawScale(ctx, s); this.drawScale(ctx, s, gridWidth / 2, gridHeight / 2);
// Render grid points
const grid = new HexagonalGrid(
0,
0,
0,
50,
this.width / 2,
this.height / 2,
);
for (let i = -1; i < 2; i++) { // Label
for (let j = -1; j < 2; j++) { ctx.font = "bold 18px Arial";
const { x, y } = grid.axialToPixel(i, j); ctx.fillStyle = "#000";
this.drawCircle(ctx, x, y, 2, { ctx.textBaseline = "top";
stroke: "#000", ctx.fillText(
fill: "#000", `Grid (${this.grid.method})`,
}); -gridWidth / 2 + 5,
const txt = `${i}, ${j}`; -gridHeight / 2 + 5,
ctx.fillText(txt, x + 5, y + 5); );
}
}
} }
private drawCircle( private drawCircle(
...@@ -306,25 +326,31 @@ export class StampySnip extends BaseSnip { ...@@ -306,25 +326,31 @@ export class StampySnip extends BaseSnip {
* If the scale is too small, the scale will be multiplied until it is * If the scale is too small, the scale will be multiplied until it is
* at least 150px. * at least 150px.
*/ */
private drawScale(ctx: RenderContext, scale: number) { private drawScale(
ctx: RenderContext,
scale: number,
x: number = 0,
y: number = 0,
padding: number = 10,
) {
ctx.save();
let mm = 1; let mm = 1;
let s = scale; let s = scale;
while (s < 150) { while (s < 50) {
mm *= 2; mm *= 2;
s *= 2; s *= 2;
} }
// pos // pos
const padding = 10;
const rectHeight = 5; const rectHeight = 5;
const x = this.width - s - padding; const _x = x - s - padding;
const y = this.height - rectHeight - padding - 18; //18=font size const _y = y - rectHeight - padding - 18; //18=font size
// Render scale rect // Render scale rect
ctx.globalAlpha = 0.8; ctx.globalAlpha = 0.8;
ctx.fillStyle = "#000"; ctx.fillStyle = "#000";
ctx.beginPath(); ctx.beginPath();
ctx.fillRect(x, y, s, rectHeight); ctx.fillRect(_x, _y, s, rectHeight);
ctx.stroke(); ctx.stroke();
// Render text // Render text
...@@ -333,7 +359,8 @@ export class StampySnip extends BaseSnip { ...@@ -333,7 +359,8 @@ export class StampySnip extends BaseSnip {
ctx.font = "18px Arial"; ctx.font = "18px Arial";
ctx.textBaseline = "top"; ctx.textBaseline = "top";
const ts = ctx.measureText(`${mm} mm`); const ts = ctx.measureText(`${mm} mm`);
ctx.fillText(`${mm} mm`, x + s / 2 - ts.width / 2, y + rectHeight); ctx.fillText(`${mm} mm`, _x + s / 2 - ts.width / 2, _y + rectHeight);
ctx.restore();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment