Skip to content

Instantly share code, notes, and snippets.

@kuniakisuzuki
Created September 26, 2019 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuniakisuzuki/772e8538a79246d1977c1887a25d035c to your computer and use it in GitHub Desktop.
Save kuniakisuzuki/772e8538a79246d1977c1887a25d035c to your computer and use it in GitHub Desktop.
electron用、windowサイズにコンテンツをフィットさせるjs
function setFrameSize () {
var container = document.querySelector('body')
// windowのサイズ
var windowHeight = window.innerHeight
var windowWidth = window.innerWidth
// windowの高さ/幅比
var windowRatio = windowHeight/ windowWidth
// 画面のデフォルトサイズ
var canvasDefaultWidth = 1920
var canvasDefaultHeight = 1080
// 画面の高さ/幅比
var canvasRatio = canvasDefaultHeight / canvasDefaultWidth
// 幅の拡縮比
var widthScaleRatio = (windowWidth / canvasDefaultWidth)
// 高さの拡縮比
var heightScaleRatio = (windowHeight / canvasDefaultHeight)
// 画面比が縦長の時
if (windowRatio > canvasRatio) {
// 幅比を基準に拡縮
container.style.transform = 'scale(' + widthScaleRatio + ')'
//container.style.zoom = widthScaleRatio;//こっちでもいいと思われる
container.style.transformOrigin = 'top left'
container.style.marginLeft = "inherit"
container.style.marginRight = "inherit"
var offset = (windowHeight - canvasDefaultHeight * widthScaleRatio) / 2;
container.style.marginTop = offset + "px"
} else {
container.style.transform = 'scale(' + heightScaleRatio + ')'
//container.style.zoom = heightScaleRatio;//こっちでもいいと思われる
container.style.transformOrigin = 'top left'
container.style.marginLeft = "inherit"
container.style.marginRight = "inherit"
var offset = (windowWidth - canvasDefaultWidth * heightScaleRatio) / 2;
container.style.marginLeft = offset + "px"
}
container.style.visibility = "visible";
}
window.addEventListener('load', setFrameSize)
window.addEventListener('resize', setFrameSize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment