2015-09-04 44 views
1

在画布中,我绘制了一个矩形并完成了拖动和调整大小功能。绘制圆以表示手柄来调整矩形的大小。但现在我想拖动使用光标类型。如何使用光标类型拖动和调整svg矩形大小

我已经使用光标键入如下:

$("#canvas").css({'cursor':'ne-resize'}); 

这里是的jsfiddle链接。 http://jsfiddle.net/BaliBalo/9HXMG/

var canvas = document.getElementById('canvas'), 
 
    ctx = canvas.getContext('2d'), 
 
    rect = { 
 
     x: 150, 
 
     y: 100, 
 
     w: 123, 
 
     h: 58 
 
    }, 
 
    handlesSize = 8, 
 
    currentHandle = false, 
 
    drag = false; 
 

 
function init() { 
 
    canvas.addEventListener('mousedown', mouseDown, false); 
 
    canvas.addEventListener('mouseup', mouseUp, false); 
 
    canvas.addEventListener('mousemove', mouseMove, false); 
 
} 
 

 
function point(x, y) { 
 
    return { 
 
     x: x, 
 
     y: y 
 
    }; 
 
} 
 

 
function dist(p1, p2) { 
 
    return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)); 
 
} 
 

 
function getHandle(mouse) { 
 
    if (dist(mouse, point(rect.x, rect.y)) <= handlesSize) return 'topleft'; 
 
    if (dist(mouse, point(rect.x + rect.w, rect.y)) <= handlesSize) return 'topright'; 
 
    if (dist(mouse, point(rect.x, rect.y + rect.h)) <= handlesSize) return 'bottomleft'; 
 
    if (dist(mouse, point(rect.x + rect.w, rect.y + rect.h)) <= handlesSize) return 'bottomright'; 
 
    if (dist(mouse, point(rect.x + rect.w/2, rect.y)) <= handlesSize) return 'top'; 
 
    if (dist(mouse, point(rect.x, rect.y + rect.h/2)) <= handlesSize) return 'left'; 
 
    if (dist(mouse, point(rect.x + rect.w/2, rect.y + rect.h)) <= handlesSize) return 'bottom'; 
 
    if (dist(mouse, point(rect.x + rect.w, rect.y + rect.h/2)) <= handlesSize) return 'right'; 
 
    return false; 
 
} 
 

 
function mouseDown(e) { 
 
    if (currentHandle) drag = true; 
 
    draw(); 
 
} 
 

 
function mouseUp() { 
 
    drag = false; 
 
    currentHandle = false; 
 
    draw(); 
 
} 
 

 
function mouseMove(e) { 
 
    var previousHandle = currentHandle; 
 
    if (!drag) currentHandle = getHandle(point(e.pageX - this.offsetLeft, e.pageY - this.offsetTop)); 
 
    if (currentHandle && drag) { 
 
     var mousePos = point(e.pageX - this.offsetLeft, e.pageY - this.offsetTop); 
 
     switch (currentHandle) { 
 
      case 'topleft': 
 
       rect.w += rect.x - mousePos.x; 
 
       rect.h += rect.y - mousePos.y; 
 
       rect.x = mousePos.x; 
 
       rect.y = mousePos.y; 
 
       break; 
 
      case 'topright': 
 
       rect.w = mousePos.x - rect.x; 
 
       rect.h += rect.y - mousePos.y; 
 
       rect.y = mousePos.y; 
 
       break; 
 
      case 'bottomleft': 
 
       rect.w += rect.x - mousePos.x; 
 
       rect.x = mousePos.x; 
 
       rect.h = mousePos.y - rect.y; 
 
       break; 
 
      case 'bottomright': 
 
       rect.w = mousePos.x - rect.x; 
 
       rect.h = mousePos.y - rect.y; 
 
       break; 
 

 
      case 'top': 
 
       rect.h += rect.y - mousePos.y; 
 
       rect.y = mousePos.y; 
 
       break; 
 

 
      case 'left': 
 
       rect.w += rect.x - mousePos.x; 
 
       rect.x = mousePos.x; 
 
       break; 
 

 
      case 'bottom': 
 
       rect.h = mousePos.y - rect.y; 
 
       break; 
 

 
      case 'right': 
 
       rect.w = mousePos.x - rect.x; 
 
       break; 
 
     } 
 
    } 
 
    if (drag || currentHandle != previousHandle) draw(); 
 
} 
 

 
function draw() { 
 
    ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
    ctx.fillStyle = 'black'; 
 
    ctx.fillRect(rect.x, rect.y, rect.w, rect.h); 
 
    if (currentHandle) { 
 
     var posHandle = point(0, 0); 
 
     switch (currentHandle) { 
 
      case 'topleft': 
 
       posHandle.x = rect.x; 
 
       posHandle.y = rect.y; 
 
       break; 
 
      case 'topright': 
 
       posHandle.x = rect.x + rect.w; 
 
       posHandle.y = rect.y; 
 
       break; 
 
      case 'bottomleft': 
 
       posHandle.x = rect.x; 
 
       posHandle.y = rect.y + rect.h; 
 
       break; 
 
      case 'bottomright': 
 
       posHandle.x = rect.x + rect.w; 
 
       posHandle.y = rect.y + rect.h; 
 
       break; 
 
      case 'top': 
 
       posHandle.x = rect.x + rect.w/2; 
 
       posHandle.y = rect.y; 
 
       break; 
 
      case 'left': 
 
       posHandle.x = rect.x; 
 
       posHandle.y = rect.y + rect.h/2; 
 
       break; 
 
      case 'bottom': 
 
       posHandle.x = rect.x + rect.w/2; 
 
       posHandle.y = rect.y + rect.h; 
 
       break; 
 
      case 'right': 
 
       posHandle.x = rect.x + rect.w; 
 
       posHandle.y = rect.y + rect.h/2; 
 
       break; 
 
     } 
 
     ctx.globalCompositeOperation = 'xor'; 
 
     ctx.beginPath(); 
 
     ctx.arc(posHandle.x, posHandle.y, handlesSize, 0, 2 * Math.PI); 
 
     ctx.fill(); 
 
     ctx.globalCompositeOperation = 'source-over'; 
 
    } 
 
} 
 

 
init(); 
 
