d2/d2renderers/d2svg/fitToScreen.js

21 lines
633 B
JavaScript
Raw Normal View History

2023-01-03 19:27:15 +00:00
window.addEventListener("DOMContentLoaded", () => {
2023-01-03 23:01:21 +00:00
if (document.documentElement.getAttribute("id") !== "d2-svg") {
return;
}
2023-01-03 23:01:21 +00:00
const svgEl = document.documentElement;
2023-01-03 19:27:15 +00:00
let width = parseInt(svgEl.getAttribute("width"), 10);
let height = parseInt(svgEl.getAttribute("height"), 10);
let ratio;
if (width > height) {
if (width > window.innerWidth) {
ratio = window.innerWidth / width;
}
} else if (height > window.innerHeight) {
ratio = window.innerHeight / height;
}
if (ratio) {
svgEl.setAttribute("width", width * ratio - 16);
svgEl.setAttribute("height", height * ratio - 16);
}
});