-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.js
160 lines (125 loc) · 4.29 KB
/
index2.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
function _9(svgFaces)
{
svgFaces;
const elements = Array.from(document.querySelectorAll(".buddy"));
const wait = () =>
250 + Math.pow(elements.length, 0.75) * 1 * 1000 * Math.random();
elements.forEach(function (buddy) {
let timerId = setTimeout(function tick() {
buddy.classList.add("wink");
timerId = setTimeout(tick, wait());
}, wait());
buddy.addEventListener("animationend", () => {
buddy.classList.remove("wink");
});
});
return " ";
}
function _nCols(Inputs){return(
Inputs.range([1, 60], {
step: 1,
value: 13,
label: "number of columns"
})
)}
function _splitn(Inputs){return(
Inputs.range([0,1], {
step: .1,
value: .5,
label: "Split"
})
)}
function _svgFaces(_,colors,chroma,svg,width,nCols,splitn)
{
function looper(nx, ny) {
const result = [];
for (let i = 0; i < nx; i++) {
for (let j = 0; j < ny; j++) {
result.push([i, j]);
}
}
return result;
}
function oneFace(i, j) {
let faceColor = "#f8e1e2";
if (i/nCols >= splitn) {
faceColor = "#BFF4BE";
}
let browColor = chroma(faceColor).darken(1);
let mouthColor = chroma(faceColor).darken(2);
return svg`<svg x=${i * w} y=${
j * w
} class="buddy" viewBox="-50 0 100 100">
<ellipse cx="50" cy="30" rx="50" ry="50" class="face" fill="${faceColor}" />
<ellipse cx="30" cy="27.5" rx="10" ry="10" class="eye right-eye" fill="white" stroke="none" vector-effect="non-scaling-stroke" stroke-width=1 />
<ellipse cx="33" cy="27.5" rx="5" ry="5" class="eye right-eye" fill="black" stroke="none" />
<ellipse cx="70" cy="27.5" rx="10" ry="10" class="eye left-eye" fill="white" stroke="none" vector-effect="non-scaling-stroke" stroke-width=1 />
<ellipse cx="73" cy="27.5" rx="5" ry="5" class="eye left-eye" fill="black" stroke="none" />
<path d="M30 50 c0 20, 40 20, 40 0" class="mouth" fill="${mouthColor}" stroke="none" />
<path d="M17.5 10 c0 0, 12.5 -6, 25 0" class="eyebrow" fill="none" stroke="${browColor}" stroke-width=5 />
<path d="M57.5 10 c0 0, 12.5 -6, 25 0" class="eyebrow" fill="none" stroke="${browColor}" stroke-width=5 />
</svg>`;
}
const nRows = Math.floor(nCols / 2);
const result = svg`<svg id="smileysvg" class="smiley" viewBox="-50 0 50 100">
${looper(nCols, nRows).map(([x, y]) => oneFace(x, y))}
</svg>`;
return result;
}
function _13(htl){return(
htl.html`<style>
/* css animations adapted from https://dev.to/5t3ph/how-to-create-an-animated-svg-face-with-css-5djd */
svg {
overflow: visible;
}
.wink > .left-eye, .wink > .right-eye {
transform: scale(1);
transform-origin: 45% 45%;
animation: wink 250ms ease-in-out 1;
}
.blink > .left-eye, .blink > .right-eye {
transform: scale(1);
animation: wink 250ms ease-in-out 1;
}
.eye .white {
fill: white;
}
.eyebrow {
stroke-linecap: round;
/*display: none;*/
}
@keyframes wink {
50% {
transform: scale(1.2, 0.1);
}
0%, 100% {
transform: scale(1);
}
}
@media (prefers-reduced-motion: reduce) {
* {
-webkit-animation-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
-webkit-animation-iteration-count: 1 !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
</style>`
)}
function _chroma(require){return(
require("chroma-js@2.4.2/chroma.js")
)}
export default function define(runtime, observer) {
const main = runtime.module();
main.variable(observer()).define(["svgFaces"], _9);
main.variable(observer("viewof splitn")).define("viewof splitn", ["Inputs"], _splitn);
main.variable(observer("splitn")).define("splitn", ["Generators", "viewof splitn"], (G, _) => G.input(_));
main.variable(observer("viewof nCols")).define("viewof nCols", ["Inputs"], _nCols);
main.variable(observer("nCols")).define("nCols", ["Generators", "viewof nCols"], (G, _) => G.input(_));
main.variable(observer("svgFaces")).define("svgFaces", ["_","colors","chroma","svg","width","nCols","splitn"], _svgFaces);
main.variable(observer()).define(["htl"], _13);
main.variable(observer("chroma")).define("chroma", ["require"], _chroma);
return main;
}