Skip to content

Commit

Permalink
fixed grid tool config with grid's pattern dimension (#394)
Browse files Browse the repository at this point in the history
* fixed grid tool config with grid's pattern dimension

* Update docs

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 3412818 commit 25ac493
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 46 deletions.
2 changes: 1 addition & 1 deletion dist/imaging/tools/custom/gridTool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export declare class GridTool extends BaseTool {
* @returns {Promise<void>}
*/
activeCallback(element: HTMLElement): Promise<void>;
triggerInputPatternDimensionChange(event: any): void;
triggerInputGridDimensionChange(event: any): void;
/**
* function triggered when tool is set to disabled
*
Expand Down
2 changes: 1 addition & 1 deletion dist/larvitar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/larvitar.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/examples/larvitar.js

Large diffs are not rendered by default.

58 changes: 18 additions & 40 deletions imaging/tools/custom/gridTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ import { handleElement } from "./utils/gridToolUtils/gridToolUtils";
import { Coords, GridConfig, MeasurementMouseEvent } from "../types";
import { DEFAULT_TOOLS } from "../default";

const defaultGridSize = 50; // Pattern squares default size (5cm)
const defaultConfig = {
minRows: 500,
minColumns: 500,
dashHeightMM: 2, // Dash default height in mm
dashWidthMM: 10, // Dash default width in mm
colorFractionLight: 2 / 3, // Fraction of lightGray color
colorFractionDark: 1 / 3, // Fraction of darkGray color
minPixelSpacing: 0.1
};
/**
* @public
* @class GridTool
Expand All @@ -48,19 +38,14 @@ export class GridTool extends BaseTool {
super({
name: "GridTool",
configuration: {
setup:
(DEFAULT_TOOLS["Grid"].configuration as GridConfig).setup ??
defaultConfig,
patternDimension:
(DEFAULT_TOOLS["Grid"].configuration as GridConfig)
.patternDimension ?? defaultGridSize,
setup: (DEFAULT_TOOLS["Grid"].configuration as GridConfig).setup,
gridPixelArray: []
},
...props
});
this.handleMouseClick = this.handleMouseClick.bind(this);
this.triggerInputPatternDimensionChange =
this.triggerInputPatternDimensionChange.bind(this);
this.triggerInputGridDimensionChange =
this.triggerInputGridDimensionChange.bind(this);
}

/**
Expand All @@ -75,11 +60,7 @@ export class GridTool extends BaseTool {
const enabledElement = await handleElement(element);
element.addEventListener(EVENTS.MOUSE_CLICK, this.handleMouseClick);
const newSetup = (DEFAULT_TOOLS["Grid"].configuration as GridConfig).setup;
const newGridSize = (DEFAULT_TOOLS["Grid"].configuration as GridConfig)
.patternDimension;
this.configuration.setup = newSetup ?? defaultConfig;
this.configuration.patternDimension = newGridSize ?? defaultGridSize;

this.configuration.setup = newSetup;
if (
enabledElement &&
enabledElement.image.rows >= this.configuration.setup.minRows &&
Expand All @@ -88,15 +69,15 @@ export class GridTool extends BaseTool {
this.enabledElement = enabledElement;
this.triggerDrawGrid(enabledElement);
}
const buttonPatternDimension = document.getElementById("patternDimension");
buttonPatternDimension?.addEventListener(
const buttonGridDimension = document.getElementById("patternDimension");
buttonGridDimension?.addEventListener(
"input",
this.triggerInputPatternDimensionChange
this.triggerInputGridDimensionChange
);
}

triggerInputPatternDimensionChange(event: any) {
(DEFAULT_TOOLS["Grid"].configuration as GridConfig).patternDimension =
triggerInputGridDimensionChange(event: any) {
(DEFAULT_TOOLS["Grid"].configuration as GridConfig).setup.gridDimensionMM =
event.target.value;
updateImage(this.enabledElement.element);
}
Expand All @@ -110,10 +91,10 @@ export class GridTool extends BaseTool {
*/
disabledCallback(element: HTMLElement) {
element.removeEventListener(EVENTS.MOUSE_CLICK, this.handleMouseClick);
const buttonPatternDimension = document.getElementById("patternDimension");
buttonPatternDimension?.removeEventListener(
const buttonGridDimension = document.getElementById("gridDimension");
buttonGridDimension?.removeEventListener(
"input",
this.triggerInputPatternDimensionChange
this.triggerInputGridDimensionChange
);
}

Expand Down Expand Up @@ -164,10 +145,7 @@ export class GridTool extends BaseTool {
triggerDrawGrid(enabledElement: EnabledElement) {
this.configuration.gridPixelArray = [];
const newSetup = (DEFAULT_TOOLS["Grid"].configuration as GridConfig).setup;
const newGridSize = (DEFAULT_TOOLS["Grid"].configuration as GridConfig)
.patternDimension;
if (newSetup) this.configuration.setup = newSetup;
if (newGridSize) this.configuration.patternDimension = newGridSize;

const image = enabledElement.image as Image;
const pixelSpacing = {
Expand Down Expand Up @@ -199,12 +177,12 @@ export class GridTool extends BaseTool {
this.configuration.setup.colorFractionDark
);

let patternHeight = this.configuration.patternDimension
? mmToPixels(this.configuration.patternDimension, pixelSpacing.y)
: mmToPixels(this.configuration.patternDimension, pixelSpacing.y);
let patternWidth = this.configuration.patternDimension
? mmToPixels(this.configuration.patternDimension, pixelSpacing.x)
: mmToPixels(this.configuration.patternDimension, pixelSpacing.x);
let patternHeight = this.configuration.setup.gridDimensionMM
? mmToPixels(this.configuration.setup.gridDimensionMM, pixelSpacing.y)
: mmToPixels(this.configuration.setup.gridDimensionMM, pixelSpacing.y);
let patternWidth = this.configuration.setup.gridDimensionMM
? mmToPixels(this.configuration.setup.gridDimensionMM, pixelSpacing.x)
: mmToPixels(this.configuration.setup.gridDimensionMM, pixelSpacing.x);
const patternCanvasDimensions = convertDimensionsToCanvas(
element,
patternWidth,
Expand Down
12 changes: 10 additions & 2 deletions imaging/tools/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,16 @@ const DEFAULT_TOOLS: {
name: "Grid",
viewports: "all",
configuration: {
setup: null,
patternDimension: null,
setup: {
minRows: 500,
minColumns: 500,
dashHeightMM: 2, // Dash default height in mm
dashWidthMM: 10, // Dash default width in mm
colorFractionLight: 2 / 3, // Fraction of lightGray color
colorFractionDark: 1 / 3, // Fraction of darkGray color
minPixelSpacing: 0.1,
gridDimensionMM: 50 // Pattern squares default size (5cm)
},
gridPixelArray: []
},
options: {},
Expand Down
1 change: 1 addition & 0 deletions imaging/tools/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type gridSettings = {
colorFractionLight: number;
colorFractionDark: number;
minPixelSpacing: number;
gridDimensionMM: number;
};

export type ToolConfig = {
Expand Down

0 comments on commit 25ac493

Please sign in to comment.