All files / core/handlers view-target-handler.ts

100% Statements 96/96
100% Branches 13/13
100% Functions 7/7
100% Lines 96/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 11x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
import { getUUID } from '../../utils/get-uuid';
import { illusionService } from '../illusion-service';
 
 
export interface ViewTarget {
    canvasId: string;
    cTarget: CViewTarget;
}
 
/**
 * 视图目标工具类
 */
export class ViewTargetHandler {
    map: Map<string, ViewTarget>;
 
    constructor() {
        this.map = new Map();
    }
 
    public getTargetElement(id: string): HTMLCanvasElement | OffscreenCanvas {
        const { canvasId: _canvasId } = this.getViewTarget(id);
        const { module } = illusionService;
        const canvasId = _canvasId.substring(1);
        if(module.GL.offscreenCanvases[canvasId]) {
            return module.GL.offscreenCanvases[canvasId];
        }
        return document.getElementById(canvasId) as HTMLCanvasElement;
    }
 
    /**
     * 创建视图目标实例
     * @param canvasId - 视图的 Id
     * @param width - 画布宽度
     * @param height - 画布高度
     * @returns 视图目标 id
     */
    create(canvasId: string, width: number, height: number) {
        const id = getUUID();
        const cTarget = new illusionService.module.ViewTarget(canvasId);
        cTarget.setTargetSize(width, height);
        this.map.set(id, {
            cTarget,
            canvasId
        });
        return { id };
    }
 
    /**
     * 获取视图目标实例
     * @param id - 视图目标 id
     */
    getViewTarget(id: string): ViewTarget {
        if(!this.map.has(id)) {
            throw new Error(`viewTarget: ${id} is not exist`);
        }
        return this.map.get(id)!;
    }
 
    /**
     * 销毁视图目标实例
     * @param id - 视图目标 id
     */
    destory(id: string) {
        const { cTarget } = this.getViewTarget(id);
        cTarget.delete();
        this.map.delete(id);
    }
 
    /**
     * 设置渲染目标尺寸
     * @param id - 视图目标 id
     * @param width - 宽度
     * @param height - 高度
     */
    setTargetSize(id: string, width: number, height: number): void {
        const { cTarget } = this.getViewTarget(id);
        cTarget.setTargetSize(width, height);
        const canvas = this.getTargetElement(id);
        if(canvas) {
            console.log(canvas);
            canvas.width = width;
            canvas.height = height;
        }
    }
    
    /**
     * 设置是否为离屏 canvas
     * @param offscreen - 是否离屏
     */
    setOffscreen(id: string, offscreen: boolean) {
        const { cTarget } = this.getViewTarget(id);
        cTarget.setOffscreen(offscreen);
    }
}
 
export const viewTargetHandler = new ViewTargetHandler();