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

        <bdo id='7L2Ir'></bdo><ul id='7L2Ir'></ul>

        <small id='7L2Ir'></small><noframes id='7L2Ir'>

        <tfoot id='7L2Ir'></tfoot>
        <legend id='7L2Ir'><style id='7L2Ir'><dir id='7L2Ir'><q id='7L2Ir'></q></dir></style></legend>
      1. 在 HTML5 画布上绘制 X 形状并检查与边框的冲突

        时间:2023-08-02

            <tbody id='mEkhA'></tbody>
            <bdo id='mEkhA'></bdo><ul id='mEkhA'></ul>
            <legend id='mEkhA'><style id='mEkhA'><dir id='mEkhA'><q id='mEkhA'></q></dir></style></legend>

          • <small id='mEkhA'></small><noframes id='mEkhA'>

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

                  本文介绍了在 HTML5 画布上绘制 X 形状并检查与边框的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个 HTML 5 迷宫游戏.

                  I am working on a maze game in HTML 5.

                  这是我用来在画布上绘制X"的函数(使用触摸板,用户将通过迷宫导航 X).

                  Here is the function which I use to draw an "X" on the canvas (using the touchpad, the user will navigate the X through the labyrinth).

                  dim = 8;
                  
                  function rect(x,y,xdim){
                      ctx.beginPath();
                      ctx.moveTo(x - xdim, y - xdim);
                      ctx.lineTo(x + xdim, y + xdim);
                      ctx.moveTo(x + xdim, y - xdim);
                      ctx.lineTo(x - xdim, y + xdim);
                      ctx.strokeStyle = '#FF5000';
                      ctx.stroke();       
                  }
                  

                  问题出在checkcollision函数上,主要是这段代码:

                  The problem is in the checkcollision function, mainly in this piece of code:

                  ctx.getImageData(checkx, checky, 16, 16);
                  

                  我正在寻找一种最佳方式来检查画布上X"占用的所有空间.

                  我使用的代码中有更多注释.

                  There are more comments in the code i used.

                  问题:1. 有时,X 有点超出边界2. 有时,X 没有靠近,在它和边框之间留出几个像素.

                  Issues: 1. Sometimes, the X goes a little passed the border 2. Sometimes, the X doesn't get close, letting a few pixels between it and the border.

                  function checkcollision(checkx, checky){
                          var collision = 0;
                          /*
                              User presses an arrow key
                  
                              Calculate where that would move the square to.
                  
                              Look at all of the pixels on the maze image that the square will cover
                              in its new position (our square is 15x15 so we look at 225 pixels)
                  
                              Are any of the pixels black (the maze is white with black borders. 
                              If a pixel is black that means the square would collide with a border)
                  
                              YES: then do not move
                  
                              NO: then move
                          */
                          var imgd = ctx.getImageData(checkx, checky, 15, 15);
                          pix = imgd.data;
                          for (var i = 0; n = pix.length, i < n; i += 4){
                              if (pix[i] == 0) {
                                  collision = 1;
                              } else if(pix[i] == 255 && pix[i + 1] == 0){
                                  winner = "yes";
                              }
                          }
                          return collision;
                      }
                  

                  我相信 getImageData 会得到一个矩形,该矩形从角落的 x,y 位置开始.

                  I believe the getImageData gets a rectangle which starts from the x,y position in a corner.

                  所以也许有一种方法可以使用 checkx 和 checky 作为中心的坐标来绘制它并检索一个 16 像素宽的正方形

                  So maybe there is a way to draw it using the checkx and checky as the coordinates for the center and retrieve a square 16 pixels wide

                  推荐答案

                  哇,问题写的很清楚了:

                  Wow, writing the question made it clear:

                  var imgd = ctx.getImageData(checkx - xdim, checky - xdim, 16, 16);
                  

                  小伙伴们

                  这篇关于在 HTML5 画布上绘制 X 形状并检查与边框的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:javascript - Canvas 中的全屏矩形(Android/iOS 网络应用程序) 下一篇:仅转换 Path2D 的样式

                  相关文章

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

                  <tfoot id='bYRb3'></tfoot>

                  1. <i id='bYRb3'><tr id='bYRb3'><dt id='bYRb3'><q id='bYRb3'><span id='bYRb3'><b id='bYRb3'><form id='bYRb3'><ins id='bYRb3'></ins><ul id='bYRb3'></ul><sub id='bYRb3'></sub></form><legend id='bYRb3'></legend><bdo id='bYRb3'><pre id='bYRb3'><center id='bYRb3'></center></pre></bdo></b><th id='bYRb3'></th></span></q></dt></tr></i><div id='bYRb3'><tfoot id='bYRb3'></tfoot><dl id='bYRb3'><fieldset id='bYRb3'></fieldset></dl></div>
                  2. <legend id='bYRb3'><style id='bYRb3'><dir id='bYRb3'><q id='bYRb3'></q></dir></style></legend>
                      • <bdo id='bYRb3'></bdo><ul id='bYRb3'></ul>