【问题标题】:Is there a way to find the center of a browser?有没有办法找到浏览器的中心?
【发布时间】:2021-09-09 06:08:48
【问题描述】:

我希望我的文本位于浏览器窗口的中间,有什么方法可以找到(以像素为单位)浏览器的中间?这不仅仅是 HTML 文本,所以我不能使用 CSS 或任何东西。如果您不理解我令人困惑的解释,这是我的代码的一部分:

<canvas id = "my-canvas" width = "screen.width" height = "screen.height"></canvas>
        

        <script src = "https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script>
        <script type = "application/javascript">
            // program code {
                var sketchProc = function(processingInstance) {
                    with (processingInstance) {
                        background(255);
                        fill(0);
                        text("Hi", 300, 300);
                    } 
                };
            // }
            // store canvas {
                var canvas = document.getElementById("my-canvas");
            // }
            // run Processing.js {
                var processingInstance = new Processing(canvas, sketchProc);
            // }
        </script>

【问题讨论】:

  • 您的意思是说这只是 HTML 文本吗?你说你不能使用CSS“或任何东西”,但你标记了“javascript”。你能展示你做了什么以及你期望发生什么
  • 不,我正在使用一个名为 Processing 的死 API。
  • 技术上是 HTML 格式。
  • 我编辑了我的评论以包含我的一些代码。

标签: javascript html centering


【解决方案1】:

这可能对你有用

const width = window.innerWidth / 2;
const height = window.innerHeight / 2;
text("Hi", width, height);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2013-01-06
    • 2017-08-21
    相关资源
    最近更新 更多