gpt4 book ai didi

javascript - 在 Canvas 上绘制一个区域

转载 作者:行者123 更新时间:2023-11-30 18:06:55 28 4
gpt4 key购买 nike

我需要在绘图时用光标挂住绘图线。我尝试使用 mousemove 事件,但没有成功。以下是我目前所拥有的。

http://jsfiddle.net/m1erickson/qwd2a/

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var canvasMouseX;
var canvasMouseY;
var canvasOffset = $("#canvas").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var storedLines = [];

ctx.strokeStyle = "orange";
ctx.font = '12px Arial';

$("#canvas").mousedown(function (e) {
handleMouseDown(e);
});

function handleMouseDown(e) {
canvasMouseX = parseInt(e.clientX - offsetX);
canvasMouseY = parseInt(e.clientY - offsetY);

// Put your mousedown stuff here
storedLines.push({
x: canvasMouseX,
y: canvasMouseY
});
var count = storedLines.length;
var X = canvasMouseX - (count < 10 ? 4 : 7);
ctx.strokeStyle = "orange";
ctx.fillStyle = "black";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(canvasMouseX, canvasMouseY, 8, 0, 2 * Math.PI, false);
ctx.fillText(storedLines.length, X, canvasMouseY + 4);
ctx.stroke();
}

$("#draw").click(function () {
ctx.strokeStyle = "red";
ctx.fillStyle = "blue";
ctx.lineWidth = 3;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(storedLines[0].x, storedLines[0].y);
for (var i = 0; i < storedLines.length; i++) {
ctx.lineTo(storedLines[i].x, storedLines[i].y);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
storedLines = [];
});

$("#clear").click(function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
storedLines = [];
});

最佳答案

对于 Canvas ,您需要负责在每次需要时重新绘制全部内容。这意味着当鼠标光标移动时,您需要清除 Canvas ,然后重新绘制所有当前线段。根据您的代码,我想出了以下示例来说明如何完成:

<html>
<head>
<title>Canvas</title>
</head>
<body>

<p>Click to draw lines</p>
<p>Click back in the green circle to close+fill</p>
<br/>
<canvas id="canvas" width=300 height=300></canvas>
<br/>
<button id="clear">Clear Canvas</button>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function () {
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
offset = $("#canvas").offset(),
storedLines = [],
polyLines = [],
start = {x: 0, y: 0},
radius = 7;

function canvasPosition(e) {
return {
x: parseInt(e.clientX - offset.left),
y: parseInt(e.clientY - offset.top)
};
}

$("#canvas").mousedown(function (e) {
var pos = canvasPosition(e);
if (hitStartCircle(pos)) {
polyLines.push(storedLines);
storedLines = [];
draw();
}
else
{
storedLines.push(pos);
update(pos);
}
})
.mousemove(function (e) {
update(canvasPosition(e));
});

// Draw completed polylines
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
$.each(polyLines, function (idx, polyLine) {
fillPolyline(polyLine);
});
}

// Update shape currently being drawn
function update(position) {
var len = storedLines.length;
if(len==0) return;

draw();
ctx.fillStyle = "green";
ctx.beginPath();
ctx.arc(storedLines[0].x, storedLines[0].y, radius, 0, 2 * Math.PI, false);
ctx.fill();

ctx.strokeStyle = "orange";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(storedLines[0].x, storedLines[0].y);
for(var i=1; i<len; ++i) {
ctx.lineTo(storedLines[i].x, storedLines[i].y)
}
ctx.lineTo(position.x, position.y);
ctx.stroke();
};

function hitStartCircle(pos) {
var start = storedLines[0] || {x:0, y:0},
dx = pos.x - start.x,
dy = pos.y - start.y;
return (dx * dx + dy * dy < radius * radius)
}

function fillPolyline(lines) {
ctx.strokeStyle = "red";
ctx.fillStyle = "blue";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(lines[0].x, lines[0].y);
for (var i = 0; i < lines.length; i++) {
ctx.lineTo(lines[i].x, lines[i].y);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
}

$("#clear").click(function () {
polyLines = [];
draw();
});
});
</script>
</body>
</html>

这将在用户移动鼠标光标时正确更新最后一段。

关于javascript - 在 Canvas 上绘制一个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15597100/

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