UNPKG

@ue-too/board

Version:
171 lines (170 loc) 7.57 kB
import { Point } from '@ue-too/math'; import { Boundaries } from './utils/position'; import { TransformationMatrix } from './utils/matrix'; import { UnSubscribe } from './update-publisher'; import { ZoomLevelLimits } from './utils/zoom'; import { RotationLimits } from './utils/rotation'; import { CameraEventMap, CameraState } from './update-publisher'; import { ObservableBoardCamera } from './interface'; import { SubscriptionOptions } from '../utils/observable'; export declare const DEFAULT_BOARD_CAMERA_VIEWPORT_WIDTH = 1000; export declare const DEFAULT_BOARD_CAMERA_VIEWPORT_HEIGHT = 1000; export declare const DEFAULT_BOARD_CAMERA_ZOOM_BOUNDARIES: ZoomLevelLimits; export declare const DEFAULT_BOARD_CAMERA_BOUNDARIES: Boundaries; export declare const DEFAULT_BOARD_CAMERA_ROTATION_BOUNDARIES: RotationLimits; /** * @description The default board camera. This is basically the same as the {@link BaseCamera} class. * But it's observable. * * @category Camera */ export default class DefaultBoardCamera implements ObservableBoardCamera { private _baseCamera; private _observer; /** * @param position The position of the camera in the world coordinate system * @param rotation The rotation of the camera in the world coordinate system * @param zoomLevel The zoom level of the camera * @param viewPortWidth The width of the viewport. (The width of the canvas in css pixels) * @param viewPortHeight The height of the viewport. (The height of the canvas in css pixels) * @param boundaries The boundaries of the camera in the world coordinate system * @param zoomLevelBoundaries The boundaries of the zoom level of the camera * @param rotationBoundaries The boundaries of the rotation of the camera */ constructor(viewPortWidth?: number, viewPortHeight?: number, position?: Point, rotation?: number, zoomLevel?: number, boundaries?: Boundaries, zoomLevelBoundaries?: ZoomLevelLimits, rotationBoundaries?: RotationLimits); /** * @description The boundaries of the camera in the world coordinate system. * * @category Camera */ get boundaries(): Boundaries | undefined; set boundaries(boundaries: Boundaries | undefined); /** * @description The width of the viewport. (The width of the canvas in css pixels) * * @category Camera */ get viewPortWidth(): number; set viewPortWidth(width: number); /** * @description The height of the viewport. (The height of the canvas in css pixels) * * @category Camera */ get viewPortHeight(): number; set viewPortHeight(height: number); /** * @description The position of the camera in the world coordinate system. * * @category Camera */ get position(): Point; /** * @description This function is used to set the position of the camera. * @param destination The destination point of the camera. * @returns Whether the position is set successfully. * * @description This function has a guard that checks if the destination point is within the boundaries of the camera. * If the destination point is not within the boundaries, the function will return false and the position will not be updated. * If the destination point is within the boundaries, the function will return true and the position will be updated. */ setPosition(destination: Point): boolean; /** * @description The zoom level of the camera. * * @category Camera */ get zoomLevel(): number; /** * @description The boundaries of the zoom level of the camera. * * @category Camera */ get zoomBoundaries(): ZoomLevelLimits | undefined; set zoomBoundaries(zoomBoundaries: ZoomLevelLimits | undefined); setMaxZoomLevel(maxZoomLevel: number): boolean; setMinZoomLevel(minZoomLevel: number): boolean; setZoomLevel(zoomLevel: number): boolean; /** * @description The rotation of the camera in the world coordinate system. * * @category Camera */ get rotation(): number; /** * @description The boundaries of the rotation of the camera. * * @category Camera */ get rotationBoundaries(): RotationLimits | undefined; set rotationBoundaries(rotationBoundaries: RotationLimits | undefined); /** * @description The order of the transformation is as follows: * 1. Scale (scale the context using the device pixel ratio) * 2. Translation (move the origin of the context to the center of the canvas) * 3. Rotation (rotate the context negatively the rotation of the camera) * 4. Zoom (scale the context using the zoom level of the camera) * 5. Translation (move the origin of the context to the position of the camera in the context coordinate system) * * @param devicePixelRatio The device pixel ratio of the canvas * @param alignCoorindate Whether to align the coordinate system to the camera's position * @returns The transformation matrix */ getTransform(devicePixelRatio: number, alignCoorindate: boolean): TransformationMatrix; /** * @description This function is used to set the rotation of the camera. * @param rotation The rotation of the camera in the world coordinate system. * @returns Whether the rotation is set successfully. */ setRotation(rotation: number): boolean; /** * @description The origin of the camera in the window coordinate system. * @deprecated * * @param centerInWindow The center of the camera in the window coordinate system. * @returns The origin of the camera in the window coordinate system. */ getCameraOriginInWindow(centerInWindow: Point): Point; /** * @description Converts a point from the viewport coordinate system to the world coordinate system. * * @param point The point in the viewport coordinate system. * @returns The point in the world coordinate system. */ convertFromViewPort2WorldSpace(point: Point): Point; /** * @description Converts a point from the world coordinate system to the viewport coordinate system. * * @param point The point in the world coordinate system. * @returns The point in the viewport coordinate system. */ convertFromWorld2ViewPort(point: Point): Point; /** * @description Inverts a point from the world coordinate system to the viewport coordinate system. * * @param point The point in the world coordinate system. * @returns The point in the viewport coordinate system. */ invertFromWorldSpace2ViewPort(point: Point): Point; setHorizontalBoundaries(min: number, max: number): void; setVerticalBoundaries(min: number, max: number): void; /** * @description This function is used to subscribe to the camera events. * @param eventName The name of the event to subscribe to. * @param callback The callback function to be called when the event is triggered. * @param options The options for the subscription. * @returns The unsubscribe function. */ on<K extends keyof CameraEventMap>(eventName: K, callback: (event: CameraEventMap[K], cameraState: CameraState) => void, options?: SubscriptionOptions): UnSubscribe; getTRS(devicePixelRatio: number, alignCoordinateSystem: boolean): { scale: { x: number; y: number; }; rotation: number; translation: { x: number; y: number; }; }; }