Javascript 如何使矩形在HTML5中可拖动?
我正在研究HTML5中的可缩放和可拖动矩形 这是我的JSFIDLE链接 在这段代码中,我能够绘制可重新调整大小的矩形,但不能拖动鼠标事件`Javascript 如何使矩形在HTML5中可拖动?,javascript,html,Javascript,Html,我正在研究HTML5中的可缩放和可拖动矩形 这是我的JSFIDLE链接 在这段代码中,我能够绘制可重新调整大小的矩形,但不能拖动鼠标事件` //Make the DIV element draggagle: dragElement(document.getElementById("mydiv")); function dragElement(elmnt) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
Move
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
要使用Jquery UI拖放形状,我们可以设置
draggable
属性
在实例化形状时设置为true,或者可以使用draggable()
方法。
draggable()
自动应用程序
在这里拉小提琴
var canvas=document.getElementById('canvas'),
ctx=canvas.getContext('2d'),
rect={},
drag=false,
慕斯,
老鼠,
足够近=10,
dragTL=dragBL=dragTR=dragBR=false;
函数init(){
canvas.addEventListener('mousedown',mousedown,false);
canvas.addEventListener('mouseup',mouseup,false);
canvas.addEventListener('mousemove',mousemove,false);
矩形={
startX:100,
星号:200,
w:300,
h:200
}
}
功能鼠标向下(e){
mouseX=e.pageX-this.offsetLeft;
mouseY=e.pageY-this.offsetTop;
//如果还没有直肠的话
if(rect.w==未定义){
rect.startX=mouseY;
rect.startY=mouseX;
dragBR=true;
}
//如果有,检查哪个角落
//(如果有)已单击
//
//4例:
//1.左上角
else if(checkcloseverough(mouseX,rect.startX)和&checkcloseverough(mouseY,rect.startY)){
dragTL=真;
}
//2.右上角
else if(checkcloseough(mouseX,rect.startX+rect.w)和&checkcloseough(mouseY,rect.startY)){
dragTR=真;
}
//3.左下角
else if(checkcloseverough(mouseX,rect.startX)和&checkcloseverough(mouseY,rect.startY+rect.h)){
dragBL=true;
}
//4.右下角
else if(checkcloseverough(mouseX,rect.startX+rect.w)和&checkcloseverough(mouseY,rect.startY+rect.h)){
dragBR=true;
}
//(5)没有
否则{
//不调整大小的句柄
}
clearRect(0,0,canvas.width,canvas.height);
draw();
}
功能检查是否足够紧密(p1、p2){
返回Math.abs(p1-p2)<足够接近;
}
函数mouseUp(){
dragTL=dragTR=dragBL=dragBR=false;
}
函数mouseMove(e){
mouseX=e.pageX-this.offsetLeft;
mouseY=e.pageY-this.offsetTop;
如果(dragTL){
rect.w+=rect.startX-mouseX;
rect.h+=rect.startY-mouseY;
rect.startX=mouseX;
rect.startY=mouseY;
}否则,如果(图纸){
rect.w=Math.abs(rect.startX-mouseX);
rect.h+=rect.startY-mouseY;
rect.startY=mouseY;
}否则如果(dragBL){
rect.w+=rect.startX-mouseX;
rect.h=Math.abs(rect.startY-mouseY);
rect.startX=mouseX;
}否则如果(dragBR){
rect.w=Math.abs(rect.startX-mouseX);
rect.h=Math.abs(rect.startY-mouseY);
}
clearRect(0,0,canvas.width,canvas.height);
draw();
}
函数绘图(){
ctx.fillStyle=“#22222”;
ctx.fillRect(rect.startX、rect.startY、rect.w、rect.h);
拉手();
}
函数绘图圆(x、y、半径){
ctx.fillStyle=“#FF0000”;
ctx.beginPath();
弧(x,y,半径,0,2*Math.PI);
ctx.fill();
}
函数drawHandles(){
drawCircle(矩形星形,矩形星形,足够近);
拉丝圈(矩形星型+矩形w型,矩形星型,足够近);
画圈(矩形星型+矩形w,矩形星型+矩形h,足够近);
画圈(矩形星形,矩形星形+矩形h,足够近);
}
init();
var canvas=document.getElementById('canvas'),
ctx=canvas.getContext('2d'),
rect={},
drag=false,
慕斯,
老鼠,
足够近=10,
dragTL=dragBL=dragTR=dragBR=false;
函数init(){
canvas.addEventListener('mousedown',mousedown,false);
canvas.addEventListener('mouseup',mouseup,false);
canvas.addEventListener('mousemove',mousemove,false);
矩形={
startX:100,
星号:200,
w:300,
h:200
}
}
功能鼠标向下(e){
mouseX=e.pageX-this.offsetLeft;
mouseY=e.pageY-this.offsetTop;
//如果还没有直肠的话
if(rect.w==未定义){
rect.startX=mouseY;
rect.startY=mouseX;
dragBR=true;
}
//如果有,检查哪个角落
//(如果有)已单击
//
//4例:
//1.左上角
else if(checkcloseverough(mouseX,rect.startX)和&checkcloseverough(mouseY,rect.startY)){
dragTL=真;
}
//2.右上角
else if(checkcloseough(mouseX,rect.startX+rect.w)和&checkcloseough(mouseY,rect.startY)){
dragTR=真;
}
//3.左下角
else if(checkcloseverough(mouseX,rect.startX)和&checkcloseverough(mouseY,rect.startY+rect.h)){
dragBL=true;
}
//4.右下角
else if(checkcloseverough(mouseX,rect.startX+rect.w)和&checkcloseverough(mouseY,rect.startY+rect.h)){
dragBR=true;
}
//(5)没有
否则{
//不调整大小的句柄
}
clearRect(0,0,canvas.width,canvas.height);
draw();
}
功能检查是否足够紧密(p1、p2){
返回Math.abs(p1-p2)<足够接近;
}
函数mouseUp(){
dragTL=dragTR=dragBL=dragBR=false;
}
$("#canvas").draggable();
<input type="button" value="Delete" id="btnDelete"/>
$("#btnDelete").click(function () {
$("#canvas").remove();
});