Files
sunny9898/task2/sketch3b/index.js
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

45 lines
986 B
JavaScript

let num_of_line = 0;
function keyPressed() {
if (keyCode === UP_ARROW) {
console.log("up_arrow");
num_of_line = num_of_line + 1;
} else if (keyCode === DOWN_ARROW) {
console.log("down_arrow");
num_of_line = num_of_line - 1;
}
if (num_of_line < 0) num_of_line = 0;
if (num_of_line > 9) num_of_line = 9;
}
function setup() {
createCanvas(400, 400);
background(220);
}
function draw() {
background(220);
var line_xys = [
[0, 0, 400, 400],
[400, 0, 0, 400],
];
if (num_of_line > 0) line(...line_xys[0]);
if (num_of_line > 1) line(...line_xys[1]);
if (num_of_line > 2) {
var need_to_draw = num_of_line - 2;
for (var i = 1; i <= need_to_draw; i++) {
var step = 400 / (need_to_draw + 1);
var top_left = 0;
var bottom_right = 400;
var top_x = top_left + step * i;
var bottom_x = bottom_right - step * i;
line(top_x, 0, bottom_x, 400);
console.log({ step, top_x, bottom_x });
}
}
}