xxxxxxxxxx
138
let left = 0;
let right = 0;
let array = [];
class Circle {
constructor(circleX, circleY, circleSize) {
this.circleX = circleX;
this.circleY = circleY;
this.circleSize = circleSize;
this.alpha = 255;
}
}
let draggingTop = false;
let draggingRight = false;
let draggingBottom = false;
let draggingLeft = false;
function setup() {
createCanvas(windowWidth, windowHeight);
noFill();
textSize(10);
strokeWeight(1);
}
function draw() {
background(255, 236, 153);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("connected", 20, 30);
}
// if (mouseIsPressed) {
// if (draggingLeft) {
// left = 1;
// right = 0;
// } else if (draggingTop) {
// left = right = 1;
// } else if (draggingRight) {
// left = 0;
// right = 1;
// } else if (draggingBottom) {
// left = right = -1;
// }
// } else {
// left = right = 0;
// }
if (keyIsDown(LEFT_ARROW)) {
left = 1;
right = 0;
} else if (keyIsDown(UP_ARROW)) {
left = right = 1;
} else if (keyIsDown(RIGHT_ARROW)) {
left = 0;
right = 1;
} else if (keyIsDown(DOWN_ARROW)) {
left = right = -1;
} else {
left = right = 0;
}
for (let i = 0; i < array.length; i++) {
array[i].circleSize += 2;
array[i].alpha -= 6;
noStroke();
fill(255, array[i].alpha);
ellipse(array[i].circleX, array[i].circleY, array[i].circleSize);
}
array = array.filter((circle) => circle.alpha > 0);
}
function readSerial(data) {
if (data != null) {
let fromArduino = split(trim(data), ",");
let sendToArduino = left + "," + right + "\n";
writeSerial(sendToArduino);
}
}
function keyPressed() {
if (key == " ") {
setUpSerial();
}
if (key == "s") {
left = right = 0;
}
}
function mouseReleased() {
left = right = 0;
}
function mouseDragged() {
array.push(new Circle(mouseX, mouseY, 0));
if (pmouseY > mouseY) {
draggingTop = true;
draggingBottom = false;
} else if (pmouseY < mouseY) {
draggingTop = false;
draggingBottom = true;
} else {
draggingTop = false;
draggingBottom = false;
}
if (pmouseX > mouseX) {
draggingLeft = true;
draggingRight = false;
} else if (pmouseX < mouseX) {
draggingRight = true;
draggingLeft = false;
} else {
draggingLeft = false;
draggingRight = false;
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function keyTyped() {
if (key === "f") {
toggleFullscreen();
}
}
function toggleFullscreen() {
let fs = fullscreen(); // Get the current state
fullscreen(!fs); // Flip it!
}