Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xibosignage/xibo-layout-renderer",
"version": "1.0.35",
"version": "1.0.36",
"description": "Xibo layout renderer is a shared library that performs rendering of given layout from sources (e.g. CMS Preview, Linux Player, etc.)",
"main": "dist/xibo-layout-renderer.cjs.js",
"module": "dist/xibo-layout-renderer.esm.js",
Expand Down
48 changes: 48 additions & 0 deletions src/Modules/Layout/Layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,49 @@ export function initRenderingDOM(targetContainer: Element | null) {
}
}

/**
* Refits every layout in the screen container to the container's current size.
*
* Layouts, regions and media are sized in pixels from the scale factor worked out when the
* layout was parsed, so rather than recalculating all of them, scale the layout container
* as a whole by the ratio between that scale factor and the one the new size needs. A layout
* parsed after the resize picks up the new size by itself and needs no transform.
*/
export function rescaleLayouts() {
const $screen = document.getElementById('screen_container');
const sw = $screen?.offsetWidth || 0;
const sh = $screen?.offsetHeight || 0;

if (!$screen || sw === 0 || sh === 0) {
return;
}

$screen.querySelectorAll<HTMLDivElement>(':scope > div[data-scale-factor]').forEach(($layout) => {
const xw = Number($layout.dataset.designWidth);
const xh = Number($layout.dataset.designHeight);
const parsedScaleFactor = Number($layout.dataset.scaleFactor);

if (!xw || !xh || !parsedScaleFactor) {
return;
}

const scaleFactor = Math.min(sw / xw, sh / xh);
const sWidth = xw * scaleFactor;
const sHeight = xh * scaleFactor;

$layout.style.setProperty('left', `${Math.abs(sw - sWidth) / 2}px`);
$layout.style.setProperty('top', `${Math.abs(sh - sHeight) / 2}px`);

if (scaleFactor === parsedScaleFactor) {
$layout.style.removeProperty('transform');
$layout.style.removeProperty('transform-origin');
} else {
$layout.style.setProperty('transform-origin', '0 0');
$layout.style.setProperty('transform', `scale(${scaleFactor / parsedScaleFactor})`);
}
});
}

export async function getXlf(layoutOptions: OptionsType) {
let xlfUrl = layoutOptions.xlfUrl;
let fetchOptions: RequestInit = {};
Expand Down Expand Up @@ -534,6 +577,11 @@ export default class Layout implements ILayout {
top: ${this.offsetY}px;
`;
$layout.style.cssText = cssText;

// Kept on the element so rescaleLayouts() can refit it when the screen is resized
$layout.dataset.designWidth = `${this.xw}`;
$layout.dataset.designHeight = `${this.xh}`;
$layout.dataset.scaleFactor = `${this.scaleFactor}`;
}

if ($layout && this.zIndex !== null && this.isOverlay) {
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Layout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

export {
default,
initRenderingDOM, getXlf, getLayout,
initRenderingDOM, rescaleLayouts, getXlf, getLayout,
} from './Layout';
20 changes: 19 additions & 1 deletion src/xibo-layout-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
import { createNanoEvents } from 'nanoevents';

import Layout, { getXlf, initRenderingDOM } from './Modules/Layout';
import Layout, { getXlf, initRenderingDOM, rescaleLayouts } from './Modules/Layout';
import { ELayoutState, ILayout, initialLayout, InputLayoutType, OptionsType, } from './Types/Layout';
import { ELayoutType, initialXlr, IXlr, IXlrEvents } from './Types/XLR';
import { IMedia } from './Types/Media';
Expand All @@ -32,6 +32,8 @@ import { ConsumerPlatform, LayoutPlaybackType } from './types';
import { setLayoutIndex } from './Modules/Generators/Generators';
import { errorSummary, inputLayoutSummary, layoutListSummary, layoutSummary } from './Lib';

let rescaleListenerAdded = false;

export default function XiboLayoutRenderer(
inputLayouts: InputLayoutType[],
overlays: InputLayoutType[],
Expand Down Expand Up @@ -253,6 +255,22 @@ export default function XiboLayoutRenderer(

initRenderingDOM(previewCanvas);

// Refit what is already on screen when the window is resized, at most once per frame
if (!rescaleListenerAdded) {
rescaleListenerAdded = true;
let rescaleFrame: number | null = null;
window.addEventListener('resize', () => {
if (rescaleFrame !== null) {
return;
}

rescaleFrame = requestAnimationFrame(() => {
rescaleFrame = null;
rescaleLayouts();
});
});
}

// Prepare splash screen
splashScreen = SplashScreen(
document.querySelector('.player-preview'),
Expand Down
Loading