-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_6.js
177 lines (146 loc) · 3.73 KB
/
day_6.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
// (c) 2023 Joseph HENRY
// Credit: Vahram Muratyan
// This code is licensed under MIT license (see LICENSE for details)
function computeGradient(fac, middle, size) {
const half = size / 2;
if (fac <= middle - half) return 0;
else if (fac >= middle + half) return 1;
return (fac - (middle - half)) / size;
}
function easeInOutSine(x) {
return -(Math.cos(Math.PI * x) - 1) / 2;
}
function windowMask(w, h) {
const mask = createGraphics(width, height);
mask.fill(255);
mask.stroke(255);
mask.strokeWeight(10);
mask.rectMode(CENTER);
mask.rect(width / 2, height / 2, w, h, 40);
return mask;
}
let blue;
let pink;
let red;
let rh, rw;
let rectMask;
let drawing;
let animValue = 0;
function lerpValue(offset, speed, min, max) {
return min + (max - min) * easeInOutSine(animValue * speed + offset);
}
function setup() {
createCanvas(400, 400);
blue = color("#2e3e8c");
pink = color("#fbc9be");
red = color("#fd312b");
rh = width * 0.8;
rw = rh / 1.5;
}
function draw() {
drawing = createGraphics(width, height);
rectMask = windowMask(rw, rh);
// Lines
drawing.push();
drawing.translate((width - rw) / 2, 0);
const startLineY = (height - rh) / 2;
const gradientPos = lerpValue(QUARTER_PI, 0.5, 0.2, 0.5);
const gradientSize = lerpValue(0, 1, 0.15, 0.25);
for (let i = 0; i <= rh; i++) {
const y = startLineY + i;
const gradientFac = computeGradient(i / rh, gradientPos, gradientSize);
const lineCol = lerpColor(blue, pink, gradientFac);
drawing.stroke(lineCol);
drawing.line(0, y, rw, y);
}
drawing.pop();
// Fuji
const fujiPos = lerpValue(PI, 0.5, 0.7, 0.8);
const fujiHeightFac = 0.4;
const fujiTopWidthFac = 0.1;
const fujiY = height / 2 + (fujiPos - 0.5) * rh;
const fujiTopWidth = fujiTopWidthFac * rw;
const fujiHeight = fujiHeightFac * rw;
let fuji = createGraphics(width, height);
const anchorDistance = (width * 1.5) / 2;
const topDist = fujiTopWidth / 2;
const topCurvatureDist = 0.2 * fujiHeight;
const bottomCurvatureDist = 0.5 * rw;
fuji.fill(blue);
fuji.noStroke();
fuji.push();
fuji.translate(width / 2, fujiY);
fuji.beginShape();
fuji.vertex(-anchorDistance, 0);
fuji.bezierVertex(
-bottomCurvatureDist,
0,
-topDist,
-fujiHeight + topCurvatureDist,
-topDist,
-fujiHeight
);
fuji.bezierVertex(
-topDist,
-fujiHeight,
-topDist,
-fujiHeight,
topDist,
-fujiHeight
);
fuji.bezierVertex(
topDist,
-fujiHeight + topCurvatureDist,
bottomCurvatureDist,
0,
anchorDistance,
0
);
fuji.endShape();
fuji.pop();
// Bottom fuji blue
fuji.rect((width - rw) / 2, fujiY - 10, rw, rh);
// Top snow
let fujiSnow = createGraphics(width, height);
const fujiSnowRatio = lerpValue(PI, 0.5, 0.7, 0.9);
fujiSnow.fill(255);
fujiSnow.noStroke();
fujiSnow.rectMode(CENTER);
fujiSnow.rect(width / 2, fujiY - fujiHeight, rw, fujiSnowRatio * fujiHeight);
fujiSnow.drawingContext.globalCompositeOperation = "destination-in";
fujiSnow.image(fuji, 0, 0);
// Apply snow
drawing.image(fuji, 0, 0);
drawing.image(fujiSnow, 0, 0);
// Mask
rectMask.drawingContext.globalCompositeOperation = "source-in";
rectMask.image(drawing, 0, 0);
// Background
background(red);
image(rectMask, 0, 0);
// Border
noFill();
stroke(255);
strokeWeight(10);
rectMode(CENTER);
rect(width / 2, height / 2, rw, rh, 40);
// red circle
const angle = animValue;
fill(red);
noStroke();
circle(
width / 2 + rw / 4 + cos(angle) * 20,
height / 2 - rh / 3 + sin(angle) * 20,
15
);
// Clean
fuji.remove();
fujiSnow.remove();
drawing.remove();
rectMask.remove();
fuji = null;
fujiSnow = null;
drawing = null;
rectMask = null;
animValue += 0.05;
}