gpt4 book ai didi

javascript - Canvas - 如何在新的 "route"上从单击到单击绘制线条并更改颜色

转载 作者:行者123 更新时间:2023-11-30 14:15:46 49 4
gpt4 key购买 nike

我有一个 Canvas (见下图),我已经被困了一段时间试图在用户点击 Canvas 内部时绘制的圆圈之间画线。

从照片中的示例可以看出,这也是我希望在 Canvas 中包含的内容:

用户点击蓝色 - 首先点蓝色 (x),继续沿着网格在 Canvas 内点击,添加更多蓝色 (x's),它们之间有蓝线。(我也不知道如何画 X 而不是我目前使用的圆圈)

用户点击红色 - 从第一个点红色 (o) 开始,继续沿着网格在 Canvas 内点击,添加更多红色 (o),它们之间有红线。

用户点击“Sterge” - Canvas 清除,网格保留。

[ see canvas here ]到目前为止我的代码:

var needFirstPoint = true;
var lineTo = [{}];

function drawNextLine(ctx, x, y) {
if (needFirstPoint) {
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x, y);
needFirstPoint = false;

}
else {
ctx.beginPath();
ctx.lineTo(x, y);
ctx.stroke();
}
}

$(document).ready(function () {
var canvas = $('#myCanvas').get(0);
if (!canvas.getContext) { return; }
var ctx = canvas.getContext('2d');

var drawGrid = function (w, h, id) {
ctx.canvas.width = w;
ctx.canvas.height = h;
ctx.lineWidth = .1;

for (x = 15; x <= w; x += 60) {
ctx.moveTo(x, 0);
ctx.lineTo(x, h);
for (y = 20; y <= h; y += 20) {
ctx.moveTo(0, y);
ctx.lineTo(w, y);
}
}
ctx.stroke();

};

drawGrid(450, 280, "myCanvas");

var drawChart = function () {
ctx.arc(lineTo[lineTo.length - 1].x, lineTo[lineTo.length - 1].y, 6, 0, Math.PI * 2, false)
ctx.stroke();
}

drawChart();

document.getElementById('move').addEventListener('click', function () {

$('#myCanvas').on('click', function (e) {

var offset = $(this).offset();
var x = e.pageX - offset.left;
var y = e.pageY - offset.top;
ctx.strokeStyle = "red";
drawNextLine(ctx, x, y);

lineTo.push({ x: x, y: y });
drawChart();

});
});

document.getElementById('move1').addEventListener('click', function () {

$('#myCanvas').on('click', function (e) {

var offset = $(this).offset();
var x = e.pageX - offset.left;
var y = e.pageY - offset.top;
ctx.strokeStyle = "blue";
drawNextLine1(ctx, x, y);

lineTo.push({ x: x, y: y });
drawChart();

});
});

});

谢谢。

最佳答案

Ì简化了您的代码。我希望你不介意。接下来是我的代码。我希望这就是您想要的。

var lineTo = [];

$(document).ready(function() {
var canvas = $("#myCanvas").get(0);
if (!canvas.getContext) {
return;
}
var ctx = canvas.getContext("2d");

var drawGrid = function(w, h, id) {
ctx.canvas.width = w;
ctx.canvas.height = h;
ctx.lineWidth = 0.1;
ctx.beginPath();
for (x = 15; x <= w; x += 60) {
ctx.moveTo(x, 0);
ctx.lineTo(x, h);
for (y = 20; y <= h; y += 20) {
ctx.moveTo(0, y);
ctx.lineTo(w, y);
}
}
ctx.stroke();
};

drawGrid(450, 280, "myCanvas");



ctx.lineWidth = 1;



var drawChart = function(x, y) {
ctx.beginPath();
ctx.arc(x, y, 6, 0, Math.PI * 2);
ctx.strokeStyle = "red";
ctx.stroke();
};

$("#myCanvas").on("click", function(e) {
var offset = $(this).offset();
var x = e.pageX - offset.left;
var y = e.pageY - offset.top;

drawChart(x, y);

if (lineTo.length > 0) {
var last = lineTo[lineTo.length - 1];
ctx.beginPath();
ctx.moveTo(last.x, last.y);
ctx.lineTo(x, y);
ctx.strokeStyle = "blue";
ctx.stroke();
}

lineTo.push({ x: x, y: y });
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="myCanvas"></canas>

更新

OP 正在评论:

I need to have 2 buttons : Left and Right. When I click on Left, the Right path is stopped and Left path begins from a independent point.

这个我不是很懂。这是我的理解:

我知道你需要一个按钮来删除最后一个点 sterge

您还需要一个按钮 dreapta 来开始一条新路径。

这是我的代码。请阅读评论。

//this is an array of arrays
//when I click on the canvas a new point is pushed on the last array of this array
var ry = [[]];


var canvas = document.querySelector("#myCanvas");
let w = (canvas.width = 450);
let h = (canvas.height = 280);

var ctx = canvas.getContext("2d");

drawGrid();



myCanvas.addEventListener("click", e => {
var offset = canvas.getBoundingClientRect();
var x = e.clientX - offset.left;
var y = e.clientY - offset.top;
//a new point is pushed on the last array of ry
ry[ry.length - 1].push({ x: x, y: y });
// delete everything
ctx.clearRect(0, 0, w, h);
// draw everything
drawGrid();
drawChart();
});

sterge.addEventListener("click", e => {
//when sterge is clicked the last point from the last array is deleted
if (ry[ry.length - 1].length > 0) {
ry[ry.length - 1].pop();
} else {
//if the last array is empty I delete this array
ry.pop();
//and then I delete the last point from the last array
ry[ry.length - 1].pop();
}
// delete everything
ctx.clearRect(0, 0, w, h);
// draw everything
drawGrid();
drawChart();
});

dreapta.addEventListener("click", e => {
//when dreapta is clicked, a new array is pushed into the ry
ry.push([]);
});

function drawGrid() {
ctx.strokeStyle = "black";
ctx.lineWidth = 0.1;
ctx.beginPath();
for (x = 15; x <= w; x += 60) {
ctx.moveTo(x, 0);
ctx.lineTo(x, h);
for (y = 20; y <= h; y += 20) {
ctx.moveTo(0, y);
ctx.lineTo(w, y);
}
}
ctx.stroke();
}

function drawChart() {
ctx.lineWidth = 1;
// for every array in the ry array
for (let index = 0; index < ry.length; index++) {
// for every point in the ry[index]
for (let i = 0; i < ry[index].length; i++) {
let l = ry[index][i];
// draw the circle
drawCircle(l.x, l.y);
// draw the line
if (i > 0) {
let last = ry[index][i - 1];
ctx.beginPath();
ctx.moveTo(last.x, last.y);
ctx.lineTo(l.x, l.y);
ctx.strokeStyle = "blue";
ctx.stroke();
}
}
}
}

function drawCircle(x, y) {
ctx.beginPath();
ctx.arc(x, y, 6, 0, Math.PI * 2);
ctx.strokeStyle = "red";
ctx.stroke();
}
<canvas id="myCanvas"></canvas>
<p><input type="button" value="dreapta" id="dreapta" />
<input type="button" value="sterge" id="sterge" />
</p>

关于javascript - Canvas - 如何在新的 "route"上从单击到单击绘制线条并更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53584576/

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