-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
248 lines (218 loc) · 6.06 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// HOW TO USE
// predictWebcam(video) will start predicting landmarks
// pass a video MediaElement using createCapture
// make sure to call predictWebcam as a callback to createCapture
// this ensures the video is ready
// parts index:
// https://developers.google.com/mediapipe/solutions/vision/pose_landmarker/index
let capture;
let loadedCamera;
let captureEvent;
let confidence = 0.0;
let yOff = 0;
let defaultWeight = 50;
let lerpRate = 0.4;
let madeClone = false;
let lerpLandmarks = [];
let videGraphics;
//font stuff
let variableText;
let weightSlider;
let slantSlider;
let crsvSlider;
let monoSlider;
let caslSlider;
// slider values
let slant;
let crsv;
let mono;
let casl;
let fingerIsClicked = false;
let fingerClickPos = {
x: 0,
y: 0,
};
let textPos = {
x: 0,
y: 0,
};
let tFrames = [];
function setup() {
createCanvas(windowWidth, windowHeight);
captureWebcam();
vidGraphics = createGraphics(width, height);
variableText = select(".variable");
weightSlider = select(".weight");
slantSlider = select(".slant");
crsvSlider = select(".crsv");
monoSlider = select(".mono");
caslSlider = select(".casl");
}
function draw() {
// background(255);
let xDiff = width - capture.width;
let yDiff = height - capture.height;
let xM = capture.width + xDiff / 2;
let yM = capture.height + yDiff / 2;
clear();
image(capture, xDiff / 2, yDiff / 2);
fill(255, 255);
noStroke();
rect(0, 0, width, height);
function relPos(point) {
let pos = {
x: point.x * xM * 0.8,
y: point.y * xM * 0.8,
};
return pos;
}
// console.log({landmarks})
let first = true;
if (landmarks.length > 0) {
for (const hand of landmarks) {
first ? fill(255, 0, 0) : fill(0, 255, 0);
let tWidth;
if (first) {
tWidth = hand[4].y * yM - hand[8].y * yM;
}
for (const [index, m] of hand.entries()) {
if (first) {
stroke(100, 200, 255);
} else {
stroke(255, 200, 100);
}
if (index === 4 || index === 8 || index === 12) {
stroke(255, 0, 0);
}
if ((index === 4 || index === 8) && first) {
if (tWidth < height/20) {
// find the finger clicked pos
// only happens at the moment fingers are clicked
if (!fingerIsClicked) fingerClickPos = relPos(hand[8]);
// set to true to prevent above happening in this loop
fingerIsClicked = true;
stroke(0, 255, 0);
// get the previous position and current position
let prev = textPos;
let pos = relPos(hand[8]);
// find differences between position and clicked pos
let diffX = pos.x - fingerClickPos.x;
let diffY = pos.y - fingerClickPos.y;
// move the text
textPos.x = prev.x + diffX;
textPos.y = prev.y + diffY;
// reset the position of fingerClick to prevent runaway movement
fingerClickPos = relPos(hand[8]);
} else {
fingerIsClicked = false;
}
}
noFill();
strokeWeight(abs(50 * m.z));
let x = m.x * capture.width + xDiff / 2;
let y = m.y * capture.height + yDiff / 2;
let z = m.z * 400;
circle(x, y, z);
}
// console.log(dist(hand[4].x, hand[8].x))
let weight;
let slant;
let mono;
let casl;
let crsv;
if (first) {
// variableText.style("font-size", tWidth + "px");
// variableText.style("line-height", tWidth + "px");
variableText.style("left", textPos.x + "px");
variableText.style("top", textPos.y + "px");
slant = map(hand[8].x * xM - hand[4].x * xM, 10, width / 6, 0, -15);
casl = map(hand[4].y * yM - hand[12].y * yM, height / 4, 10, 0, 1);
// crsv = map(hand[4].y * yM - hand[16].y * yM, height / 4, 10, 0, 1);
slantSlider.value(slant);
caslSlider.value(casl);
// crsvSlider.value(crsv);
} else {
weight = map(
hand[4].y * yM - hand[8].y * yM,
10,
height / 2,
300,
1000
);
mono = map(hand[4].y * yM - hand[12].y * yM, 10, height / 4, 0, 1);
weightSlider.value(weight);
monoSlider.value(mono);
}
first = false;
}
}
variableText.style("font-weight", weightSlider.value());
slant = slantSlider.value();
crsv = crsvSlider.value();
mono = monoSlider.value();
casl = caslSlider.value();
variableText.style(
"font-variation-settings",
"'slnt' " +
slant +
", 'CRSV' " +
crsv +
", 'MONO' " +
mono +
", 'CASL' " +
casl
);
}
function loc(point) {
let xDiff = width - capture.width;
let yDiff = height - capture.height;
return [
point.x * capture.width + xDiff / 2 - width / 2,
point.y * capture.height + yDiff / 2 - height / 2,
point.z * 1000,
];
}
function captureWebcam() {
capture = createCapture(
{
audio: false,
video: {
facingMode: "user",
},
},
function (e) {
captureEvent = e;
// do things when video ready
// until then, the video element will have no dimensions, or default 640x480
capture.srcObject = e;
setCameraDimensions();
predictWebcam(capture);
}
);
capture.elt.setAttribute("playsinline", "");
// capture.hide();
}
function setCameraDimensions() {
loadedCamera = captureEvent.getTracks()[0].getSettings();
// console.log("cameraDimensions", loadedCamera);
if (capture.width > capture.height) {
if(height > width) {
capture.size((capture.width / capture.height) * height, height);
} else {
capture.size(width, (capture.height / capture.width) * width);
}
} else {
capture.size(width, (capture.height / capture.width) * width);
}
// console.log(capture);
}
function getAngle(v0x, v0y, v1x, v1y) {
return atan2(v1y - v0y, v1x - v0x);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
setCameraDimensions();
}
function simpLerp(a, b, rate) {
return a + rate * (b - a);
}