-
Notifications
You must be signed in to change notification settings - Fork 0
/
plague-doctors-test-page.html
875 lines (804 loc) · 24 KB
/
plague-doctors-test-page.html
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plague Doctors Test Page</title>
<style>
body {
position: absolute;
width: 100%;
height: 100%;
}
h1,
h2,
p {
position: relative;
text-align: center;
width: fit-content;
max-width: 500px;
margin: 36px auto;
z-index: 2;
}
.test-div {
position: absolute;
width: 100px;
height: 100px;
max-width: 20vw;
max-height: 20vw;
padding: 16px;
border: 2px solid #888;
background-color: #eee;
color: #eee;
z-index: 1;
}
.test-div-1 {
top: 80%;
left: 15%;
}
.test-div-2 {
top: 60%;
left: 10%;
}
.test-div-3 {
top: 55%;
left: 60%;
}
.test-div-4 {
top: 82%;
left: 55%;
}
</style>
<style>
/* COPY AND PASTE YOUR CSS HERE */
/* .plague-doctor-element {
position: fixed;
height: 52px;
width: 44px;
transform: scale(1);
opacity: 1;
transition: opacity 0.5s ease, top 0.1s, left 0.1s;
cursor: crosshair;
z-index: 100;
}
.plague-doctor-element-init.plague-doctor-element-init {
opacity: 0;
transition: opacity 0.5s ease top 0.1s, left 0.1s;
}
.image-reversed.image-reversed {
transform: scaleX(-1);
}
.dead.dead {
transform: rotate(-90deg);
}
.plague-doctor-controls {
position: fixed;
right: 0;
top: 0;
display: flex;
flex-direction: column;
padding: 12px;
max-width: 100px;
transform: translateY(-100%);
transition: transform 0.4s ease;
}
.plague-doctor-controls button {
margin: 8px;
}
.doctors-4tv-logo {
position: absolute;
top: 100%;
height: 40px;
align-self: flex-end;
}
.plague-doctor-controls:hover {
transform: translateY(0px);
}
@media only screen and (max-device-width: 768px) {
.plague-doctor-element {
transform: scale(2);
}
.image-reversed.image-reversed {
transform: scaleX(-2) scaleY(2);
}
} */
</style>
<link rel="stylesheet" href="./4tv.css" />
<link rel="stylesheet" href="./plague-doctors.css" />
</head>
<body class="test-page">
<aside class="page-controls plague-doctor-ignore">
<label>Page color</label>
<select id="page-color-select">
<option value="">None</option>
<option value="purple">Purple</option>
<option value="yellow">Yellow</option>
</select>
<script>
colorSelectHandler = (e) => {
console.log(e);
document.body.className = "test-page";
document.body.classList.add(`${e.target.value}-page`);
};
document
.querySelector("#page-color-select")
.addEventListener("change", (e) => colorSelectHandler(e));
</script>
<button>Test Button</button>
</aside>
<main class="page-container">
<h1 class="title-font">Plague Doctors</h1>
<h2>A project by and for the</h2>
<h2>
<a
class="title-font"
target="_blank"
href="https://fourthievesvinegar.org/"
>Four Thieves Vinegar Collective</a
>
</h2>
<p>
But you can use it too!
<a
target="_blank"
href="https://github.com/FourThievesVinegar/plague-doctors"
>Get the script and styles from the GitHub repo</a
>
or add
<a
target="_blank"
href="https://plague-doctors.netlify.app/plague-doctors.js"
>the script</a
>
and
<a
target="_blank"
href="https://plague-doctors.netlify.app/plague-doctors.css"
>the styles</a
>
to your page!
</p>
<p>
<code>
<link rel="stylesheet"
href="https://plague-doctors.netlify.app/plague-doctors.css"/>
</code>
<br /><br />
<code>
<script type="text/javascript"
src="https://plague-doctors.netlify.app/plague-doctors.js"></script>
</code>
</p>
<div class="test-div test-div-1">Hack the planet!</div>
<div class="test-div test-div-2">Food, shelter, and medicine for all!</div>
<div class="test-div test-div-3">Down with hierarchy!</div>
<div class="test-div test-div-4">We will get free.</div>
</main>
<script>
const IMAGE_URL_START = "https://plague-doctors.netlify.app/images/";
const ACTION_MAP = {
idle: 0,
head_wagging: 1,
looking: 1,
walking: 2,
running: 2,
whacking: 3,
coloring: 4,
shrugging: 5,
dying: 6,
dead: 0,
};
const ALLOWED_ACTIVITIES = [
"idle",
"head_wagging",
"walking",
"running",
"whacking",
"looking",
"coloring",
"shrugging",
"dying",
"dead",
];
const TARGETED_ACTIVITIES = {
walking: "to",
running: "to",
looking: "at",
coloring: "",
};
const SPEEDS = {
walking: 10,
running: 25,
};
const COLORING_INCREMENT = 64;
const COLORING_TARGETS = ["borderColor", "backgroundColor"];
const ALLOWED_COLORS = [
"red",
"orange",
"yellow",
"green",
"blue",
"purple",
];
const COLOR_MAP = {
red: { dark: "#ca1c1d", medium: "#e60000", light: "#ff2525" },
orange: { dark: "#ca671d", medium: "#e66300", light: "#ff8325" },
yellow: { dark: "#c0cb1d", medium: "#d9e600", light: "#f2ff25" },
green: { dark: "#55a83f", medium: "#55a83f", light: "#64bc2a" },
blue: { dark: "#1d96ca", medium: "#00a1e6", light: "#25beff" },
purple: { dark: "#a61dca", medium: "#b600e6", light: "#d225ff" },
};
const HEATBEAT_INTERVAL = 100;
const HEARTBEATS_TO_DIE = 12;
const HEARTBEATS_TO_WHACK = 6;
const HEARTBEATS_TO_COLOR = 9;
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function getRandomColor() {
return ALLOWED_COLORS[getRandomInt(ALLOWED_COLORS.length)];
}
function randomly(chance, outcome) {
switch (getRandomInt(chance)) {
case 1:
outcome();
return true; //We return true if the outcome happened
}
}
var plagueDoctorTemplate = function plagueDoctorTemplate() {
let myElement = null; // The element rendering the gif
let debugging = false; // Should we output debugging logs?
let dead = false;
let hurrying = false;
let currentActivity = "idle";
// How we are rendered
let isReversed = false; // Is the plague dr facing left?
let currentColor = getRandomColor(); // What color are we currently?
let currentAction = "head_wag"; // What is the dr doing?
// When we are traveling about the page
let targetElement; // What element are we walking to?
let locationX = 0;
let locationY = 0;
let velocityX = 0;
let velocityY = 0;
let cleaningUp = false;
let activityHeartbeatCount;
//
// UTILITY FUNCTIONS
//
let updateSpriteLocation = () => {
if (debugging) {
console.log(myElement);
}
myElement.style.left = locationX + "px";
myElement.style.top = locationY + "px";
};
let statusString = () => {
return `At (${locationX},${locationY}), ${currentActivity} ${
Object.keys(TARGETED_ACTIVITIES).includes(currentActivity)
? `${
TARGETED_ACTIVITIES[currentActivity]
} ${targetElement.classList.toString()}`
: ""
}`;
};
let setDebugging = (value) => {
debugging = value;
};
let setCleaningUp = (value) => {
cleaningUp = value;
};
let setHurrying = (value) => {
hurrying = value;
};
let setLocation = (x, y) => {
locationX = x;
locationY = y;
};
let getElementBottom = (element) => {
const rect = element.getBoundingClientRect();
return rect.top + rect.height;
};
//
// SPRITE MANAGEMENT
//
let buildImageUrl = (action, color) => {
if (!ALLOWED_COLORS.includes(color)) {
console.error("Error: non-valid color specified");
}
return `${IMAGE_URL_START}plague-doctor-gif${ACTION_MAP[action]}_${color}.gif`;
};
let setSprite = (action, color = currentColor) => {
if (currentAction === action && color === currentColor) {
return; //We are already using the correct src. No need to change
}
myElement.style.transform = "";
myElement.src = buildImageUrl(action, color);
currentAction = action;
currentColor = color;
};
//
// NAVIGATION
//
let pickModifiedElement = () => {
const whackedElements =
document.getElementsByClassName("doctor-whacked");
const coloredElements =
document.getElementsByClassName("doctor-colored");
if (whackedElements && whackedElements.length) {
return whackedElements[getRandomInt(whackedElements.length)];
}
if (coloredElements && coloredElements.length) {
return coloredElements[getRandomInt(coloredElements.length)];
}
return null;
};
let pickRandomElement = () => {
let randomElement;
do {
randomElement = document.elementFromPoint(
getRandomInt(window.innerWidth),
getRandomInt(window.innerHeight)
);
} while (
!randomElement ||
randomElement === document.body ||
(randomElement && randomElement.tagName === "HTML") ||
randomElement.window ||
randomElement.classList.contains("plague-doctor-element") ||
randomElement.classList.contains("plague-doctor-controls") ||
randomElement.classList.contains("plague-doctor-ignore")
);
return randomElement;
};
let getVectorToElement = (theElement) => {
if (!theElement || !myElement) {
return { x: 0, y: 0 };
}
let myBox = myElement.getBoundingClientRect();
let theirBox = theElement.getBoundingClientRect();
let xDistance = theirBox.x - myBox.x + myBox.width;
let yDistance = theirBox.y + theirBox.height - (myBox.y + myBox.height);
return { x: xDistance, y: yDistance };
};
let setVelocity = (vector) => {
const totalDistance = Math.abs(vector.x) + Math.abs(vector.y);
const speed = SPEEDS[currentActivity];
if (!speed) {
return;
}
velocityX = speed * (vector.x / totalDistance);
velocityY = speed * (vector.y / totalDistance);
if (velocityX < -0.5) {
isReversed = true;
myElement.classList.add("image-reversed");
} else {
isReversed = false;
myElement.classList.remove("image-reversed");
}
};
let checkArrived = (vector) => {
if (Math.abs(vector.y) <= 15 && Math.abs(vector.x) <= 15) {
//We are near the destination
return true;
}
};
//
// BASIC BEHAVIORS
//
let stop = () => {
activityHeartbeatCount = 0;
currentActivity = "idle";
velocityX = 0;
velocityY = 0;
setSprite("idle");
};
let die = () => {
setSprite("dying");
currentActivity = "dying";
if (activityHeartbeatCount > HEARTBEATS_TO_DIE) {
dead = true;
// becomeCorpse() //Too sad looking at the little corpses
vanish();
}
};
let vanish = () => {
myElement.style.display = "none";
};
let becomeCorpse = () => {
setSprite("idle");
myElement.classList.add("dead");
};
let changeColor = (color) => {
currentColor = color || getRandomColor();
};
let move = () => {
locationX += velocityX;
locationY += velocityY;
};
//
// INTERACT WITH ELEMENTS
//
let actOnElement = () => {
if (cleaningUp) {
activityHeartbeatCount = 0;
currentActivity = "cleaning";
return;
}
switch (getRandomInt(3)) {
case 0:
lookAtElement();
break;
case 1:
whackElement();
break;
case 2:
colorElement();
break;
}
};
let lookAtElement = () => {
currentActivity = "looking";
setSprite("looking");
};
let walkToElement = (theElement) => {
if (theElement) {
if (debugging) {
console.log("Walking to", theElement);
}
targetElement = theElement;
currentActivity = "walking";
setSprite("walking");
}
//We actually want to pick a point here, not the element itself.
const vector = getVectorToElement(targetElement);
if (checkArrived(vector)) {
stop();
actOnElement();
} else {
setVelocity(vector);
move();
}
};
let runToElement = (theElement) => {
if (theElement) {
if (debugging) {
console.log("Running to", theElement);
}
targetElement = theElement;
currentActivity = "running";
setSprite("walking");
}
//We might actually want to pick a point here, not the element itself.
const vector = getVectorToElement(targetElement);
if (checkArrived(vector)) {
stop();
actOnElement();
} else {
setVelocity(vector);
move();
}
};
let whackElement = () => {
let doctorWhacks;
if (currentActivity !== "whacking") {
currentActivity = "whacking";
setSprite("whacking");
activityHeartbeatCount = 0;
if (window.getComputedStyle(targetElement).display === "inline") {
targetElement.style.display = "inline-block";
}
}
if (
activityHeartbeatCount > 0 &&
activityHeartbeatCount % HEARTBEATS_TO_WHACK === 0
) {
doctorWhacks =
parseInt(targetElement.getAttribute("doctor-whacks")) || 0;
if (isReversed) {
doctorWhacks--;
} else {
doctorWhacks++;
}
targetElement.setAttribute("doctor-whacks", doctorWhacks);
targetElement.style.transform = `rotate(${doctorWhacks}deg)`;
}
if (doctorWhacks === 0) {
targetElement.classList.remove("doctor-whacked");
} else {
targetElement.classList.add("doctor-whacked");
}
};
let colorElement = () => {
let coloringTarget =
COLORING_TARGETS[getRandomInt(COLORING_TARGETS.length)];
let elementColor;
let elementColoringCount;
if (currentActivity !== "coloring") {
currentActivity = "coloring";
setSprite("coloring");
activityHeartbeatCount = 0;
}
if (
activityHeartbeatCount > 0 &&
activityHeartbeatCount % HEARTBEATS_TO_COLOR === 0
) {
elementColor = targetElement.getAttribute(
`doctor-${coloringTarget}-color`
);
elementColoringCount = parseInt(
targetElement.getAttribute(
`doctor-${coloringTarget}-coloring-count`
)
);
if (
elementColor &&
elementColoringCount &&
elementColor == currentColor
) {
elementColoringCount += COLORING_INCREMENT;
} else {
elementColor = currentColor;
elementColoringCount = COLORING_INCREMENT;
}
if (coloringTarget === "borderColor") {
targetElement.style.borderWidth = "2px";
targetElement.style.borderStyle = "solid";
}
if (elementColoringCount > 256) {
stop(); // We have colored to the maximum
changeColor(); // And we are bored of this color now
return;
}
targetElement.setAttribute(
`doctor-${coloringTarget}-color`,
currentColor
);
targetElement.setAttribute(
`doctor-${coloringTarget}-coloring-count`,
elementColoringCount
);
targetElement.style[coloringTarget] = `${
COLOR_MAP[currentColor].dark
}${(elementColoringCount - 1).toString(16)}`;
targetElement.classList.add("doctor-colored");
}
};
let cleanUpElement = () => {
const elementWhacks = parseInt(
targetElement.getAttribute("doctor-whacks")
);
if (elementWhacks && elementWhacks !== 0) {
setSprite("whacking");
if (
activityHeartbeatCount > 0 &&
activityHeartbeatCount % HEARTBEATS_TO_WHACK === 0
) {
if (elementWhacks <= 3 && elementWhacks >= -3) {
targetElement.setAttribute("doctor-whacks", 0);
targetElement.style.transform = `rotate(0deg)`;
targetElement.classList.remove("doctor-whacked");
activityHeartbeatCount = 0;
} else {
if (elementWhacks > 3) {
targetElement.setAttribute("doctor-whacks", elementWhacks - 3);
targetElement.style.transform = `rotate(${
elementWhacks - 3
}deg)`;
} else {
targetElement.setAttribute("doctor-whacks", elementWhacks + 3);
targetElement.style.transform = `rotate(${
elementWhacks + 3
}deg)`;
}
}
}
return;
}
const elementBorderColored = parseInt(
targetElement.getAttribute("doctor-bordercolor-coloring-count")
);
const elementBackgroundColored = parseInt(
targetElement.getAttribute("doctor-backgroundcolor-coloring-count")
);
if (elementBorderColored || elementBackgroundColored) {
setSprite("coloring");
if (
activityHeartbeatCount > 0 &&
activityHeartbeatCount % HEARTBEATS_TO_COLOR === 0
) {
targetElement.setAttribute("doctor-bordercolor-coloring-count", 0);
targetElement.setAttribute(
"doctor-backgroundcolor-coloring-count",
0
);
targetElement.setAttribute("doctor-backgroundcolor-color", "");
targetElement.setAttribute("doctor-bordercolor-color", "");
targetElement.style.borderWidth = "";
targetElement.style.borderStyle = "";
targetElement.style.borderColor = "";
targetElement.style.backgroundColor = "";
targetElement.classList.remove("doctor-colored");
stop();
}
} else {
stop();
}
};
//
// USER INTERACTIONS
//
const addEventListeners = () => {
myElement.addEventListener("click", () => {
// TODO: Detect whether it was in the center or periphery of the element
// If periphery, make him scared for a second, then run for cover
activityHeartbeatCount = 0;
die();
window.setTimeout(() => {
PlagueDoctorFactory.createPlagueDoctor().init();
PlagueDoctorFactory.createPlagueDoctor().init();
}, (getRandomInt(5) + 5) * 1000);
});
};
// HIGH-LEVEL BEHAVIORS
let doSomething = () => {
if (dead) {
return;
}
if (cleaningUp) {
switch (currentActivity) {
case "idle": {
const newTargetElement = pickModifiedElement();
if (newTargetElement) {
runToElement(newTargetElement);
} else {
cleaningUp = false; // We are done cleaning up. All that's left is to clean up ourselves...
die(); // The controler should delete
}
break;
}
case "running": {
runToElement();
break;
}
case "cleaning": {
cleanUpElement();
break;
}
default:
stop();
}
return;
}
switch (currentActivity) {
case "idle": {
randomly(10, () => {
walkToElement(pickRandomElement());
});
break;
}
case "walking": {
walkToElement();
randomly(500, () => {
stop();
changeColor();
});
break;
}
case "running": {
runToElement();
break;
}
case "looking": {
randomly(20, () => {
stop();
});
break;
}
case "dying": {
die();
break;
}
case "whacking": {
whackElement();
if (activityHeartbeatCount > HEARTBEATS_TO_WHACK) {
randomly(20, () => {
stop();
lookAtElement();
});
}
break;
}
case "coloring": {
colorElement();
if (activityHeartbeatCount > HEARTBEATS_TO_COLOR) {
randomly(80, () => {
stop();
changeColor(); // We are bored of this color now
lookAtElement();
});
}
}
}
};
let init = (speed) => {
myElement = document.createElement("img");
setLocation(
getRandomInt(window.innerWidth),
getRandomInt(window.innerHeight)
);
myElement.classList.add(
"plague-doctor-element",
"plague-doctor-element-init"
);
window.setTimeout(() => {
myElement.classList.toggle("plague-doctor-element-init");
}, 400);
setSprite("idle");
document.body.appendChild(myElement);
addEventListeners();
heartbeat = window.setInterval(() => {
if (debugging) {
console.log("❤️ ", statusString());
}
activityHeartbeatCount++;
doSomething();
updateSpriteLocation();
}, HEATBEAT_INTERVAL / speed);
};
return {
init,
setCleaningUp,
setDebugging,
setSprite,
stop,
walkToElement,
};
};
var PlagueDoctorFactory = (function () {
let createPlagueDoctor = () => {
let newDoctor = new Function(
"return " + plagueDoctorTemplate.toString()
)()();
return newDoctor;
};
return {
createPlagueDoctor,
};
})();
var PlagueDoctors = (function () {
let plagueDoctors = [];
let enableDebugging = (value) => {
for (i = 0; i < plagueDoctors.length; i++) {
plagueDoctors[i].setDebugging(value);
}
};
cleanUp = () => {
for (i = 0; i < plagueDoctors.length; i++) {
plagueDoctors[i].setCleaningUp(true);
}
};
let init = (numDoctors = 1, speed = 1) => {
for (i = 0; i < numDoctors; i++) {
plagueDoctors.push(PlagueDoctorFactory.createPlagueDoctor());
plagueDoctors[i].init(speed);
}
document
.getElementsByClassName("doctors-clean-up-button")[0]
.addEventListener("click", () => {
cleanUp();
});
};
return {
init,
cleanUp,
enableDebugging,
};
})();
var plagueDoctorsControls = document.createElement("div");
plagueDoctorsControls.classList.add("plague-doctor-controls");
plagueDoctorsControls.innerHTML = `
<button class="doctors-clean-up-button">Clean Up</button>
<img class="doctors-4tv-logo" src="https://plague-doctors.netlify.app/images/4tv_logo.png" />`;
document.body.appendChild(plagueDoctorsControls);
PlagueDoctors.init(3, 1);
</script>
</body>