<small id='8fYQy'></small><noframes id='8fYQy'>

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

        <bdo id='8fYQy'></bdo><ul id='8fYQy'></ul>

    1. 画布旋转后查找坐标

      时间:2023-06-20
            <tbody id='vG2Ty'></tbody>
          1. <legend id='vG2Ty'><style id='vG2Ty'><dir id='vG2Ty'><q id='vG2Ty'></q></dir></style></legend>
          2. <small id='vG2Ty'></small><noframes id='vG2Ty'>

            <tfoot id='vG2Ty'></tfoot>

              <i id='vG2Ty'><tr id='vG2Ty'><dt id='vG2Ty'><q id='vG2Ty'><span id='vG2Ty'><b id='vG2Ty'><form id='vG2Ty'><ins id='vG2Ty'></ins><ul id='vG2Ty'></ul><sub id='vG2Ty'></sub></form><legend id='vG2Ty'></legend><bdo id='vG2Ty'><pre id='vG2Ty'><center id='vG2Ty'></center></pre></bdo></b><th id='vG2Ty'></th></span></q></dt></tr></i><div id='vG2Ty'><tfoot id='vG2Ty'></tfoot><dl id='vG2Ty'><fieldset id='vG2Ty'></fieldset></dl></div>
              • <bdo id='vG2Ty'></bdo><ul id='vG2Ty'></ul>
                本文介绍了画布旋转后查找坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                0 到 0,-70 通过这个:

                0 to 0,-70 by this :

                ctx.strokeStyle = "red";
                ctx.lineWidth = 2;
                ctx.rotate(Math.PI/-10;);
                ctx.beginPath();
                ctx.moveTo(0,0); 
                ctx.lineTo(0,-70);
                ctx.stroke();
                

                我可以将其旋转PI/-10",这样就可以了.

                And I can rotate that by 'PI/-10', and that works.

                使用旋转后如何获得 x,y 点?

                How i can get the x,y points of this after using rotate?

                推荐答案

                你的 x 和 y 点仍然是 0 和 -70,因为它们是相对于平移(旋转)的.这基本上意味着您需要对矩阵进行逆向工程"以获得您在画布上看到的结果位置.

                Your x and y points will still be 0 and -70 as they are relative to the translation (rotation). It basically means you would need to "reverse engineer" the matrix to get the resulting position you see on the canvas.

                如果您想计算一条在 -10 度处延伸 70 像素的线,您可以使用简单的三角函数自行计算(这比在矩阵中倒退更容易).

                If you want to calculate a line which goes 70 pixels at -10 degrees you can use simple trigonometry to calculate it yourself instead (which is easier than going sort of backwards in the matrix).

                您可以使用这样的函数来获取您的上下文、线的起始位置 (x, y)、要绘制的线的长度(以像素为单位)和角度(以度为单位).它画线并返回一个带有 xy 的对象作为该线的终点:

                You can use a function like this that takes your context, the start position of the line (x, y) the length (in pixels) and angle (in degrees) of the line you want to draw. It draw the line and returns an object with x and y for the end point of that line:

                function lineToAngle(ctx, x1, y1, length, angle) {
                
                    angle *= Math.PI / 180;
                
                    var x2 = x1 + length * Math.cos(angle),
                        y2 = y1 + length * Math.sin(angle);
                
                    ctx.moveTo(x1, y1);
                    ctx.lineTo(x2, y2);
                    ctx.stroke();
                
                    return {x: x2, y: y2};
                }
                

                那么就叫它:

                var pos = lineToAngle(ctx, 0, 0, 70, -10);
                
                //show result of end point
                console.log('x:', pos.x.toFixed(2), 'y:', pos.y.toFixed(2));
                

                结果:

                x: 68.94 y: -12.16
                

                或者您可以通过这样做来扩展画布的上下文:

                Or you can instead extend the canvas' context by doing this:

                if (typeof CanvasRenderingContext2D !== 'undefined') {
                
                    CanvasRenderingContext2D.prototype.lineToAngle = 
                        function(x1, y1, length, angle) {
                
                            angle *= Math.PI / 180;
                
                            var x2 = x1 + length * Math.cos(angle),
                                y2 = y1 + length * Math.sin(angle);
                
                            this.moveTo(x1, y1);
                            this.lineTo(x2, y2);
                
                            return {x: x2, y: y2};
                        }
                }
                

                然后像这样直接在你的上下文中使用它:

                And then use it directly on your context like this:

                var pos = ctx.lineToAngle(100, 100, 70, -10);
                ctx.stroke(); //we stroke separately to allow this being used in a path
                
                console.log('x:', pos.x.toFixed(2), 'y:', pos.y.toFixed(2));
                

                (0 度将指向右侧).

                (0 degrees will point to the right).

                这篇关于画布旋转后查找坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:滚动到该部分时如何使 Chart.js 动画? 下一篇:HTML5 删除画布中先前绘制的对象

                相关文章

                      <bdo id='s1uTC'></bdo><ul id='s1uTC'></ul>

                    <tfoot id='s1uTC'></tfoot>
                  1. <legend id='s1uTC'><style id='s1uTC'><dir id='s1uTC'><q id='s1uTC'></q></dir></style></legend>
                  2. <small id='s1uTC'></small><noframes id='s1uTC'>

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