「Unity/WebGL/画面縦横幅拡大」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→リサイズ対応) |
(→リサイズ対応) |
||
行7: | 行7: | ||
参考:http://corevale.com/unity/1636 | 参考:http://corevale.com/unity/1636 | ||
==リサイズ対応== | ==リサイズ対応== | ||
− | canvas.style.width = " | + | <pre> |
+ | canvas.style.width = "{{{ WIDTH }}}px"; | ||
+ | canvas.style.height = "{{{ HEIGHT }}}px"; | ||
+ | <pre> | ||
+ | 上記辺りのところを以下に変更 | ||
<pre> | <pre> | ||
function loadWindow() { | function loadWindow() { | ||
canvas.style.width = "100%"; | canvas.style.width = "100%"; | ||
canvas.style.height = (document.documentElement.clientWidth * 540 / 960) + "px" | canvas.style.height = (document.documentElement.clientWidth * 540 / 960) + "px" | ||
+ | window.setTimeout(resizeWindow, 5000); | ||
+ | } | ||
+ | function loadWindow() { | ||
+ | canvas.style.width = "100%"; | ||
+ | canvas.style.height = (document.documentElement.clientWidth * {{{ HEIGHT }}} / {{{ WIDTH }}}) + "px" | ||
window.setTimeout(resizeWindow, 5000); | window.setTimeout(resizeWindow, 5000); | ||
} | } | ||
function resizeWindow() { | function resizeWindow() { | ||
− | if (document.documentElement.clientWidth / document.documentElement.clientHeight < | + | if (document.documentElement.clientWidth / document.documentElement.clientHeight < {{{ WIDTH }}} / {{{ HEIGHT }}}) { |
canvas.style.width = "100%"; | canvas.style.width = "100%"; | ||
− | canvas.style.height = (document.documentElement.clientWidth * | + | canvas.style.height = (document.documentElement.clientWidth * {{{ HEIGHT }}} / {{{ WIDTH }}}) + "px" |
} else { | } else { | ||
canvas.style.height = "100%"; | canvas.style.height = "100%"; | ||
− | canvas.style.width = ((document.documentElement.clientHeight - 20) * | + | canvas.style.width = ((document.documentElement.clientHeight - 20) * {{{ WIDTH }}} / {{{ HEIGHT }}}) + "px" |
} | } | ||
} | } | ||
+ | window.addEventListener('resize', resizeWindow); | ||
+ | window.addEventListener('load', loadWindow); | ||
window.addEventListener('resize', resizeWindow); | window.addEventListener('resize', resizeWindow); | ||
window.addEventListener('load', loadWindow); | window.addEventListener('load', loadWindow); |
2022年12月1日 (木) 15:55時点における版
WebGLで出力されてたcanvasを100%にする方法
- http://corevale.com/unity/1636 にある以下の.webgl-content * {}スタイルシートで、TemplateData/style.cssを更新
- index.htmlに記載されてる縦幅横幅を100%に変更。
- 出力されたhtmlに以下記載されてることを確認
<link rel="stylesheet" href="TemplateData/style.css">
参考:http://corevale.com/unity/1636
リサイズ対応
canvas.style.width = "{{{ WIDTH }}}px"; canvas.style.height = "{{{ HEIGHT }}}px"; <pre> 上記辺りのところを以下に変更 <pre> function loadWindow() { canvas.style.width = "100%"; canvas.style.height = (document.documentElement.clientWidth * 540 / 960) + "px" window.setTimeout(resizeWindow, 5000); } function loadWindow() { canvas.style.width = "100%"; canvas.style.height = (document.documentElement.clientWidth * {{{ HEIGHT }}} / {{{ WIDTH }}}) + "px" window.setTimeout(resizeWindow, 5000); } function resizeWindow() { if (document.documentElement.clientWidth / document.documentElement.clientHeight < {{{ WIDTH }}} / {{{ HEIGHT }}}) { canvas.style.width = "100%"; canvas.style.height = (document.documentElement.clientWidth * {{{ HEIGHT }}} / {{{ WIDTH }}}) + "px" } else { canvas.style.height = "100%"; canvas.style.width = ((document.documentElement.clientHeight - 20) * {{{ WIDTH }}} / {{{ HEIGHT }}}) + "px" } } window.addEventListener('resize', resizeWindow); window.addEventListener('load', loadWindow); window.addEventListener('resize', resizeWindow); window.addEventListener('load', loadWindow);
右に表示されてしまうのを中央表示に変更する方法
TemplateData/style.css
body { text-align: center; }