Skip to content

Commit 5c2d5b8

Browse files
authored
Merge pull request #27 from ThomasAbbink/e
E
2 parents 5219764 + 7b2beda commit 5c2d5b8

File tree

4 files changed

+127
-55
lines changed

4 files changed

+127
-55
lines changed

public/assets/g-dark.png

2.02 MB
Loading

public/assets/g2.png

2.04 MB
Loading

src/sketches/generative/e/sketch.ts

Lines changed: 124 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
import { getCanvasSize } from '../../../utility/canvas'
22

33
import p5 from 'p5'
4+
import { generateOscillatingNumber } from '../../../utility/numbers'
45

6+
let showimage = false
57
const backgroundColor = 255
68
const e = (p5: p5) => {
79
let cardObject: ReturnType<typeof card> | undefined
810
let sunrays: ReturnType<typeof sunRay>[] = []
911
const waves: ReturnType<typeof wave>[] = []
1012
const flames: ReturnType<typeof flame>[] = []
1113

14+
let image: p5.Image | undefined
15+
let cardWidth = 0
16+
let cardHeight = 0
17+
let cardX = 0
18+
let cardY = 0
19+
1220
p5.preload = () => {
13-
p5.loadImage(`/assets/e2.png`, (im) => {
21+
p5.loadImage(`/assets/g2.png`, (im) => {
1422
im.loadPixels()
23+
im.resize(cardWidth, cardHeight)
24+
image = im
1525
})
1626
}
1727
p5.setup = () => {
1828
const { width, height } = getCanvasSize()
1929
p5.createCanvas(width, height)
2030
p5.background(backgroundColor)
21-
cardObject = card(p5)
31+
cardObject = card(p5, {
32+
image: image!,
33+
cardWidth,
34+
cardHeight,
35+
cardX,
36+
cardY,
37+
})
2238

23-
const rayCount = 100
39+
const rayCount = 500
2440
for (let i = 0; i < rayCount; i++) {
2541
sunrays.push(
2642
sunRay(p5, {
@@ -41,32 +57,99 @@ const e = (p5: p5) => {
4157
}),
4258
)
4359
}
60+
resize()
61+
}
62+
p5.mousePressed = () => {
63+
showimage = !showimage
64+
if (!showimage) {
65+
p5.background(255, 255, 255, 255)
66+
}
67+
}
68+
69+
const getPixelData = (pos: p5.Vector) => {
70+
if (
71+
!image ||
72+
pos.x < cardX ||
73+
pos.x > cardX + cardWidth ||
74+
pos.y < cardY ||
75+
pos.y > cardY + cardHeight
76+
) {
77+
return null
78+
}
79+
return image.get(pos.x - cardX, pos.y - cardY)
80+
}
81+
82+
const resize = () => {
83+
const cardAspectRatio = 210 / 148
84+
const canvasAspectRatio = p5.width / p5.height
85+
if (canvasAspectRatio < cardAspectRatio) {
86+
cardWidth = p5.width - p5.width * 0.3
87+
cardHeight = cardWidth / cardAspectRatio
88+
}
89+
if (canvasAspectRatio > cardAspectRatio) {
90+
cardHeight = p5.height - p5.height * 0.3
91+
cardWidth = cardHeight * cardAspectRatio
92+
}
93+
cardX = p5.width / 2 - cardWidth / 2
94+
cardY = p5.height / 2 - cardHeight / 2
95+
96+
if (image) {
97+
image.resize(cardWidth, cardHeight)
98+
}
4499
}
45100

46101
p5.windowResized = () => {
47102
const { width, height } = getCanvasSize()
48103
p5.resizeCanvas(width, height)
49-
cardObject?.resize()
104+
resize()
50105
p5.background(backgroundColor)
51106
}
107+
const opacity = generateOscillatingNumber({
108+
min: 0.5,
109+
max: 3,
110+
initialValue: 2,
111+
increment: 0.01,
112+
minSpeed: 0.01,
113+
restFrames: 1000,
114+
})
52115

53116
p5.draw = () => {
54-
cardObject?.draw()
55117
sunrays.forEach((sunray) => {
56118
// @ts-ignore
57-
sunray.draw(cardObject?.getPixelData)
119+
sunray.draw(getPixelData)
58120
sunray.update()
59121
})
60122
waves.forEach((wave) => {
61123
// @ts-ignore
62-
wave.draw(cardObject?.getPixelData)
124+
wave.draw(getPixelData)
63125
wave.update()
64126
})
65127
flames.forEach((flame) => {
66128
// @ts-ignore
67-
flame.draw(cardObject?.getPixelData)
129+
flame.draw(getPixelData)
68130
flame.update()
69131
})
132+
if (image) {
133+
p5.blend(
134+
image,
135+
image.width / 2 + 2,
136+
0,
137+
image.width / 2,
138+
image.height,
139+
cardX + cardWidth / 2,
140+
cardY,
141+
cardWidth / 2,
142+
cardHeight,
143+
p5.BLEND,
144+
)
145+
}
146+
p5.background(255, 255, 255, opacity())
147+
if (showimage) {
148+
p5.image(image!, cardX, cardY, cardWidth, cardHeight)
149+
}
150+
// if (p5.frameCount % 1000 === 0) {
151+
// p5.background(255, 255, 255, 255)
152+
// }
70153
}
71154
}
72155

@@ -100,7 +183,7 @@ const sunRay = (p5: p5, { start }: { start: p5.Vector }) => {
100183
return
101184
}
102185

103-
let brightnessBasedSize = p5.map(pixelData[1], 0, 255, size, 0, true) / 2
186+
let brightnessBasedSize = p5.map(pixelData[0], 0, 255, size, 0, true) / 3
104187

105188
p5.push()
106189
p5.noStroke()
@@ -118,14 +201,19 @@ const sunRay = (p5: p5, { start }: { start: p5.Vector }) => {
118201

119202
const wave = (p5: p5, { start }: { start: p5.Vector }) => {
120203
let position = start.copy()
121-
const color = [p5.random(0, 10), p5.random(150, 220), 255]
204+
const color = [p5.random(40, 50), p5.random(150, 170), 100]
205+
// const color = [0, 0, 0]
122206
const speed = p5.random(2, 4)
123207
const size = p5.random(speed + 4, 10)
124208

125209
const update = () => {
126210
// use perlin noise to move the ray
127211
const noise = p5.noise(p5.frameCount)
128-
position.add(p5.createVector(Math.sin(noise), 0).setMag(speed))
212+
position.add(
213+
p5
214+
.createVector(Math.sin(noise), p5.random(-noise / 4, noise / 4))
215+
.setMag(speed),
216+
)
129217
if (position.y > p5.height || position.x > p5.width) {
130218
position = start.copy()
131219
}
@@ -145,7 +233,7 @@ const wave = (p5: p5, { start }: { start: p5.Vector }) => {
145233
return
146234
}
147235

148-
let brightnessBasedSize = p5.map(pixelData[2], 0, 255, size, 0, true) / 2
236+
let brightnessBasedSize = p5.map(pixelData[1], 0, 255, size, 0, true) / 2
149237

150238
p5.push()
151239
p5.noStroke()
@@ -191,7 +279,7 @@ const flame = (p5: p5, { start }: { start: p5.Vector }) => {
191279
return
192280
}
193281

194-
let brightnessBasedSize = p5.map(pixelData[1], 0, 100, size, 0, true) / 2
282+
let brightnessBasedSize = p5.map(pixelData[2], 0, 100, size, 0, true) / 2
195283

196284
p5.push()
197285
p5.noStroke()
@@ -206,63 +294,46 @@ const flame = (p5: p5, { start }: { start: p5.Vector }) => {
206294
}
207295
}
208296

209-
const card = (p5: p5) => {
210-
let width = 0
211-
let height = 0
212-
let x = 0
213-
let y = 0
214-
let image: p5.Image | undefined
215-
216-
const resize = () => {
217-
const cardAspectRatio = 105 / 148
218-
const canvasAspectRatio = p5.width / p5.height
219-
if (canvasAspectRatio < cardAspectRatio) {
220-
width = p5.width - p5.width * 0.3
221-
height = width / cardAspectRatio
222-
}
223-
if (canvasAspectRatio > cardAspectRatio) {
224-
height = p5.height - p5.height * 0.3
225-
width = height * cardAspectRatio
226-
}
227-
x = p5.width / 2 - width / 2
228-
y = p5.height / 2 - height / 2
229-
}
230-
231-
const loadImage = () => {
232-
p5.loadImage(`/assets/e2.png`, (im) => {
233-
image = im
234-
image.resize(width, height)
235-
im.loadPixels()
236-
})
237-
}
238-
297+
const card = (
298+
p5: p5,
299+
{
300+
image,
301+
cardWidth,
302+
cardHeight,
303+
cardX,
304+
cardY,
305+
}: {
306+
image: p5.Image
307+
cardWidth: number
308+
cardHeight: number
309+
cardX: number
310+
cardY: number
311+
},
312+
) => {
239313
const getPixelData = (pos: p5.Vector) => {
240314
if (
241315
!image ||
242-
pos.x < x ||
243-
pos.x > x + width ||
244-
pos.y < y ||
245-
pos.y > y + height
316+
pos.x < cardX ||
317+
pos.x > cardX + cardWidth ||
318+
pos.y < cardY ||
319+
pos.y > cardY + cardHeight
246320
) {
247321
return null
248322
}
249-
return image.get(pos.x - x, pos.y - y)
323+
return image.get(pos.x - cardX, pos.y - cardY)
250324
}
251325

252326
const draw = () => {
253327
p5.push()
254328
p5.noStroke()
255329
p5.noFill()
256-
p5.fill(255, 255, 255, 0.1)
257-
p5.rect(x, y, width, height)
330+
p5.fill(255, 255, 255, 100)
331+
p5.rect(cardX, cardY, cardWidth, cardHeight)
258332
p5.pop()
259333
}
260-
resize()
261-
loadImage()
262334

263335
return {
264336
draw,
265-
resize,
266337
getPixelData,
267338
}
268339
}

src/utility/numbers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ export const generateNumber = ({
1919

2020
type Props = {
2121
initialValue: number
22-
increment: number
22+
increment?: number
2323
min: number
2424
max: number
25-
easing: number
25+
easing?: number
2626
minSpeed: number
2727
restFrames: number
2828
}
29+
2930
export const generateOscillatingNumber = ({
3031
initialValue,
3132
increment = 0,

0 commit comments

Comments
 (0)