draw();
<canvas id="canvas" width="500" height="500"></canvas>

回答

3

添加想要的游标类型在draw功能的swicth声明:乌拉圭回合response.actually我已经在rect.but拖动的矩形时设置游标类型

var canvas = document.getElementById('canvas'), 
 
    ctx = canvas.getContext('2d'), 
 
    rect = { 
 
     x: 150, 
 
     y: 100, 
 
     w: 123, 
 
     h: 58 
 
    }, 
 
    handlesSize = 8, 
 
    currentHandle = false, 
 
    drag = false; 
 

 
function init() { 
 
    canvas.addEventListener('mousedown', mouseDown, false); 
 
    canvas.addEventListener('mouseup', mouseUp, false); 
 
    canvas.addEventListener('mousemove', mouseMove, false); 
 
} 
 

 
function point(x, y) { 
 
    return { 
 
     x: x, 
 
     y: y 
 
    }; 
 
} 
 

 
function dist(p1, p2) { 
 
    return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)); 
 
} 
 

 
function getHandle(mouse) { 
 
    if (dist(mouse, point(rect.x, rect.y)) <= handlesSize) return 'topleft'; 
 
    if (dist(mouse, point(rect.x + rect.w, rect.y)) <= handlesSize) return 'topright'; 
 
    if (dist(mouse, point(rect.x, rect.y + rect.h)) <= handlesSize) return 'bottomleft'; 
 
    if (dist(mouse, point(rect.x + rect.w, rect.y + rect.h)) <= handlesSize) return 'bottomright'; 
 
    if (dist(mouse, point(rect.x + rect.w/2, rect.y)) <= handlesSize) return 'top'; 
 
    if (dist(mouse, point(rect.x, rect.y + rect.h/2)) <= handlesSize) return 'left'; 
 
    if (dist(mouse, point(rect.x + rect.w/2, rect.y + rect.h)) <= handlesSize) return 'bottom'; 
 
    if (dist(mouse, point(rect.x + rect.w, rect.y + rect.h/2)) <= handlesSize) return 'right'; 
 
    return false; 
 
} 
 

 
function mouseDown(e) { 
 
    if (currentHandle) drag = true; 
 
    draw(); 
 
} 
 

 
function mouseUp() { 
 
    drag = false; 
 
    currentHandle = false; 
 
    draw(); 
 
} 
 

 
function mouseMove(e) { 
 
    var previousHandle = currentHandle; 
 
    if (!drag) currentHandle = getHandle(point(e.pageX - this.offsetLeft, e.pageY - this.offsetTop)); 
 
    if (currentHandle && drag) { 
 
     var mousePos = point(e.pageX - this.offsetLeft, e.pageY - this.offsetTop); 
 
     switch (currentHandle) { 
 
      case 'topleft': 
 
       rect.w += rect.x - mousePos.x; 
 
       rect.h += rect.y - mousePos.y; 
 
       rect.x = mousePos.x; 
 
       rect.y = mousePos.y; 
 
       break; 
 
      case 'topright': 
 
       rect.w = mousePos.x - rect.x; 
 
       rect.h += rect.y - mousePos.y; 
 
       rect.y = mousePos.y; 
 
       break; 
 
      case 'bottomleft': 
 
       rect.w += rect.x - mousePos.x; 
 
       rect.x = mousePos.x; 
 
       rect.h = mousePos.y - rect.y; 
 
       break; 
 
      case 'bottomright': 
 
       rect.w = mousePos.x - rect.x; 
 
       rect.h = mousePos.y - rect.y; 
 
       break; 
 

 
      case 'top': 
 
       rect.h += rect.y - mousePos.y; 
 
       rect.y = mousePos.y; 
 
       break; 
 

 
      case 'left': 
 
       rect.w += rect.x - mousePos.x; 
 
       rect.x = mousePos.x; 
 
       break; 
 

 
      case 'bottom': 
 
       rect.h = mousePos.y - rect.y; 
 
       break; 
 

 
      case 'right': 
 
       rect.w = mousePos.x - rect.x; 
 
       break; 
 
     } 
 
    } 
 
    if (drag || currentHandle != previousHandle) draw(); 
 

 

 
} 
 

 
function draw() { 
 
    ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
    ctx.fillStyle = 'black'; 
 
    ctx.fillRect(rect.x, rect.y, rect.w, rect.h); 
 
    if (currentHandle) { 
 
     var posHandle = point(0, 0); 
 
     var dir; 
 
     switch (currentHandle) { 
 
      case 'topleft': 
 
       dir={x:rect.w>0?'w':'e',y:rect.h>0?'n':'s'}; 
 
       canvas.style.cursor= dir.y+dir.x+'-resize'; 
 
       break; 
 
      case 'topright': 
 
       dir={x:rect.w>0?'e':'w',y:rect.h>0?'n':'s'}; 
 
       canvas.style.cursor= dir.y+dir.x+'-resize'; 
 
       break; 
 
      case 'bottomleft': 
 
       dir={x:rect.w>0?'w':'e',y:rect.h>0?'s':'n'}; 
 
       canvas.style.cursor= dir.y+dir.x+'-resize'; 
 
       break; 
 
      case 'bottomright': 
 
       dir={x:rect.w>0?'e':'w',y:rect.h>0?'s':'n'}; 
 
       canvas.style.cursor= dir.y+dir.x+'-resize'; 
 
       break; 
 
      case 'top': 
 
       canvas.style.cursor= (rect.h>0?'n':'s')+'-resize'; 
 
       break; 
 
      case 'left': 
 
       canvas.style.cursor= (rect.w>0?'w':'e')+'-resize'; 
 
       break; 
 
      case 'bottom': 
 
       canvas.style.cursor= (rect.h>0?'s':'n')+'-resize'; 
 
       break; 
 
      case 'right': 
 
       canvas.style.cursor= (rect.w>0?'e':'w')+'-resize'; 
 
       break; 
 
     } 
 
    }else canvas.style.cursor=''; 
 
} 
 

 
init(); 
 
draw();
<canvas id="canvas" width="500" height="500"></canvas>

+0

谢谢@ Kaiido.This也已完成我也。在这种情况下,无论你有检查光标在向负方向拖动矩形后键入已经运行你的代码片段。最初的光标绘制为右上角的位置是crct.but当你不得不拖动所有侧面后,你有检查方向是错的。请你检查一下。我在问这个 – Rekha

+0

你是对的,更新。 – Kaiido

+0

是的,现在工作正常。感谢很多。 – Rekha

0

如果你只是想改变光标类型,把这个在你的HTML的风格标签或链接到您的HTML CSS文件

#canvas{ cursor: ne-resize} 

或使用javascript做到这一点

document.getElementById("canvas").style.cursor = "ne-resize"; 

你有$("#canvas").css({'cursor':'ne-resize'});的代码是jQuery。为了实现这一点,您需要将头部标签中的库链接起来。

+0

谢谢360度,在消极的一面得到错误的句柄多数民众赞成为什么光标不正确设置。如果你在小提琴链接ü可以明白什么即时通讯 – Rekha