示例工程 35-multi-display-frameless

35-multi-display-frameless/app/.tsc/shared/display.js

javascript · 5.3 KB

export function intersectionArea(a, b) {
    const left = Math.max(a.x, b.x);
    const top = Math.max(a.y, b.y);
    const right = Math.min(a.x + a.width, b.x + b.width);
    const bottom = Math.min(a.y + a.height, b.y + b.height);
    if (right <= left || bottom <= top)
        return 0;
    return (right - left) * (bottom - top);
}
function contains(rect, point) {
    return (point.x >= rect.x &&
        point.x < rect.x + rect.width &&
        point.y >= rect.y &&
        point.y < rect.y + rect.height);
}
function distanceSquared(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;
    return dx * dx + dy * dy;
}
function center(rect) {
    return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
}
export function pickDisplay(displays, rect) {
    if (displays.length === 0)
        return null;
    let best = null;
    let bestScore = -1;
    for (const display of displays) {
        const score = intersectionArea(rect, display.bounds);
        if (score > bestScore) {
            best = display;
            bestScore = score;
        }
    }
    if (best && bestScore > 0)
        return best;
    const target = center(rect);
    let nearest = null;
    let nearestDistance = Infinity;
    for (const display of displays) {
        const distance = distanceSquared(target, center(display.bounds));
        if (distance < nearestDistance) {
            nearest = display;
            nearestDistance = distance;
        }
    }
    return nearest;
}
export function pickDisplayAtPoint(displays, point) {
    const exact = displays.find((display) => contains(display.bounds, point));
    if (exact)
        return exact;
    return pickDisplay(displays, {
        x: point.x,
        y: point.y,
        width: 1,
        height: 1,
    });
}
export function clampToWorkArea(rect, workArea, min = { width: 480, height: 360 }) {
    const width = Math.min(Math.max(rect.width, min.width), workArea.width);
    const height = Math.min(Math.max(rect.height, min.height), workArea.height);
    const maxX = workArea.x + workArea.width - width;
    const maxY = workArea.y + workArea.height - height;
    return {
        x: Math.min(Math.max(rect.x, workArea.x), maxX),
        y: Math.min(Math.max(rect.y, workArea.y), maxY),
        width,
        height,
    };
}
export function centerOn(workArea, size, min = { width: 480, height: 360 }) {
    const fitted = clampToWorkArea({
        x: workArea.x,
        y: workArea.y,
        width: size.width,
        height: size.height,
    }, workArea, min);
    return {
        ...fitted,
        x: workArea.x + Math.round((workArea.width - fitted.width) / 2),
        y: workArea.y + Math.round((workArea.height - fitted.height) / 2),
    };
}
export function placeOnDisplay(display, size, margin = 0) {
    const area = {
        x: display.workArea.x + margin,
        y: display.workArea.y + margin,
        width: Math.max(1, display.workArea.width - margin * 2),
        height: Math.max(1, display.workArea.height - margin * 2),
    };
    return centerOn(area, size);
}
export function unionRect(rects) {
    if (rects.length === 0)
        return { x: 0, y: 0, width: 1, height: 1 };
    const left = Math.min(...rects.map((rect) => rect.x));
    const top = Math.min(...rects.map((rect) => rect.y));
    const right = Math.max(...rects.map((rect) => rect.x + rect.width));
    const bottom = Math.max(...rects.map((rect) => rect.y + rect.height));
    return {
        x: left,
        y: top,
        width: Math.max(1, right - left),
        height: Math.max(1, bottom - top),
    };
}
export function mapRect(rect, union, width, height, padding = 10) {
    const scale = Math.min((width - padding * 2) / Math.max(1, union.width), (height - padding * 2) / Math.max(1, union.height));
    return {
        x: padding + (rect.x - union.x) * scale,
        y: padding + (rect.y - union.y) * scale,
        width: Math.max(2, rect.width * scale),
        height: Math.max(2, rect.height * scale),
    };
}
export function physicalSize(rect, scaleFactor) {
    return {
        width: Math.round(rect.width * scaleFactor),
        height: Math.round(rect.height * scaleFactor),
    };
}
export function simulatedDisplays() {
    return [
        {
            id: 101,
            label: "左侧外接屏(模拟)",
            primary: false,
            internal: false,
            scaleFactor: 1,
            rotation: 0,
            bounds: { x: -1920, y: -180, width: 1920, height: 1080 },
            workArea: { x: -1920, y: -150, width: 1920, height: 1000 },
            simulated: true,
        },
        {
            id: 102,
            label: "主屏(模拟,2x)",
            primary: true,
            internal: true,
            scaleFactor: 2,
            rotation: 0,
            bounds: { x: 0, y: 0, width: 1920, height: 1080 },
            workArea: { x: 0, y: 30, width: 1920, height: 1000 },
            simulated: true,
        },
        {
            id: 103,
            label: "右侧竖屏(模拟,1.5x)",
            primary: false,
            internal: false,
            scaleFactor: 1.5,
            rotation: 90,
            bounds: { x: 1920, y: -180, width: 1080, height: 1920 },
            workArea: { x: 1920, y: -150, width: 1080, height: 1840 },
            simulated: true,
        },
    ];
}
//# sourceMappingURL=display.js.map