gpt4 book ai didi

jquery - 在 Canvas 上拖动元素

转载 作者:行者123 更新时间:2023-12-01 07:13:57 25 4
gpt4 key购买 nike

我将一个元素作为字符串绘制到 Canvas 上。然后,我希望能够单击该元素(或点击移动设备)并将其拖动。我现在拥有的是:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<script src="jquery-1.11.0.js"></script>
</head>
<body>
<canvas id="canvas1" width="662" height="983">
<script src="jquery-1.11.0.js"></script>
</canvas>

<script>
var chosenChord=""; //this gets updated at various times by excluded code, but that code works, because the chord does always print correctly below
` $("button").click(function() {
canvas = document.getElementById('canvas1');
canvas1.addEventListener('click', on_canvas_click, false);

function on_canvas_click(ev) {
x = ev.clientX - canvas1.offsetLeft-40;
y = ev.clientY - canvas1.offsetTop;

//add to canvas
var canvas = document.getElementById("canvas1");
var context = canvas.getContext("2d");
context.fillStyle = "blue";
context.font = "bold 16px Arial";
context.fillText([theString], [x], [y]);
});
});
</script>
</body>
</html>

我需要添加什么才能启用此拖动功能?

最佳答案

以下概述了如何在 Canvas 上拖动元素

演示:http://jsfiddle.net/m1erickson/AGd6u/

监听鼠标事件:mousedown、mousemove、mouseup、mouseout

    // listen for mouse events
$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseOut(e);});

将文本片段定义为数组中的对象

    // some text objects
var texts=[];

// some test texts
texts.push({text:"Hello",x:20,y:20});
texts.push({text:"World",x:20,y:70});

在鼠标按下时:

  • 测试鼠标是否位于文本上方
  • 如果是,请选择要拖动的文本

    // handle mousedown events
    // iterate through texts[] and see if the user
    // mousedown'ed on one of them
    // If yes, set the selectedText to the index of that text
    function handleMouseDown(e){
    e.preventDefault();
    startX=parseInt(e.clientX-offsetX);
    startY=parseInt(e.clientY-offsetY);
    // Put your mousedown stuff here
    for(var i=0;i<texts.length;i++){
    if(textHittest(startX,startY,i)){
    selectedText=i;
    }
    }
    }

在鼠标移动中:

  • 根据用户拖动的距离更改所选文本的位置
  • 重画 Canvas

    // handle mousemove events
    // calc how far the mouse has been dragged since
    // the last mousemove event and move the selected text
    // by that distance
    function handleMouseMove(e){
    if(selectedText<0){return;}
    e.preventDefault();
    mouseX=parseInt(e.clientX-offsetX);
    mouseY=parseInt(e.clientY-offsetY);

    // Put your mousemove stuff here
    var dx=mouseX-startX;
    var dy=mouseY-startY;
    startX=mouseX;
    startY=mouseY;

    var text=texts[selectedText];
    text.x+=dx;
    text.y+=dy;
    draw();
    }

鼠标悬停时:

  • 拖动结束,因此取消选择文本

    // done dragging
    function handleMouseUp(e){
    e.preventDefault();
    selectedText=-1;
    }

示例代码:

<!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");

// variables used to get mouse position on the canvas
var $canvas=$("#canvas");
var canvasOffset=$canvas.offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var scrollX=$canvas.scrollLeft();
var scrollY=$canvas.scrollTop();

// variables to save last mouse position
// used to see how far the user dragged the mouse
// and then move the text by that distance
var startX;
var startY;

// some text objects
var texts=[];

// some test texts
texts.push({text:"Hello",x:20,y:20});
texts.push({text:"World",x:20,y:70});

// calculate width of each text for hit-testing purposes
ctx.font="16px verdana";
for(var i=0;i<texts.length;i++){
var text=texts[i];
text.width=ctx.measureText(text.text).width;
text.height=16;
}

// this var will hold the index of the selected text
var selectedText=-1;

// START: draw all texts to the canvas
draw();

// clear the canvas draw all texts
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
for(var i=0;i<texts.length;i++){
var text=texts[i];
ctx.fillText(text.text,text.x,text.y);
}
}

// test if x,y is inside the bounding box of texts[textIndex]
function textHittest(x,y,textIndex){
var text=texts[textIndex];
return(x>=text.x &&
x<=text.x+text.width &&
y>=text.y-text.height &&
y<=text.y);
}

// handle mousedown events
// iterate through texts[] and see if the user
// mousedown'ed on one of them
// If yes, set the selectedText to the index of that text
function handleMouseDown(e){
e.preventDefault();
startX=parseInt(e.clientX-offsetX);
startY=parseInt(e.clientY-offsetY);

// Put your mousedown stuff here
for(var i=0;i<texts.length;i++){
if(textHittest(startX,startY,i)){
selectedText=i;
}
}
}

// done dragging
function handleMouseUp(e){
e.preventDefault();
selectedText=-1;
}

// also done dragging
function handleMouseOut(e){
e.preventDefault();
selectedText=-1;
}

// handle mousemove events
// calc how far the mouse has been dragged since
// the last mousemove event and move the selected text
// by that distance
function handleMouseMove(e){
if(selectedText<0){return;}
e.preventDefault();
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

// Put your mousemove stuff here
var dx=mouseX-startX;
var dy=mouseY-startY;
startX=mouseX;
startY=mouseY;

var text=texts[selectedText];
text.x+=dx;
text.y+=dy;
draw();
}

// listen for mouse events
$("#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>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

关于jquery - 在 Canvas 上拖动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21605942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com