Skip to content

Commit 1e9e16f

Browse files
committed
add loading screen + press "r" to refresh with new seed
1 parent b09bc79 commit 1e9e16f

File tree

1 file changed

+74
-51
lines changed

1 file changed

+74
-51
lines changed

sketch.js

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ let imgRightEye;
1010
let imgMouth;
1111
let imgBackground;
1212

13+
let loadCount = 0;
14+
1315
const faceApiOptions = {
1416
withLandmarks: true,
1517
withDescriptors: false
1618
}
1719

18-
function preload() {
20+
function setup() {
21+
createCanvas(windowWidth, windowHeight);
22+
1923
let hlRandomSeed = hl.random(1e12);
2024
randomSeed(hlRandomSeed);
2125

@@ -24,58 +28,77 @@ function preload() {
2428

2529
let backgroundId = random(backgrounds_data);
2630

27-
hl.token.setTraits({
28-
"Base portrait": baseId[2] + ", " + baseId[3],
29-
"Left eye": leftEyeId[2] + ", " + leftEyeId[3],
30-
"Right eye": rightEyeId[2] + ", " + rightEyeId[3],
31-
"Mouth": mouthId[2] + ", " + mouthId[3],
32-
"Background": backgroundId[2] + ", " + backgroundId[3]
33-
});
34-
35-
imgBase = loadImage("portraits/"+baseId[0]+"."+baseId[1], () => console.log("Base portrait: " + baseId[2] + ", " + baseId[3]));
36-
imgLeftEye = loadImage("portraits/"+leftEyeId[0]+"."+leftEyeId[1], () => console.log("Left eye: " + leftEyeId[2] + ", " + leftEyeId[3]));
37-
imgRightEye = loadImage("portraits/"+rightEyeId[0]+"."+rightEyeId[1], () => console.log("Right eye: " + rightEyeId[2] + ", " + rightEyeId[3]));
38-
imgMouth = loadImage("portraits/"+mouthId[0]+"."+mouthId[1], () => console.log("Mouth: " + mouthId[2] + ", " + mouthId[3]));
39-
40-
bodyPixBase = loadJSON("ml/bodyPix_"+baseId[0]+".json");
41-
faceApiBase = loadJSON("ml/faceApi_"+baseId[0]+".json");
42-
faceApiLeftEye = loadJSON("ml/faceApi_"+leftEyeId[0]+".json");
43-
faceApiRightEye = loadJSON("ml/faceApi_"+rightEyeId[0]+".json");
44-
faceApiMouth = loadJSON("ml/faceApi_"+mouthId[0]+".json");
45-
46-
imgBackground = loadImage("backgrounds/"+backgroundId[0]+"."+backgroundId[1], () => console.log("Background: " + backgroundId[2] + ", " + backgroundId[3]));
31+
imgBase = loadImage("portraits/"+baseId[0]+"."+baseId[1], () => {console.log("Base portrait: " + baseId[2] + ", " + baseId[3]); loadCount++});
32+
imgLeftEye = loadImage("portraits/"+leftEyeId[0]+"."+leftEyeId[1], () => {console.log("Left eye: " + leftEyeId[2] + ", " + leftEyeId[3]); loadCount++});
33+
imgRightEye = loadImage("portraits/"+rightEyeId[0]+"."+rightEyeId[1], () => {console.log("Right eye: " + rightEyeId[2] + ", " + rightEyeId[3]); loadCount++});
34+
imgMouth = loadImage("portraits/"+mouthId[0]+"."+mouthId[1], () => {console.log("Mouth: " + mouthId[2] + ", " + mouthId[3]); loadCount++});
35+
36+
bodyPixBase = loadJSON("ml/bodyPix_"+baseId[0]+".json", () => loadCount++);
37+
faceApiBase = loadJSON("ml/faceApi_"+baseId[0]+".json", () => loadCount++);
38+
faceApiLeftEye = loadJSON("ml/faceApi_"+leftEyeId[0]+".json", () => loadCount++);
39+
faceApiRightEye = loadJSON("ml/faceApi_"+rightEyeId[0]+".json", () => loadCount++);
40+
faceApiMouth = loadJSON("ml/faceApi_"+mouthId[0]+".json", () => loadCount++);
41+
42+
imgBackground = loadImage("backgrounds/"+backgroundId[0]+"."+backgroundId[1], () => {console.log("Background: " + backgroundId[2] + ", " + backgroundId[3]); loadCount++});
4743
}
4844

49-
function setup() {
50-
let ratio = imgBase.height/imgBase.width;
51-
let W = windowWidth, H = windowHeight;
52-
if (W*ratio < H) createCanvas(W, W*ratio);
53-
else createCanvas(H/ratio, H);
54-
55-
pixelDensity(2);
56-
noLoop();
57-
58-
if (imgBase.width < width) imgBase.resize(width, 0);
59-
if (imgBase.height < height) imgBase.resize(0, height);
60-
if (imgLeftEye.width < width) imgLeftEye.resize(width, 0);
61-
if (imgLeftEye.height < height) imgLeftEye.resize(0, height);
62-
if (imgRightEye.width < width) imgRightEye.resize(width, 0);
63-
if (imgRightEye.height < height) imgRightEye.resize(0, height);
64-
if (imgMouth.width < width) imgMouth.resize(width, 0);
65-
if (imgMouth.height < height) imgMouth.resize(0, height);
66-
67-
imgBackground.resize(0, height);
68-
69-
image(imgBackground, random(width-imgBackground.width), 0);
70-
71-
drawingContext.shadowOffsetX = 0;
72-
drawingContext.shadowOffsetY = 0;
73-
drawingContext.shadowBlur = width/100;
74-
drawingContext.shadowColor = "#00000095";
75-
76-
cutOutline();
77-
drawFacialFeatures();
78-
hl.token.capturePreview();
45+
function draw() {
46+
background(0);
47+
frameRate(20);
48+
noStroke();
49+
fill(255);
50+
textAlign(LEFT, CENTER);
51+
let fonts = shuffle(["Arial", "Verdana", "Tahoma", "Times New Roman", "Georgia", "Garamond", "Courier New"]);
52+
let letters = "Loading".split("");
53+
let x = width/2-40;
54+
for (let l of letters) {
55+
textFont(fonts.pop(), 24);
56+
text(l, x, height/2);
57+
x += textWidth(l);
58+
}
59+
//text("Loading...", width/2, height/2);
60+
61+
if (loadCount == 10) {
62+
console.log(loadCount)
63+
let ratio = imgBase.height/imgBase.width;
64+
let W = windowWidth, H = windowHeight;
65+
if (W*ratio < H) resizeCanvas(W, W*ratio);
66+
else resizeCanvas(H/ratio, H);
67+
68+
pixelDensity(2);
69+
noLoop();
70+
71+
if (imgBase.width < width) imgBase.resize(width, 0);
72+
if (imgBase.height < height) imgBase.resize(0, height);
73+
if (imgLeftEye.width < width) imgLeftEye.resize(width, 0);
74+
if (imgLeftEye.height < height) imgLeftEye.resize(0, height);
75+
if (imgRightEye.width < width) imgRightEye.resize(width, 0);
76+
if (imgRightEye.height < height) imgRightEye.resize(0, height);
77+
if (imgMouth.width < width) imgMouth.resize(width, 0);
78+
if (imgMouth.height < height) imgMouth.resize(0, height);
79+
80+
imgBackground.resize(0, height);
81+
82+
image(imgBackground, random(width-imgBackground.width), 0);
83+
84+
drawingContext.shadowOffsetX = 0;
85+
drawingContext.shadowOffsetY = 0;
86+
drawingContext.shadowBlur = width/100;
87+
drawingContext.shadowColor = "#00000095";
88+
89+
cutOutline();
90+
drawFacialFeatures();
91+
hl.token.capturePreview();
92+
}
93+
}
94+
95+
function keyPressed() {
96+
if (key == "r") {
97+
loadCount = 0;
98+
console.clear();
99+
loop();
100+
setup();
101+
}
79102
}
80103

81104
// BACKGROUND

0 commit comments

Comments
 (0)