[英]Javascript p5 Unable to draw line between two points
In javascript, using p5, I want to draw a line between two points, set by the mouse.在 javascript 中,使用 p5,我想在两点之间画一条线,由鼠标设置。 I'm using an array to store the mouse positions, then draw the line.
我正在使用一个数组来存储鼠标位置,然后画线。 However, I get a weird output when I log the array in the console.
但是,当我在控制台中记录数组时,我得到了一个奇怪的 output 。
What would the " empty x 2 " mean? “空 x 2 ”是什么意思? This is my code:
这是我的代码:
function mouseClicked(){
pointSize = slider.value();
stroke(color_picker.value());
strokeWeight(pointSize);
point(mouseX, mouseY);
if(selection === "line"){
line_points.push(mouseX, mouseY);
console.log(line_points);
console.log(line_points.length);
if(line_points.length = 4){
line(line_points[0],line_points[1],line_points[2],line_points[3]);
line_points = [];
}
}
}
The main issue in the code is the conditional代码中的主要问题是条件
if(line_points.length = 4)
should be应该
if(line_points.length ==== 4)
as we want to compare length to 4. The original code sets the length of the array to 4 which leaves the array with some empty positions, thus producing the empty slots message when the array is output to the console.因为我们想将长度与 4 进行比较。原始代码将数组的长度设置为 4,从而使数组具有一些空位置,从而在数组为 output 时向控制台生成空槽消息。
function setup(){ const cnv = createCanvas(200, 200); } var line_points = []; function draw(){ } function mouseClicked(){ stroke(0); point(mouseX, mouseY); line_points.push(mouseX, mouseY); console.log(line_points); console.log(line_points.length); if(line_points.length === 4){ line(line_points[0],line_points[1],line_points[2],line_points[3]); line_points = []; } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.