gpt4 book ai didi

javascript - 我正在尝试使用鼠标事件在 Canvas 上画一条线

转载 作者:行者123 更新时间:2023-11-28 13:35:45 25 4
gpt4 key购买 nike

我尝试使用 mousedownmousemovemouseup 事件在 Canvas 上画一条线,但无法设置绘图坐标 Canvas 上的一条线。这里我使用以下 JavaScript 代码:

function line() 
{
canvas = document.getElementById("drawingCanvas");
context = canvas.getContext("2d");
canvas.onmousedown = startLine;
canvas.onmouseup = drawLine;
canvas.onmouseout = stopLine;
//canvas.onmousemove =drawLine;
};

function startLine(e)
{
isLine = true;
context.beginPath();
context.moveTo(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop);

}

function drawLine(e)
{
if (isLine==true)
{
var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
context.lineTo(x ,y);
context.stroke();
context.closePath();
}
cPush();
}
function stopLine()
{
isLine = false;
}

当我使用 mousemove 事件作为 drawLine() 方法时,它正在绘制多条线。你能告诉我我的代码有什么问题吗?

最佳答案

基本上,您需要重构代码以在 mousemove 中执行 beginPath+moveTo+lineTo+lines。

否则你会得到多行......

在 mouseDown 中:保存 startX/startY 并设置 isDown 标志以指示拖动已开始:

function handleMouseDown(e){
e.preventDefault();
startX=parseInt(e.clientX-offsetX);
startY=parseInt(e.clientY-offsetY);
isDown=true;
}

在mouseMove中:从startX/Y到mouseX/Y绘制一条新线并重置startX/Y=mouseX/Y

function handleMouseMove(e){
if(!isDown){return;}
e.preventDefault();

mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

ctx.beginPath(); // use beginPath for every segment of the line
ctx.moveTo(startX,startY);
ctx.lineTo(mouseX,mouseY);
ctx.stroke();
startX=mouseX;
startY=mouseY;
}

在 mouseUP 中:清除 isDown 标志以指示拖动已结束:

function handleMouseUp(e){
e.preventDefault();
isDown=false;
}

这是代码和 fiddle :http://jsfiddle.net/m1erickson/hzNg4/

<!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 $canvas=$("#canvas");
var canvasOffset=$canvas.offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var scrollX=$canvas.scrollLeft();
var scrollY=$canvas.scrollTop();

var isDown=false;
var startX;
var startY;


function handleMouseDown(e){
e.preventDefault();
startX=parseInt(e.clientX-offsetX);
startY=parseInt(e.clientY-offsetY);
isDown=true;
}

function handleMouseUp(e){
e.preventDefault();
isDown=false;
}

function handleMouseOut(e){
e.preventDefault();
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
isDown=false;
}

function handleMouseMove(e){
if(!isDown){return;}
e.preventDefault();
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

// Put your mousemove stuff here
ctx.beginPath();
ctx.moveTo(startX,startY);
ctx.lineTo(mouseX,mouseY);
ctx.stroke();
startX=mouseX;
startY=mouseY;

}

$("#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>

关于javascript - 我正在尝试使用鼠标事件在 Canvas 上画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472714/

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