diff --git a/memory-viz/src/memory_model.ts b/memory-viz/src/memory_model.ts index a36d9e8..443b3af 100644 --- a/memory-viz/src/memory_model.ts +++ b/memory-viz/src/memory_model.ts @@ -11,12 +11,15 @@ import { Rect, Primitive, Style, + DisplaySettings, + Size, } from "./types"; import { isArrayOfType } from "./typeguards"; import { RoughSVG } from "roughjs/bin/svg"; import { Config, Options } from "roughjs/bin/core"; import type * as fsType from "fs"; import type * as CSS from "csstype"; +import { getSize } from "./automate"; // Dynamic import of Node fs module let fs: typeof fsType | undefined; @@ -950,4 +953,53 @@ export class MemoryModel { return sizes_arr; } + + /** + * Return the dimension of the canvas. + * @param configuration: The configuration of the canvas. + * @param snapshotObjects: The objects that will be on the canvas. + */ + static getCanvasDimensions( + configuration: Partial, + snapshotObjects: DrawnEntity[] + ): Size { + // Dynamically determining the width of the canvas, in case one has not been provided. + const size: Size = { + width: configuration.width, + height: configuration.height, + }; + if (!configuration.hasOwnProperty("width")) { + let rightmost_obj; + let rightmost_edge = 0; + + for (const obj of snapshotObjects) { + const width = getSize(obj).width; + const curr_edge = obj.x + width; + if (curr_edge > rightmost_edge) { + rightmost_edge = curr_edge; + rightmost_obj = obj; + } + } + size.width = rightmost_edge + 100; + } + + // Dynamically determining the height of the canvas, in case one has not been provided. + if (!configuration.hasOwnProperty("height")) { + let downmost_obj = snapshotObjects[0]; + let downmost_edge = 0; + + for (const obj of snapshotObjects) { + const height = getSize(obj).height; + const curr_edge = obj.y + height; + + if (curr_edge > downmost_edge) { + downmost_obj = obj; + downmost_edge = obj.y + height; + } + } + + size.height = downmost_edge + 100; + } + return size; + } } diff --git a/memory-viz/src/user_functions.ts b/memory-viz/src/user_functions.ts index 60f3bc1..9e803a8 100644 --- a/memory-viz/src/user_functions.ts +++ b/memory-viz/src/user_functions.ts @@ -1,5 +1,5 @@ import { MemoryModel } from "./memory_model"; -import { drawAutomated, getSize } from "./automate"; +import { drawAutomated } from "./automate"; import { DrawnEntity, DisplaySettings } from "./types"; import type * as fsType from "fs"; export * from "./types"; @@ -59,7 +59,8 @@ function draw( const isArrayOfArrays = Array.isArray(objs) && Array.isArray(objs[0]); const processSnapshot = (snapshotObjects: DrawnEntity[]) => { - getCanvasDimensions(configuration, snapshotObjects); + ({ width: configuration.width, height: configuration.height } = + MemoryModel.getCanvasDimensions(configuration, snapshotObjects)); const model = new MemoryModel({ width: configuration.width, height: configuration.height, @@ -88,43 +89,4 @@ function draw( : processSnapshot(snapshotObjects); } -function getCanvasDimensions( - configuration: Partial, - snapshotObjects: DrawnEntity[] -): void { - // Dynamically determining the width of the canvas, in case one has not been provided. - if (!configuration.hasOwnProperty("width")) { - let rightmost_obj; - let rightmost_edge = 0; - - for (const obj of snapshotObjects) { - const width = getSize(obj).width; - const curr_edge = obj.x + width; - if (curr_edge > rightmost_edge) { - rightmost_edge = curr_edge; - rightmost_obj = obj; - } - } - configuration.width = rightmost_edge + 100; - } - - // Dynamically determining the height of the canvas, in case one has not been provided. - if (!configuration.hasOwnProperty("height")) { - let downmost_obj = snapshotObjects[0]; - let downmost_edge = 0; - - for (const obj of snapshotObjects) { - const height = getSize(obj).height; - const curr_edge = obj.y + height; - - if (curr_edge > downmost_edge) { - downmost_obj = obj; - downmost_edge = obj.y + height; - } - } - - configuration.height = downmost_edge + 100; - } -} - export { draw };