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

    1. <tfoot id='KFG47'></tfoot>

        <bdo id='KFG47'></bdo><ul id='KFG47'></ul>
    2. <small id='KFG47'></small><noframes id='KFG47'>

      <legend id='KFG47'><style id='KFG47'><dir id='KFG47'><q id='KFG47'></q></dir></style></legend>
    3. 如何检查鼠标单击是否在 JavaScript 中 HTML5 Canvas 上的旋转文本内?

      How do I check if a mouse click is inside a rotated text on the HTML5 Canvas in JavaScript?(如何检查鼠标单击是否在 JavaScript 中 HTML5 Canvas 上的旋转文本内?)
      <legend id='MQnhT'><style id='MQnhT'><dir id='MQnhT'><q id='MQnhT'></q></dir></style></legend>
      <i id='MQnhT'><tr id='MQnhT'><dt id='MQnhT'><q id='MQnhT'><span id='MQnhT'><b id='MQnhT'><form id='MQnhT'><ins id='MQnhT'></ins><ul id='MQnhT'></ul><sub id='MQnhT'></sub></form><legend id='MQnhT'></legend><bdo id='MQnhT'><pre id='MQnhT'><center id='MQnhT'></center></pre></bdo></b><th id='MQnhT'></th></span></q></dt></tr></i><div id='MQnhT'><tfoot id='MQnhT'></tfoot><dl id='MQnhT'><fieldset id='MQnhT'></fieldset></dl></div>

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

          <tfoot id='MQnhT'></tfoot>

                <bdo id='MQnhT'></bdo><ul id='MQnhT'></ul>
                  <tbody id='MQnhT'></tbody>
                本文介绍了如何检查鼠标单击是否在 JavaScript 中 HTML5 Canvas 上的旋转文本内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在画布上以 X、Y 坐标绘制了一个文本并保存了它们.我有一个简单的方法来检查是否在文本边界内发生了鼠标单击.问题是当我将文本旋转 45 度时,我无法检查旋转后的文本中是否发生了鼠标点击.

                I have drawn a text on the canvas in coordinates X, Y and saved them. I have a simple method that checks if a mouse click has happened inside the text boundaries. The problem is when I rotate the text 45 Degree I cannot check whether a mouse click has happened within the rotated text or not.

                简而言之,如何检查鼠标点击是否在旋转的文本或形状内?

                In short, how can I check whether a mouse click is inside a rotated text or shape?

                推荐答案

                创建一个与文本同角度旋转的矩形对象,但不绘制.

                Create a rect object which is rotated at the same angle as the text, but not drawn.

                然后使用:

                // set transforms here.
                // add rect representing text region:
                ctx.beginPath();
                ctx.rect(x, y, w, h);  // region for text
                if (ctx.isPointInPath(mx, my)) {
                    // we clicked inside the region
                }
                ctx.setTransform(1,0,0,1,0,0);  // reset transforms after test
                

                演示:

                var canvas = document.querySelector("canvas"),
                    ctx = canvas.getContext("2d"),
                    txt = "ROTATED TEXT", tw, region;
                
                // transform and draw some rotated text:
                ctx.textAlign = "center";
                ctx.textBaseline = "middle";
                ctx.font = "32px sans-serif";
                ctx.translate(150, 75);
                ctx.rotate(0.33);
                ctx.translate(-150, -75);
                ctx.fillText(txt, 150, 75);
                tw = ctx.measureText(txt).width;
                
                // define a region for text:
                region = {x: 150 - tw*0.5, y: 75 - 16, w: tw, h:32}; // approx. text region
                
                // function to check if mouse x/y is inside (transformed) region
                function isInText(region, x, y) {
                  ctx.beginPath();
                  ctx.rect(region.x, region.y, region.w, region.h);
                  return ctx.isPointInPath(x, y);
                }
                
                // test for demo
                canvas.onmousemove = function(e) {
                  var rect = canvas.getBoundingClientRect(),
                      x = e.clientX - rect.left,
                      y = e.clientY - rect.top;
                  
                  // just to visualize:
                  ctx.clearRect(0,0,300,150);
                  ctx.fillStyle = isInText(region, x, y) ? "red" : "black";
                  ctx.fillText(txt, 150, 75);
                };

                <canvas></canvas>

                这篇关于如何检查鼠标单击是否在 JavaScript 中 HTML5 Canvas 上的旋转文本内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 标头)
                What is quot;X-Content-Type-Options=nosniffquot;?(什么是“X-Content-Type-Options=nosniff?)
                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 调用,良好实践)

                1. <tfoot id='z5c2y'></tfoot>

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

                        <tbody id='z5c2y'></tbody>

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

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