久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='03X8F'></bdo><ul id='03X8F'></ul>

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

        <small id='03X8F'></small><noframes id='03X8F'>

      1. <legend id='03X8F'><style id='03X8F'><dir id='03X8F'><q id='03X8F'></q></dir></style></legend>

      2. 如何在鼠標移動事件后圍繞其中心旋轉 Canvas 對象

        How to rotate a Canvas object around its center following mouse move event?(如何在鼠標移動事件后圍繞其中心旋轉 Canvas 對象?)

          <tfoot id='L3oyp'></tfoot>
            <tbody id='L3oyp'></tbody>
          <legend id='L3oyp'><style id='L3oyp'><dir id='L3oyp'><q id='L3oyp'></q></dir></style></legend>
            <bdo id='L3oyp'></bdo><ul id='L3oyp'></ul>
            • <small id='L3oyp'></small><noframes id='L3oyp'>

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

                • 本文介紹了如何在鼠標移動事件后圍繞其中心旋轉 Canvas 對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  您好,當我移動鼠標時,我想圍繞它的中心旋轉這個形狀,但目前它正在圍繞 (0, 0) 旋轉.如何更改我的代碼?

                  Hi I want to rotate this shape around its center when I move my mouse, but currently it's rotating around (0, 0). How to change my code?

                  源代碼(另見jsfiddle):

                  var canvas = document.getElementById('canvas');
                  var ctx = canvas.getContext('2d');
                  canvas.width = window.innerWidth;
                  canvas.height = window.innerHeight;
                  
                  class Circle {
                      constructor(options) {
                      this.cx = options.x;
                      this.cy = options.y;
                      this.radius = options.radius;
                      this.color = options.color;
                      this.angle = 0;
                      this.toAngle = this.angle;
                  
                      this.binding();
                    }
                  
                    binding() {
                      const self = this;
                      window.addEventListener('mousemove', (e) => {
                          self.update(e.clientX, e.clientY);
                      });
                    }
                  
                    update(nx, ny) {
                      this.toAngle = Math.atan2(ny - this.cy, nx - this.cx);
                    }
                  
                    render() {
                      ctx.clearRect(0,0,canvas.width,canvas.height);
                  
                      ctx.save();
                  
                      ctx.beginPath();
                  
                      ctx.lineWidth = 1;
                  
                      if (this.toAngle !== this.angle) {
                        ctx.rotate(this.toAngle - this.angle);
                      }
                  
                      ctx.strokeStyle = this.color;
                      ctx.arc(this.cx, this.cy, this.radius, 0, Math.PI * 2);
                  
                      ctx.stroke();
                      ctx.closePath();
                  
                      ctx.beginPath();
                  
                      ctx.fillStyle = 'black';
                      ctx.fillRect(this.cx - this.radius / 4, this.cy - this.radius / 4, 20, 20);
                  
                      ctx.closePath();
                      ctx.restore();
                    }
                  }
                  
                  var rotatingCircle = new Circle({
                    x: 150,
                    y: 100,
                    radius: 40,
                    color: 'black'
                  });
                  
                  function animate() {
                      rotatingCircle.render();
                      requestAnimationFrame(animate);
                  }
                  
                  animate();
                  

                  推薦答案

                  你需要:

                  • 首先平移到旋轉點(樞軸)
                  • 然后旋轉
                  • 然后:
                    • A:在 (0,0) 處繪制,使用 (-width/2, -height/2) 作為相對坐標(用于居中繪圖)
                    • B:向后平移并使用對象的絕對位置并減去相對坐標進行居中繪圖

                    修改后的代碼:

                    ctx.beginPath();
                    ctx.lineWidth = 1;
                    
                    ctx.translate(this.cx, this.cy);               // translate to pivot
                    
                    if (this.toAngle !== this.angle) {
                      ctx.rotate(this.toAngle - this.angle);
                    }
                    
                    ctx.strokeStyle = this.color;
                    ctx.arc(0, 0, this.radius, 0, Math.PI * 2);    // render at pivot
                    
                    ctx.closePath();                               // must come before stroke() btw.
                    ctx.stroke();
                    
                    ctx.beginPath();
                    ctx.fillStyle = 'black';
                    ctx.fillRect(-this.radius / 4, -this.radius / 4, 20, 20);  // render at pivot
                    

                    改良小提琴

                    額外提示:您當前正在使用 save()/restore() 調用來維護轉換矩陣.另一種方法是使用最初替換 save()/restore() 的絕對值來設置矩陣 - 所以而不是第一個 translate():

                    Bonus tip: you're currently using save()/restore() calls to maintain the transformation matrix. Another way could be to set the matrix using absolute values initially replacing the save()/restore() - so instead of the first translate():

                    ctx.setTranform(1,0,0,1,this.cx, this.cy);    // translate to pivot
                    

                    您還可以為每個設置單獨設置樣式等內容.無論如何,它不會改變核心解決方案.

                    You can also set things like styles on an individual basis for each. Regardless, it doesn't change the core solution though.

                    這篇關于如何在鼠標移動事件后圍繞其中心旋轉 Canvas 對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)

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

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

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

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

                          • 主站蜘蛛池模板: 精品国产乱码久久久久久牛牛 | 在线观看第一区 | 国产亚洲一级 | 色综合久久天天综合网 | 中文字幕精品一区 | 麻豆av网站| 国产一区二区三区精品久久久 | 超碰精品在线观看 | 亚洲一区在线日韩在线深爱 | 成年人网站在线观看视频 | 欧美国产免费 | 欧美韩一区二区 | 国产精品麻| 在线三级网址 | 国产精品国产三级国产a | 黄色网址在线播放 | 不卡一区二区三区四区 | 国产在线一区二区 | 久草资源在线视频 | 亚洲国产专区 | 天天干天天玩天天操 | 久久精品免费 | 国产精品福利视频 | 日日夜夜天天 | 欧美日韩高清一区二区三区 | 一区二区av| 色一情一乱一伦一区二区三区 | 欧美一级高潮片免费的 | 91国内产香蕉| 福利一区二区 | 日韩乱码一二三 | 欧美激情综合五月色丁香小说 | 婷婷丁香在线视频 | 精品国产一级 | 美女久久久久久久 | 欧美黄色一区 | 欧美性生活一区二区三区 | 精品国产乱码久久久久久闺蜜 | 欧美一级片在线观看 | 国产精品地址 | 中文字幕免费观看 |