pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <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="whypluo" 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="kzgiobp" 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="ui8is6t" 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>

                          • 主站蜘蛛池模板: 全自动变压器变比组别测试仪-手持式直流电阻测试仪-上海来扬电气 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 酵素生产厂家_酵素OEM_酵素加盟_酵素ODM_酵素原料厂家_厦门益力康 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 电池高低温试验箱-气态冲击箱-双层电池防爆箱|简户百科 | 游泳池设备安装工程_恒温泳池设备_儿童游泳池设备厂家_游泳池水处理设备-东莞市君达泳池设备有限公司 | 市政路灯_厂家-淄博信达电力科技有限公司 | Duoguan 夺冠集团| 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 踏板力计,制动仪,非接触多功能速度仪,逆反射系数测试仪-创宇 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 协议书_协议合同格式模板范本大全 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 注塑_注塑加工_注塑模具_塑胶模具_注塑加工厂家_深圳环科 | 工业风机_环保空调_冷风机_工厂车间厂房通风降温设备旺成服务平台 | 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 木材烘干机,木炭烘干机,纸管/佛香烘干设备-河南蓝天机械制造有限公司 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 贵州自考_贵州自学考试网| 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 塑料撕碎机_编织袋撕碎机_废纸撕碎机_生活垃圾撕碎机_废铁破碎机_河南鑫世昌机械制造有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 无机纤维喷涂棉-喷涂棉施工工程-山东华泉建筑工程有限公司▲ | 爆炸冲击传感器-无线遥测传感器-航天星百科 | 北京开源多邦科技发展有限公司官网| 新车测评网_网罗汽车评测资讯_汽车评测门户报道| 环球电气之家-中国专业电气电子产品行业服务网站! | 带压开孔_带压堵漏_带压封堵-菏泽金升管道工程有限公司 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 岛津二手液相色谱仪,岛津10A液相,安捷伦二手液相,安捷伦1100液相-杭州森尼欧科学仪器有限公司 | 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 电动高压冲洗车_价格-江苏速利达机车有限公司 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司| 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 |