@samatawy/diagrams
    Preparing search index...

    Class CoordinateSystem

    CoordinateSystem is a utility class that manages the transformation between diagram coordinates and canvas coordinates, including handling zooming, panning, and grid snapping. It provides methods to convert points between the diagram's coordinate system and the canvas coordinate system, as well as methods to apply the current viewport transformation to a canvas context. The class also includes functionality to snap points to a grid if the grid is forced, and to calculate bounding rectangles for nodes based on their geometry and rotation.

    Index

    Constructors

    • Creates an instance of CoordinateSystem and attaches it to a canvas rendering context. The CoordinateSystem will use this context to apply transformations and perform hit testing.

      Parameters

      • context: CanvasRenderingContext2D

        the CanvasRenderingContext2D to attach to the CoordinateSystem

      Returns CoordinateSystem

    Accessors

    Methods

    • Attaches a diagram to the CoordinateSystem, allowing it to access diagram-level information such as the grid.

      Parameters

      • diagram: IDiagram

        the IDiagram to attach to the CoordinateSystem

      Returns void

    • Applies the current viewport transformation to the specified canvas context, including zooming and panning.

      Parameters

      • context: CanvasRenderingContext2D = ...

        the CanvasRenderingContext2D to apply the transformation to

      Returns void

    • Resets the transformation of the specified canvas context to the identity matrix, effectively removing any zooming or panning.

      Parameters

      • context: CanvasRenderingContext2D = ...

        the CanvasRenderingContext2D to reset the transformation for

      Returns void

    • Converts the specified coordinates to the nearest grid point, if a grid is defined and forced.

      Parameters

      • x: number

        the x-coordinate to convert

      • y: number

        the y-coordinate to convert

      • Optionalgrid: IGrid

        an optional grid to use for snapping, defaults to the diagram's grid if not provided

      Returns IPoint

      the nearest grid point

    • Converts the specified canvas coordinates to diagram coordinates, applying the current zoom and pan transformations, and optionally snapping to the grid.

      Parameters

      • x: number

        the x-coordinate in canvas space

      • y: number

        the y-coordinate in canvas space

      • Optionalgrid: IGrid | "ignore_grid"

        an optional grid to use for snapping, defaults to the diagram's grid if not provided

      Returns IPoint

      the corresponding point in diagram coordinates

    • Converts the specified pointer event coordinates to diagram coordinates, applying the current zoom and pan transformations, and optionally snapping to the grid.

      Parameters

      • event: PointerEvent

        the PointerEvent to convert

      • Optionalgrid: IGrid | "ignore_grid"

        an optional grid to use for snapping, defaults to the diagram's grid if not provided

      Returns IPoint

      the corresponding point in diagram coordinates

    • Converts the specified point from canvas coordinates to diagram coordinates, applying the current zoom and pan transformations, and optionally snapping to the grid.

      Parameters

      • pt: IPoint

        the point in canvas coordinates to convert

      • rect: IRect

        the bounding rectangle of the shape

      • angle: number

        the rotation angle of the shape

      • Optionalcos: number

        optional precomputed cosine of the angle

      • Optionalsin: number

        optional precomputed sine of the angle

      Returns IPoint

      the corresponding point in diagram coordinates

    • Converts the specified point from diagram coordinates to canvas coordinates, applying the current zoom and pan transformations.

      Parameters

      • pt: IPoint

        the point in diagram coordinates to convert

      • rect: IRect

        the bounding rectangle of the shape

      • angle: number

        the rotation angle of the shape

      • Optionalcos: number

        optional precomputed cosine of the angle

      • Optionalsin: number

        optional precomputed sine of the angle

      Returns IPoint

      the corresponding point in canvas coordinates

    • Calculates the bounding rectangle for a given node, taking into account its geometry and rotation. If the node has multiple points, it calculates the bounding rectangle defined by the top-left and bottom-right points. If the node is rotated, it calculates the bounding rectangle after rotation to ensure that it encompasses the entire shape.

      Parameters

      • node: INode

        the INode for which to calculate the bounding rectangle

      • OptionalwithAngle: boolean

        whether to take the node's rotation angle into account when calculating the bounding rectangle.

      Returns IRect

      the calculated bounding rectangle

    • Detects whether a given point (x, y) is within the stroke of a specified path, taking into account an optional line width. This method is useful for hit testing when determining if a user interaction (like a mouse click) occurred on the border of a shape.

      Parameters

      • path: Path2D

        the Path2D object representing the shape's stroke

      • x: number

        the x-coordinate of the point to test

      • y: number

        the y-coordinate of the point to test

      • OptionallineWidth: number

        optional line width to consider for the stroke

      Returns boolean

      true if the point is within the stroke, false otherwise

    • Detects whether a given point (x, y) is within the path of a specified shape. This method is useful for hit testing when determining if a user interaction (like a mouse click) occurred inside a shape.

      Parameters

      • path: Path2D

        the Path2D object representing the shape's path

      • x: number

        the x-coordinate of the point to test

      • y: number

        the y-coordinate of the point to test

      Returns boolean

      true if the point is within the path, false otherwise