xxxxxxxxxx
140
//mouse press for 1d array
//
let x = [];
let y = [];
let twod = [];
let point2d = [];
let first= false;
let second = false;
let third = false;
let prevMouseX = 0;
let prevMouseY = 0;
function setup() {
createCanvas(600, 600);
//noLoop();
}
function draw() {
background(255);
text("1. press mouse (1d array), 2. click on any key (2d array), or 3. click right arrow (point2d array)", 0, height/2);
if (first) {
background(200);
firstarray();
}
if (second) {
background(0);
secondarray();
}
if (third) {
background(100,200,100);
thirdarray();
}
}
function firstarray() {
beginShape();
noFill();
console.log(prevMouseX, mouseX);
if (first && (mouseX != prevMouseX) && (mouseY != prevMouseY)){
x.push(mouseX);
y.push(mouseY);
prevMouseX = mouseX;
prevMouseY = mouseY;
}
for (i=0; i<x.length; i++) {
curveVertex(x[i],y[i], 2);
}
endShape();
if (x.length > 100) {
x.splice(0,1);
y.splice(0,1);
}
}
function secondarray() {
beginShape();
stroke("red");
//noFill();
if (second){
twod.push([mouseX,mouseY]);
prevMouseX = mouseX;
prevMouseY = mouseY;
}
for (i=0; i<twod.length; i++) {
curveVertex(twod[i][0], twod[i][1], 2);
}
endShape();
console.log(twod.length);
if (twod.length > 100) {
twod.splice(0,1);
}
}
function thirdarray() {
beginShape();
stroke("blue");
//noFill();
if (point2d){
point2d.push(new points(mouseX, mouseY));
}
for (i=0; i<point2d.length; i++) {
point2d[i].show();
}
endShape();
console.log(point2d.length);
if (point2d.length > 100) {
point2d.splice(0,1);
}
}
class points {
constructor (i,j) {
this.i = i;
this.j = j;
}
show () {
vertex(this.i, this.j);
}
}
function mousePressed() {
first = true;
second = false;
third = false;
}
function keyPressed() {
//any key
if (keyCode === RIGHT_ARROW) {
third = true;
second= false;
first = false;
} else {
second = true;
first = false;
third = false;
}
}