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

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

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

      • <bdo id='cmKxl'></bdo><ul id='cmKxl'></ul>

        在 html5 画布上拖动图像并调整其大小

        dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)

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

        <legend id='V0F3b'><style id='V0F3b'><dir id='V0F3b'><q id='V0F3b'></q></dir></style></legend>
      1. <tfoot id='V0F3b'></tfoot>
        • <bdo id='V0F3b'></bdo><ul id='V0F3b'></ul>

            <tbody id='V0F3b'></tbody>

                  <i id='V0F3b'><tr id='V0F3b'><dt id='V0F3b'><q id='V0F3b'><span id='V0F3b'><b id='V0F3b'><form id='V0F3b'><ins id='V0F3b'></ins><ul id='V0F3b'></ul><sub id='V0F3b'></sub></form><legend id='V0F3b'></legend><bdo id='V0F3b'><pre id='V0F3b'><center id='V0F3b'></center></pre></bdo></b><th id='V0F3b'></th></span></q></dt></tr></i><div id='V0F3b'><tfoot id='V0F3b'></tfoot><dl id='V0F3b'><fieldset id='V0F3b'></fieldset></dl></div>
                1. 本文介绍了在 html5 画布上拖动图像并调整其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在构建一个 HTML5 画布图像编辑器.将图像上传到画布后,我需要在画布上拖动并调整其大小.我设法上传了一张图片并让它在画布上可拖动.但我需要让它在画布上也可以调整大小.提前致谢.

                  I am building a HTML5 canvas image editor. After uploading an image in to the canvas i need to Dragg and resize it over the canvas. I managed to upload an image and make it draggable on the canvas. But i need to make it resizable also along the canvas. Thanks in advance.

                  var Img = new Image();
                  Img.src = file;
                  Img.onload = function () {
                    context.drawImage(Img, 50, 0, 200, 200); 
                  }
                  mouseMove = function (event){
                  if (down) 
                  {
                   context.clearRect(0,0,800,500);
                   context.translate(0, -50); 
                   context.drawImage(Img, (event.clientX - offsetX),
                   (event.clientY - offsetY), 200, 200);
                   context.translate(0, 50);
                  }
                  }
                  mouseUp = function () {
                    down = false;
                  }
                  mouseDown = function () {
                    down = true;
                  }
                  canvas.addEventListener('mousedown', mouseDown, false);
                  canvas.addEventListener('mouseup', mouseUp, false);
                  canvas.addEventListener('mousemove',mouseMove, false);
                  

                  我尝试了动力学 js,但它不适合画布.

                  I tried with kinetics js but it is not suitable with canvas.

                  推荐答案

                  这里的示例代码允许您使用 Canvas 拖动图像并调整其大小.

                  Here's example code to allow you to drag and resize an image using Canvas.

                  调整大小

                  如何使用 4 个可拖动的锚点调整图像大小

                  • 在图像的每个角上绘制一个可拖动的锚点.
                  • 如果用户 mousedown 是锚点,则开始拖动该锚点.
                  • 在 mousemove 处理程序中,使用拖动锚点的位置调整图像大小(注意如下).
                  • 作为 mousemove 的最后一个动作,重绘调整大小的图像和 4 个新锚点.
                  • 在 mouseup 时,停止锚点的拖动.

                  注意用于调整图像大小的数学:

                  Note on the math used to resize the image:

                  • 调整后的宽度是 mouseX 位置与对角 X 之间的差值.
                  • 调整后的高度是鼠标 Y 位置与对角 Y 位置之间的差值.

                  拖动

                  如何拖动图片

                  • 如果用户 mousedown 在图像内,保存鼠标开始 XY 开始拖动.
                  • 在 mousemove 处理程序中,将图像移动当前 mouseXY 减去起始XY.
                  • 同样在mousemove中,将startingXY重置为当前mouseXY,为继续拖动做准备.
                  • 在 mouseup 时,停止拖动图像.

                  这是代码和小提琴:http://jsfiddle.net/m1erickson/LAS8L/

                  <!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>
                  

                  这篇关于在 html5 画布上拖动图像并调整其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)
                  How to show a running progress bar while page is loading(如何在页面加载时显示正在运行的进度条)
                2. <i id='b2mRA'><tr id='b2mRA'><dt id='b2mRA'><q id='b2mRA'><span id='b2mRA'><b id='b2mRA'><form id='b2mRA'><ins id='b2mRA'></ins><ul id='b2mRA'></ul><sub id='b2mRA'></sub></form><legend id='b2mRA'></legend><bdo id='b2mRA'><pre id='b2mRA'><center id='b2mRA'></center></pre></bdo></b><th id='b2mRA'></th></span></q></dt></tr></i><div id='b2mRA'><tfoot id='b2mRA'></tfoot><dl id='b2mRA'><fieldset id='b2mRA'></fieldset></dl></div>

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

                    <tfoot id='b2mRA'></tfoot>
                    • <bdo id='b2mRA'></bdo><ul id='b2mRA'></ul>
                        <tbody id='b2mRA'></tbody>

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