xxxxxxxxxx
172
// global variables
let modelHand;
let thumbTip;
let indexTip;
let middleTip;
let ringTip;
let pinkyTip;
let locations = [
[36, 323],
[126, 130],
[205, 94],
[275, 145],
[336, 228],
]; // stores keypoint coordinates
let pointColors = ["red", "orange", "pink", "green", "blue"];
let keyPointObjs = []; // stores
// sketch functions
let resultPoints = {
thumbTip: "result[0].thumb_tip",
indexTip: "result[0].index_finger_tip",
middleTip: "result[0].middle_finger_tip",
ringTip: "result[0].ring_finger_tip",
pinkyTip: "result[0].pinky_finger_tip",
// add in rest and get correct selectors for handpose sketches
};
function preload() {
modelHand400 = loadImage("modelImages/modelHand400.png");
//frameRate(10);
}
function setup() {
noStroke()
canvas = createCanvas(900, 700);
//console.log(locations.thumbCoords[0])
//image(modelHand, 0, 0, modelHand.width, modelHand.height);
// create a 'keyPointObj' object for each finger and push to 'keyPointObjs' dictionary
initiateKeypointObjs()
}
function draw() {
background("#7b60a6");
fill("white")
rect(-5, 40, modelHand400.width + 15, modelHand400.height + 15, 20);
image(modelHand400, 0, 50, modelHand400.width, modelHand400.height);
drawResultContainer()
drawEmptyPoints();
noStroke();
for (let i = 0; i < keyPointObjs.length; i++) {
keyPointObjs[i].drawCircle();
}
mouseOver();
}
function mousePressed() {
for (let i = 0; i < keyPointObjs.length; i++) {
keyPointObjs[i].clicked(mouseX, mouseY);
}
}
function mouseOver() {
for (let i = 0; i < keyPointObjs.length; i++) {
keyPointObjs[i].hovering(mouseX, mouseY);
}
}
class keyPointObj {
constructor(x, y, id, selected, hovered, color, r) {
this.id = id;
this.selected = selected;
this.hovered = hovered;
this.color = color;
this.x = x;
this.y = y;
this.r = r;
}
drawCircle() {
if (this.selected || this.hovered) {
fill(this.color);
ellipse(this.x, this.y, this.r * 2, this.r * 2);
} else {
// fill("white");
// ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
}
// class function to control what happens when obj instance is clicked
clicked(px, py) {
let d = dist(px, py, this.x, this.y);
if (d < this.r) {
this.selected = !this.selected;
this.drawCircle();
console.log(resultPoints.thumbTip);
}
}
hovering(px, py) {
let d = dist(px, py, this.x, this.y);
if (d < this.r) {
this.hovered = true;
//console.log("hovering");
getMouseCoords(resultPoints[this.id]);
} else {
this.hovered = false;
}
this.drawCircle();
}
}
//utility functions below
function getMouseCoords(keyPointSelector) {
//code for finding coords
fill(255, 60, 100);
//text("(" + mouseX + ", " + mouseY + ")", mouseX, mouseY);
fill("#d4c8bb");
rect(mouseX + 20, mouseY -60, 250, 60, 20)
//ellipse(mouseX + 65, mouseY - 30, 120, 50);
textSize(20)
fill("white");
text(keyPointSelector, mouseX + 35, mouseY - 25);
noStroke();
}
function drawEmptyPoints() {
// instead of drawing a bunch of white ellipses in the else statement of the drawCircle function inside the keyPointsObj class, use this to return the diagram to have empty circles when unhovering
for (let curr_point = 0; curr_point < locations.length; curr_point++) {
fill(pointColors[curr_point]);
//console.log(point);
// console.log(locations[curr_point])
// console.log(locations[curr_point][0])
ellipse(locations[curr_point][0], locations[curr_point][1], 27, 27);
fill("white")
drawDeselect()
}
}
function drawDeselect(){
for (let curr_point = 0; curr_point < locations.length; curr_point++) {
fill("white")
noStroke()
ellipse(locations[curr_point][0], locations[curr_point][1], 20, 20);
}
}
function initiateKeypointObjs(){
keyPointObjs.push(
new keyPointObj(locations[0][0], locations[0][1], "thumbTip", false, false, "red", 10)
);
keyPointObjs.push(
new keyPointObj(locations[1][0], locations[1][1], "indexTip", false, false, "orange", 10)
);
keyPointObjs.push(
new keyPointObj(locations[2][0], locations[2][1], "middleTip", false, false, "pink", 10)
);
keyPointObjs.push(
new keyPointObj(locations[3][0], locations[3][1], "ringTip", false, false, "green", 10)
);
keyPointObjs.push(
new keyPointObj(locations[4][0], locations[4][1], "pinkyTip", false, false, "blue", 10)
);
}
function drawResultContainer(){
fill("white")
rect(width/2, 40, modelHand400.width + 15, modelHand400.height + 15, 20);
}