<i id='3nHyS'><tr id='3nHyS'><dt id='3nHyS'><q id='3nHyS'><span id='3nHyS'><b id='3nHyS'><form id='3nHyS'><ins id='3nHyS'></ins><ul id='3nHyS'></ul><sub id='3nHyS'></sub></form><legend id='3nHyS'></legend><bdo id='3nHyS'><pre id='3nHyS'><center id='3nHyS'></center></pre></bdo></b><th id='3nHyS'></th></span></q></dt></tr></i><div id='3nHyS'><tfoot id='3nHyS'></tfoot><dl id='3nHyS'><fieldset id='3nHyS'></fieldset></dl></div>

      <bdo id='3nHyS'></bdo><ul id='3nHyS'></ul>

    <small id='3nHyS'></small><noframes id='3nHyS'>

    1. <tfoot id='3nHyS'></tfoot>
    2. <legend id='3nHyS'><style id='3nHyS'><dir id='3nHyS'><q id='3nHyS'></q></dir></style></legend>

      使用浮点数时,fillRect() 不完全重叠

      fillRect() not overlapping exactly when float numbers are used(使用浮点数时,fillRect() 不完全重叠)

            • <bdo id='38KBG'></bdo><ul id='38KBG'></ul>
              <i id='38KBG'><tr id='38KBG'><dt id='38KBG'><q id='38KBG'><span id='38KBG'><b id='38KBG'><form id='38KBG'><ins id='38KBG'></ins><ul id='38KBG'></ul><sub id='38KBG'></sub></form><legend id='38KBG'></legend><bdo id='38KBG'><pre id='38KBG'><center id='38KBG'></center></pre></bdo></b><th id='38KBG'></th></span></q></dt></tr></i><div id='38KBG'><tfoot id='38KBG'></tfoot><dl id='38KBG'><fieldset id='38KBG'></fieldset></dl></div>

              • <legend id='38KBG'><style id='38KBG'><dir id='38KBG'><q id='38KBG'></q></dir></style></legend>
                <tfoot id='38KBG'></tfoot>
                  <tbody id='38KBG'></tbody>

                <small id='38KBG'></small><noframes id='38KBG'>

                本文介绍了使用浮点数时,fillRect() 不完全重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                以下代码 (jsFiddle) 在画布上的随机点绘制一个红色方块,注意擦除前一个(通过用 ctx.fillRect() 填充一个白色方块:

                The following code (jsFiddle) draws a red square at random points on a canvas taking care to erase the previous one (by filling a white square over it with ctx.fillRect():

                <html>
                  <canvas id='canvas' width=300 height=300/>
                 <script>
                   const ctx = document.getElementById('canvas').getContext('2d');
                   let prevRect = null;
                   for (let i = 0 ; i < 10; i++) {
                     if (prevRect != null) {
                       ctx.fillStyle='white';
                       ctx.fillRect(prevRect.x, prevRect.y, 50, 50);
                     }
                     ctx.fillStyle='red';
                     const newRect = {x: Math.random()*(300-50), y: Math.random()*(300-50)};
                     ctx.fillRect(newRect.x, newRect.y, 50, 50);
                     prevRect = newRect;
                   }
                  </script>
                </html>
                

                代码未能完全擦除前一个方块,并且屏幕上仍保留有伪影.相反,如果我执行以下操作:

                The code fails to completely erase the previous square and artifacts remain on the screen. If, instead, I do the following:

                const newRect = {x: Math.floor(Math.random()*(300-50)), y: Math.floor(Math.random()*(300-50))};
                

                ...然后一切都按预期进行.

                ... then everything works as intended.

                我的问题是为什么.似乎完全没有必要截断,因为我将值保留在 prevRect 中,因此对 fillRect() 的两个调用使用完全相同的坐标(即使使用浮点数)所以这两个正方形应该总是完美对齐.

                My question is why. It seems totally unnecessary that I have to truncate as I keep the values in the prevRect so the two calls to fillRect() use exactly the same coordinates (even when using floats) and so the two squares should always perfectly align.

                推荐答案

                问题源于在画布上绘制事物的基本方式.绘制正方形时,形状的边缘会略微羽化".因此,当您在前一个红色框上绘制白色框时,红色的残余部分会渗入半透明边缘.

                The problem stems from the basic way that things are drawn on a canvas. When you draw a square, the edges of the shape are slightly "feathered". Thus when you draw the white box over the previous red box, the remnants of red bleed into the semi-transparent edge.

                如果您绘制 10 个白框而不是 1 个,问题就消失了.或者,如果你让白框可能大 0.5 像素,那可能会有所帮助.

                If you draw 10 white boxes instead of one, the problem goes away. Or if you make the white box probably 0.5 pixels larger, that would likely help.

                const ctx = document.getElementById('canvas').getContext('2d');
                   let prevRect = null;
                   for (let i = 0 ; i < 10; i++) {
                     if (prevRect != null) {
                       ctx.fillStyle='white';
                       ctx.fillRect(prevRect.x - 0.75, prevRect.y - 0.75, 51.5, 51.5);
                     }
                     ctx.fillStyle='red';
                     const newRect = {x: Math.random()*(300-50), y: Math.random()*(300-50)};
                     ctx.fillRect(newRect.x, newRect.y, 50, 50);
                     prevRect = newRect;
                   }

                body, html { padding: 0; }
                
                canvas { border: 1px solid black; }

                <canvas id=canvas height=300 width=300></canvas>

                看起来每边大 0.75 效果很好,但它肯定是画布逻辑"大小与实际屏幕大小的函数.

                Looks like 0.75 bigger on each side works pretty well, but it's certain to be a function of canvas "logical" size vs. actual screen size.

                这篇关于使用浮点数时,fillRect() 不完全重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)
                      <tbody id='iTxI5'></tbody>
                    <i id='iTxI5'><tr id='iTxI5'><dt id='iTxI5'><q id='iTxI5'><span id='iTxI5'><b id='iTxI5'><form id='iTxI5'><ins id='iTxI5'></ins><ul id='iTxI5'></ul><sub id='iTxI5'></sub></form><legend id='iTxI5'></legend><bdo id='iTxI5'><pre id='iTxI5'><center id='iTxI5'></center></pre></bdo></b><th id='iTxI5'></th></span></q></dt></tr></i><div id='iTxI5'><tfoot id='iTxI5'></tfoot><dl id='iTxI5'><fieldset id='iTxI5'></fieldset></dl></div>

                        <legend id='iTxI5'><style id='iTxI5'><dir id='iTxI5'><q id='iTxI5'></q></dir></style></legend>

                        <small id='iTxI5'></small><noframes id='iTxI5'>

                        <tfoot id='iTxI5'></tfoot>
                          <bdo id='iTxI5'></bdo><ul id='iTxI5'></ul>