示例工程 35-multi-display-frameless

35-multi-display-frameless/demo-01-placement/placement.ts

typescript · 3.3 KB

type Rect = { x: number; y: number; width: number; height: number };

type Display = {
  id: number;
  primary: boolean;
  rotation: number;
  scaleFactor: number;
  bounds: Rect;
  workArea: Rect;
};

const displays: Display[] = [
  {
    id: 101,
    primary: false,
    rotation: 0,
    scaleFactor: 1,
    bounds: { x: -1920, y: -180, width: 1920, height: 1080 },
    workArea: { x: -1920, y: -150, width: 1920, height: 1000 },
  },
  {
    id: 102,
    primary: true,
    rotation: 0,
    scaleFactor: 2,
    bounds: { x: 0, y: 0, width: 1920, height: 1080 },
    workArea: { x: 0, y: 30, width: 1920, height: 1000 },
  },
  {
    id: 103,
    primary: false,
    rotation: 90,
    scaleFactor: 1.5,
    bounds: { x: 1920, y: -180, width: 1080, height: 1920 },
    workArea: { x: 1920, y: -150, width: 1080, height: 1840 },
  },
];

function intersection(a: Rect, b: Rect): number {
  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 pick(rect: Rect): Display {
  let best = displays[0] as Display;
  let score = -1;
  for (const display of displays) {
    const next = intersection(rect, display.bounds);
    if (next > score) {
      best = display;
      score = next;
    }
  }
  return best;
}

function clamp(rect: Rect, area: Rect): Rect {
  const width = Math.min(rect.width, area.width);
  const height = Math.min(rect.height, area.height);
  return {
    x: Math.min(Math.max(rect.x, area.x), area.x + area.width - width),
    y: Math.min(Math.max(rect.y, area.y), area.y + area.height - height),
    width,
    height,
  };
}

function center(area: Rect, size: { width: number; height: number }): Rect {
  const fitted = clamp(
    { x: area.x, y: area.y, width: size.width, height: size.height },
    area,
  );
  return {
    ...fitted,
    x: area.x + Math.round((area.width - fitted.width) / 2),
    y: area.y + Math.round((area.height - fitted.height) / 2),
  };
}

const negative = clamp(
  { x: -500, y: -300, width: 1200, height: 900 },
  { x: 0, y: 30, width: 1920, height: 1000 },
);
const overflow = clamp(
  { x: 1800, y: 1000, width: 4000, height: 3000 },
  { x: 0, y: 30, width: 1920, height: 1000 },
);
const left = pick({ x: -1000, y: 500, width: 100, height: 100 });
const portrait = pick({ x: 2400, y: 800, width: 100, height: 100 });
const centered = center(displays[2]?.workArea as Rect, {
  width: 1000,
  height: 700,
});
const physical = {
  width: (displays[1]?.bounds.width ?? 0) * (displays[1]?.scaleFactor ?? 1),
  height: (displays[1]?.bounds.height ?? 0) * (displays[1]?.scaleFactor ?? 1),
};

const ok =
  negative.x === 0 &&
  negative.y === 30 &&
  negative.width === 1200 &&
  overflow.width === 1920 &&
  overflow.height === 1000 &&
  left.id === 101 &&
  portrait.id === 103 &&
  centered.x === 1960 &&
  centered.y === 420 &&
  physical.width === 3840 &&
  physical.height === 2160;

console.log(
  JSON.stringify({
    evt: "smoke",
    ok,
    negative,
    overflow,
    left: left.id,
    portrait: portrait.id,
    centered,
    physical,
    displays: displays.map((display) => ({
      id: display.id,
      bounds: display.bounds,
      scaleFactor: display.scaleFactor,
    })),
  }),
);