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

      • <bdo id='DpUHY'></bdo><ul id='DpUHY'></ul>
      1. <tfoot id='DpUHY'></tfoot>

      2. <small id='DpUHY'></small><noframes id='DpUHY'>

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

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

        使用鼠標(biāo)旋轉(zhuǎn)畫布中的圖像

        To rotation an image in canvas using mouse(使用鼠標(biāo)旋轉(zhuǎn)畫布中的圖像)
          <tbody id='5t2Ci'></tbody>
      4. <tfoot id='5t2Ci'></tfoot>
        • <bdo id='5t2Ci'></bdo><ul id='5t2Ci'></ul>
                  <legend id='5t2Ci'><style id='5t2Ci'><dir id='5t2Ci'><q id='5t2Ci'></q></dir></style></legend>

                  <small id='5t2Ci'></small><noframes id='5t2Ci'>

                  <i id='5t2Ci'><tr id='5t2Ci'><dt id='5t2Ci'><q id='5t2Ci'><span id='5t2Ci'><b id='5t2Ci'><form id='5t2Ci'><ins id='5t2Ci'></ins><ul id='5t2Ci'></ul><sub id='5t2Ci'></sub></form><legend id='5t2Ci'></legend><bdo id='5t2Ci'><pre id='5t2Ci'><center id='5t2Ci'></center></pre></bdo></b><th id='5t2Ci'></th></span></q></dt></tr></i><div class="bdrtd3z" id='5t2Ci'><tfoot id='5t2Ci'></tfoot><dl id='5t2Ci'><fieldset id='5t2Ci'></fieldset></dl></div>
                  本文介紹了使用鼠標(biāo)旋轉(zhuǎn)畫布中的圖像的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  在我的代碼中,我將圖像加載到畫布中.然后我需要調(diào)整大小、旋轉(zhuǎn)和拖動(dòng)它.我設(shè)法實(shí)現(xiàn)了拖動(dòng)和調(diào)整大小.

                  In my code I am loading an image in to a canvas. Then I need to resize, rotate and drag it. I managed to implement both dragging and resizing.

                  如何在此代碼上使用鼠標(biāo)實(shí)現(xiàn)旋轉(zhuǎn)(沿圖像中心).

                  How can I implement rotation(along the center of the image) using mouse on this code.

                  我的 HTML 頁面:

                  <!doctype html>
                  <html>
                  <head>
                  <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
                  <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
                  
                  <style>
                      body{ background-color: ivory; padding:10px;}
                      #canvas{border:1px solid red;}
                  </style>
                  
                  <script>
                  $(function(){
                  
                      var canvas=document.getElementById("canvas");
                      var ctx=canvas.getContext("2d");
                  
                      var canvasOffset=$("#canvas").offset();
                      var offsetX=canvasOffset.left;
                      var offsetY=canvasOffset.top;
                  
                      var startX;
                      var startY;
                      var isDown=false;
                  
                  
                      var pi2=Math.PI*2;
                      var resizerRadius=8;
                      var rr=resizerRadius*resizerRadius;
                      var draggingResizer={x:0,y:0};
                      var imageX=50;
                      var imageY=50;
                      var imageWidth,imageHeight,imageRight,imageBottom;
                      var draggingImage=false;
                      var startX;
                      var startY;
                  
                  
                  
                      var img=new Image();
                      img.onload=function(){
                          imageWidth=img.width;
                          imageHeight=img.height;
                          imageRight=imageX+imageWidth;
                          imageBottom=imageY+imageHeight
                          draw(true,false);
                      }
                      img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/facesSmall.png";
                  
                  
                      function draw(withAnchors,withBorders){
                  
                          // clear the canvas
                          ctx.clearRect(0,0,canvas.width,canvas.height);
                  
                          // draw the image
                          ctx.drawImage(img,0,0,img.width,img.height,imageX,imageY,imageWidth,imageHeight);
                  
                          // optionally draw the draggable anchors
                          if(withAnchors){
                              drawDragAnchor(imageX,imageY);
                              drawDragAnchor(imageRight,imageY);
                              drawDragAnchor(imageRight,imageBottom);
                              drawDragAnchor(imageX,imageBottom);
                          }
                  
                          // optionally draw the connecting anchor lines
                          if(withBorders){
                              ctx.beginPath();
                              ctx.moveTo(imageX,imageY);
                              ctx.lineTo(imageRight,imageY);
                              ctx.lineTo(imageRight,imageBottom);
                              ctx.lineTo(imageX,imageBottom);
                              ctx.closePath();
                              ctx.stroke();
                          }
                  
                      }
                  
                      function drawDragAnchor(x,y){
                          ctx.beginPath();
                          ctx.arc(x,y,resizerRadius,0,pi2,false);
                          ctx.closePath();
                          ctx.fill();
                      }
                  
                      function anchorHitTest(x,y){
                  
                          var dx,dy;
                  
                          // top-left
                          dx=x-imageX;
                          dy=y-imageY;
                          if(dx*dx+dy*dy<=rr){ return(0); }
                          // top-right
                          dx=x-imageRight;
                          dy=y-imageY;
                          if(dx*dx+dy*dy<=rr){ return(1); }
                          // bottom-right
                          dx=x-imageRight;
                          dy=y-imageBottom;
                          if(dx*dx+dy*dy<=rr){ return(2); }
                          // bottom-left
                          dx=x-imageX;
                          dy=y-imageBottom;
                          if(dx*dx+dy*dy<=rr){ return(3); }
                          return(-1);
                  
                      }
                  
                  
                      function hitImage(x,y){
                          return(x>imageX && x<imageX+imageWidth && y>imageY && y<imageY+imageHeight);
                      }
                  
                  
                      function handleMouseDown(e){
                        startX=parseInt(e.clientX-offsetX);
                        startY=parseInt(e.clientY-offsetY);
                        draggingResizer=anchorHitTest(startX,startY);
                        draggingImage= draggingResizer<0 && hitImage(startX,startY);
                      }
                  
                      function handleMouseUp(e){
                        draggingResizer=-1;
                        draggingImage=false;
                        draw(true,false);
                      }
                  
                      function handleMouseOut(e){
                        handleMouseUp(e);
                      }
                  
                      function handleMouseMove(e){
                  
                        if(draggingResizer>-1){
                  
                            mouseX=parseInt(e.clientX-offsetX);
                            mouseY=parseInt(e.clientY-offsetY);
                  
                            // resize the image
                            switch(draggingResizer){
                                case 0: //top-left
                                    imageX=mouseX;
                                    imageWidth=imageRight-mouseX;
                                    imageY=mouseY;
                                    imageHeight=imageBottom-mouseY;
                                    break;
                                case 1: //top-right
                                    imageY=mouseY;
                                    imageWidth=mouseX-imageX;
                                    imageHeight=imageBottom-mouseY;
                                    break;
                                case 2: //bottom-right
                                    imageWidth=mouseX-imageX;
                                    imageHeight=mouseY-imageY;
                                    break;
                                case 3: //bottom-left
                                    imageX=mouseX;
                                    imageWidth=imageRight-mouseX;
                                    imageHeight=mouseY-imageY;
                                    break;
                            }
                  
                            // enforce minimum dimensions of 25x25
                            if(imageWidth<25){imageWidth=25;}
                            if(imageHeight<25){imageHeight=25;}
                  
                            // set the image right and bottom
                            imageRight=imageX+imageWidth;
                            imageBottom=imageY+imageHeight;
                  
                            // redraw the image with resizing anchors
                            draw(true,true);
                  
                        }else if(draggingImage){
                  
                            imageClick=false;
                  
                            mouseX=parseInt(e.clientX-offsetX);
                            mouseY=parseInt(e.clientY-offsetY);
                  
                            // move the image by the amount of the latest drag
                            var dx=mouseX-startX;
                            var dy=mouseY-startY;
                            imageX+=dx;
                            imageY+=dy;
                            imageRight+=dx;
                            imageBottom+=dy;
                            // reset the startXY for next time
                            startX=mouseX;
                            startY=mouseY;
                  
                            // redraw the image with border
                            draw(false,true);
                  
                        }
                  
                  
                      }
                  
                  
                      $("#canvas").mousedown(function(e){handleMouseDown(e);});
                      $("#canvas").mousemove(function(e){handleMouseMove(e);});
                      $("#canvas").mouseup(function(e){handleMouseUp(e);});
                      $("#canvas").mouseout(function(e){handleMouseOut(e);});
                  
                  
                  }); // end $(function(){});
                  </script>
                  
                  </head>
                  
                  <body>
                      <p>Resize the image using the 4 draggable corner anchors</p>
                      <p>You can also drag the image</p>
                      <canvas id="canvas" width=350 height=350></canvas>
                  </body>
                  </html>
                  

                  推薦答案

                  以下是如何使用拖動(dòng)手柄旋轉(zhuǎn)圖像

                  mousedown 事件處理程序命中測試用戶是否開始拖動(dòng)旋轉(zhuǎn)手柄.

                  使用 context.isPointInPath(x,y) 可以更輕松地進(jìn)行命中測試,它測試指定的 [x,y] 坐標(biāo)是否在最近繪制的路徑內(nèi)(方便地,旋轉(zhuǎn)手柄實(shí)際上是路徑).

                  This hit-testing is made easier with context.isPointInPath(x,y) which tests whether a specified [x,y] coordinate is inside the most recently drawn path (Conveniently, the rotation-handle is actually a path).

                  所以 mousedown 像這樣激活拖動(dòng)手柄:

                  So mousedown activates the drag-handle like this:

                  • 計(jì)算當(dāng)前的 mouseX 和 mouseY.
                  • 重繪旋轉(zhuǎn)手柄(必需,因?yàn)?isPointInPath 只對最近的路徑進(jìn)行命中測試)
                  • 如果用戶確實(shí)點(diǎn)擊了旋轉(zhuǎn)手柄,則設(shè)置 isDown 標(biāo)志.

                  mousedown 代碼如下所示:

                  The mousedown code looks like this:

                  function handleMouseDown(e){
                    mouseX=parseInt(e.clientX-offsetX);
                    mouseY=parseInt(e.clientY-offsetY);
                    drawRotationHandle(false);
                    isDown=ctx.isPointInPath(mouseX,mouseY);
                  }
                  

                  是的...我們可以簡單地對旋轉(zhuǎn)手柄末端的一個(gè)圓圈進(jìn)行命中測試,但是使用 isPointInPath 將允許您繪制任何您想要的花哨的旋轉(zhuǎn)手柄.

                  Yes...we could have simply hit-tested a circle on the end of the rotation-handle, but using isPointInPath will allow you to draw whatever fancy rotation handle you desire.

                  isPointInPath 還有另一個(gè)好處.當(dāng)包含路徑的上下文被旋轉(zhuǎn)時(shí),isPointInPath 將為您測試旋轉(zhuǎn)路徑.這意味著您不必編寫數(shù)學(xué)代碼來取消旋轉(zhuǎn)鼠標(biāo)坐標(biāo)來進(jìn)行命中測試——它已經(jīng)為您完成了!

                  And isPointInPath has another nice benefit. When the context containing the path is rotated, isPointInPath will hit-test the rotated path for you. This means you don't have to code the math to unrotate the mouse coordinates to do the hit testing--it's done for you!

                  mousemove 處理程序以旋轉(zhuǎn)句柄指定的角度重繪可旋轉(zhuǎn)圖像:

                  • 如果未設(shè)置 isDown 標(biāo)志,則返回(用戶未拖動(dòng)旋轉(zhuǎn)手柄).
                  • 計(jì)算當(dāng)前的 mouseX 和 mouseY.
                  • 計(jì)算旋轉(zhuǎn)手柄的當(dāng)前角度.
                  • 以當(dāng)前角度重繪可旋轉(zhuǎn)圖像.

                  mousemove 代碼如下所示:

                  The mousemove code looks like this:

                  function handleMouseMove(e){
                    if(!isDown){return;}
                  
                    mouseX=parseInt(e.clientX-offsetX);
                    mouseY=parseInt(e.clientY-offsetY);
                    var dx=mouseX-cx;
                    var dy=mouseY-cy;
                    r=Math.atan2(dy,dx);
                    draw();
                  }
                  

                  使用上下文的變換方法在指定的旋轉(zhuǎn)處繪制圖像

                  function drawRect(){
                      ctx.save();
                      ctx.translate(cx,cy);
                      ctx.rotate(r);
                      ctx.drawImage(img,0,0);
                      ctx.restore();
                  }
                  

                  最后,mouseup 和 mouseout 處理程序通過清除 isDown 標(biāo)志來停止拖動(dòng)操作.

                  function handleMouseUp(e){
                    isDown=false;
                  }
                  
                  function handleMouseOut(e){
                    isDown=false;
                  }
                  

                  這是代碼和小提琴:http://jsfiddle.net/m1erickson/QqwKR/

                  <!doctype html>
                  <html>
                  <head>
                  <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
                  <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
                  
                  <style>
                      body{ background-color: ivory; }
                      #canvas{border:1px solid red;}
                  </style>
                  
                  <script>
                  $(function(){
                  
                      var canvas=document.getElementById("canvas");
                      var ctx=canvas.getContext("2d");
                  
                      var canvasOffset=$("#canvas").offset();
                      var offsetX=canvasOffset.left;
                      var offsetY=canvasOffset.top;
                  
                      var isDown=false;
                  
                      var cx=canvas.width/2;
                      var cy=canvas.height/2;
                      var w;
                      var h;
                      var r=0;
                  
                      var img=new Image();
                      img.onload=function(){
                          w=img.width/2;
                          h=img.height/2;
                          draw();
                      }
                      img.src="facesSmall.png";
                  
                  
                      function draw(){
                          ctx.clearRect(0,0,canvas.width,canvas.height);
                          drawRotationHandle(true);
                          drawRect();
                      }
                  
                      function drawRect(){
                          ctx.save();
                          ctx.translate(cx,cy);
                          ctx.rotate(r);
                          ctx.drawImage(img,0,0,img.width,img.height,-w/2,-h/2,w,h);
                          ctx.restore();
                      }
                  
                      function drawRotationHandle(withFill){
                          ctx.save();
                          ctx.translate(cx,cy);
                          ctx.rotate(r);
                          ctx.beginPath();
                          ctx.moveTo(0,-1);
                          ctx.lineTo(w/2+20,-1);
                          ctx.lineTo(w/2+20,-7);
                          ctx.lineTo(w/2+30,-7);
                          ctx.lineTo(w/2+30,7);
                          ctx.lineTo(w/2+20,7);
                          ctx.lineTo(w/2+20,1);
                          ctx.lineTo(0,1);
                          ctx.closePath();
                          if(withFill){
                              ctx.fillStyle="blue";
                              ctx.fill();
                          }
                          ctx.restore();
                      }
                  
                      function handleMouseDown(e){
                        mouseX=parseInt(e.clientX-offsetX);
                        mouseY=parseInt(e.clientY-offsetY);
                        drawRotationHandle(false);
                        isDown=ctx.isPointInPath(mouseX,mouseY);
                        console.log(isDown);
                      }
                  
                      function handleMouseUp(e){
                        isDown=false;
                      }
                  
                      function handleMouseOut(e){
                        isDown=false;
                      }
                  
                      function handleMouseMove(e){
                        if(!isDown){return;}
                  
                        mouseX=parseInt(e.clientX-offsetX);
                        mouseY=parseInt(e.clientY-offsetY);
                        var dx=mouseX-cx;
                        var dy=mouseY-cy;
                        r=Math.atan2(dy,dx);
                        draw();
                      }
                  
                      $("#canvas").mousedown(function(e){handleMouseDown(e);});
                      $("#canvas").mousemove(function(e){handleMouseMove(e);});
                      $("#canvas").mouseup(function(e){handleMouseUp(e);});
                      $("#canvas").mouseout(function(e){handleMouseOut(e);});
                  
                  }); // end $(function(){});
                  </script>
                  
                  </head>
                  
                  <body>
                      <p>Rotate by dragging blue rotation handle</p>
                      <canvas id="canvas" width=300 height=300></canvas>
                  </body>
                  </html>
                  

                  這篇關(guān)于使用鼠標(biāo)旋轉(zhuǎn)畫布中的圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調(diào)用完成)
                  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標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)

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

                        <tbody id='eytrh'></tbody>

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

                          <bdo id='eytrh'></bdo><ul id='eytrh'></ul>
                            主站蜘蛛池模板: 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 胶辊硫化罐_胶鞋硫化罐_硫化罐厂家-山东鑫泰鑫智能装备有限公司 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 学叉车培训|叉车证报名|叉车查询|叉车证怎么考-工程机械培训网 | 517瓜水果特产网|一个专注特产好物的网站 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 无锡网站建设_小程序制作_网站设计公司_无锡网络公司_网站制作 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 无刷电机_直流无刷电机_行星减速机-佛山市藤尺机电设备有限公司 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 车辆定位管理系统_汽车GPS系统_车载北斗系统 - 朗致物联 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 猪I型/II型胶原-五克隆合剂-细胞冻存培养基-北京博蕾德科技发展有限公司 | 列管冷凝器,刮板蒸发器,外盘管反应釜厂家-无锡曼旺化工设备有限公司 | 中药超微粉碎机(中药细胞级微粉碎)-百科 | 布袋除尘器-单机除尘器-脉冲除尘器-泊头市兴天环保设备有限公司 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司| 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 一体化污水处理设备-一体化净水设备-「山东梦之洁水处理」 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | 渣油泵,KCB齿轮泵,不锈钢齿轮泵,重油泵,煤焦油泵,泊头市泰邦泵阀制造有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | 特材真空腔体_哈氏合金/镍基合金/纯镍腔体-无锡国德机械制造有限公司 | Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司 | 制氮设备_PSA制氮机_激光切割制氮机_氮气机生产厂家-苏州西斯气体设备有限公司 | 天坛家具官网| LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | 顶空进样器-吹扫捕集仪-热脱附仪-二次热解吸仪-北京华盛谱信仪器 | 培训中心-翰香原香酥板栗饼加盟店总部-正宗板栗酥饼技术 | 软文世界-软文推广-软文营销-新闻稿发布-一站式软文自助发稿平台 | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 |