Skip to content

Commit 7fab73a

Browse files
committed
fixed hygen
1 parent 43bd739 commit 7fab73a

8 files changed

+675
-47
lines changed

README.md

+45-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## What Is This Project?
22

3+
**FIRST AND FOREMOST**, this project is largely based on Matt DesLaurier's [`canvas-sketch`](https://github.com/mattdesl/canvas-sketch).
4+
35
This project is an attempt to be an accessible, low barrier to entry sketchbook for generative art. The styling is minimal, and the rest of the opinionated pieces _should_ be largely abstracted away.
46

57
## Getting Started
@@ -56,22 +58,24 @@ import SketchWrapper from 'components/SketchWrapper'
5658
import { NextPage } from 'next'
5759
import { ColorValue, Draw } from 'types/CustomP5'
5860

59-
const width: number = 2048
60-
const height: number = 2048
61-
const dimensions: number[] = [width, height]
62-
const padding: number[] = [40]
63-
const background: ColorValue = [255, 253, 252]
64-
65-
const draw: Draw = p5 => {}
66-
67-
const MyNewSketch: NextPage = () => (
68-
<SketchWrapper
69-
draw={draw}
70-
dimensions={dimensions}
71-
padding={padding}
72-
background={background}
73-
/>
74-
)
61+
const MyNewSketch: NextPage = () => {
62+
const width: number = 2048
63+
const height: number = 2048
64+
const dimensions: number[] = [width, height]
65+
const padding: number[] = [40]
66+
const background: ColorValue = [255, 253, 252]
67+
68+
const draw: Draw = p5 => {}
69+
70+
return (
71+
<SketchWrapper
72+
draw={draw}
73+
dimensions={dimensions}
74+
padding={padding}
75+
background={background}
76+
/>
77+
)
78+
}
7579

7680
export default MyNewSketch
7781
```
@@ -80,7 +84,7 @@ You can navigate to this sketch by either finding it in the list of sketches on
8084

8185
## How Do I Work With p5 in This Environment?
8286

83-
You can treat any of your sketch pages as if you were writing a p5 sketch in [instance mode](https://www.youtube.com/watch?v=Su792jEauZg).
87+
You can treat any of your sketch pages as if you were writing a p5 sketch in [instance mode](https://www.youtube.com/watch?v=Su792jEauZg).
8488

8589
### Setup
8690

@@ -91,29 +95,32 @@ import SketchWrapper from 'components/SketchWrapper'
9195
import { NextPage } from 'next'
9296
import { ColorValue, Draw, Setup } from 'types/CustomP5'
9397

94-
const width: number = 2048
95-
const height: number = 2048
96-
const dimensions: number[] = [width, height]
97-
const padding: number[] = [40]
98-
const background: ColorValue = [255, 253, 252]
99-
100-
const setup: Setup = p5 => {
101-
p5.colorMode(p5.HSB)
98+
const MyNewSketch: NextPage = () => {
99+
const width: number = 2048
100+
const height: number = 2048
101+
const dimensions: number[] = [width, height]
102+
const padding: number[] = [40]
103+
const background: ColorValue = [255, 253, 252]
104+
105+
const setup: Setup = p5 => {
106+
p5.colorMode(p5.HSB)
107+
}
108+
109+
const draw: Draw = p5 => {}
110+
111+
return (
112+
<SketchWrapper
113+
setup={setup}
114+
draw={draw}
115+
dimensions={dimensions}
116+
padding={padding}
117+
background={background}
118+
/>
119+
)
102120
}
103121

104-
const draw: Draw = p5 => {}
105-
106-
const MyNewSketch: NextPage = () => (
107-
<SketchWrapper
108-
setup={setup}
109-
draw={draw}
110-
dimensions={dimensions}
111-
padding={padding}
112-
background={background}
113-
/>
114-
)
115-
116122
export default MyNewSketch
123+
117124
```
118125

119126
We define a `setup` arrow function and perform whatever logic we'd like. We then need to pass that function to the `setup` prop in the `<SketchWrapper />` component. There is _a lot_ going on under the hood of the `<SketchWrapper />` that offers you some strong defaults. Maintaining the ratio of your sketch as you resize the window, being able to save sketches with `cmd + s` or `ctrl + s` depening on your OS, etc. **This functionality won't be overwritten by passing in new props**. You'd need to specifically override what these utility functions are doing. To get a feel for what's going on there, you can take a look in `./src/util/defaults`.
@@ -143,7 +150,7 @@ const setup: Setup = p5 => {
143150
const draw: Draw = p5 => {
144151
p5.noLoop()
145152
// Set stroke color to white
146-
p5.stroke(0, 0, 255)
153+
p5.stroke(0, 255, 255)
147154
p5.strokeWeight(6)
148155

149156
// create an array with a length of TWO_PI.

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"new:sketch:fullscreen": "npx hygen fullscreen new --name",
10-
"new:sketch:square": "npx hygen square new --name",
11-
"new:sketch:a4": "npx hygen a4 new --name",
9+
"new:sketch:fullscreen": "hygen fullscreen new --name",
10+
"new:sketch:square": "hygen square new --name",
11+
"new:sketch:a4": "hygen a4 new --name",
1212
"lint": "next lint",
1313
"lint:fix": "next lint --fix"
1414
},
@@ -36,6 +36,7 @@
3636
"eslint": "8.23.0",
3737
"eslint-config-next": "12.2.5",
3838
"eslint-plugin-simple-import-sort": "^8.0.0",
39+
"hygen": "6.2.8",
3940
"typescript": "4.8.2"
4041
}
4142
}

src/pages/sketches/my-new-sketch.tsx

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import SketchWrapper from 'components/SketchWrapper'
2+
import { NextPage } from 'next'
3+
import { ColorValue, Draw, Setup } from 'types/CustomP5'
4+
5+
const MyNewSketch: NextPage = () => {
6+
const width: number = 2048
7+
const height: number = 2048
8+
const dimensions: number[] = [width, height]
9+
const padding: number[] = [40]
10+
const background: ColorValue = [255, 253, 252]
11+
12+
const setup: Setup = p5 => {
13+
p5.colorMode(p5.HSB)
14+
}
15+
16+
const draw: Draw = p5 => {
17+
p5.noLoop()
18+
// Set stroke color to white
19+
p5.stroke(0, 255, 255)
20+
p5.strokeWeight(6)
21+
22+
// create an array with a length of TWO_PI.
23+
const circleArray = Array.from({ length: p5.TWO_PI * 10.1 })
24+
25+
// For each point in the array, calculate the cartesian coordinates to draw a point.
26+
circleArray.forEach((_, i) => {
27+
const a = i * 0.1
28+
const r = p5.width / 3
29+
const x = r * Math.cos(a) + p5.width / 2
30+
const y = r * Math.sin(a) + p5.height / 2
31+
32+
p5.point(x, y)
33+
})
34+
}
35+
36+
return (
37+
<SketchWrapper
38+
setup={setup}
39+
draw={draw}
40+
dimensions={dimensions}
41+
padding={padding}
42+
background={background}
43+
/>
44+
)
45+
}
46+
47+
export default MyNewSketch

src/util/canvasSizes.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type PaperSize = 'A0' | 'A1' | 'A2' | 'A3' | 'A4'
2+
3+
export interface PaperSizes {
4+
[key: string]: number[]
5+
}
6+
7+
// Sizes are for 96 DPI
8+
const paperSizes: PaperSizes = {
9+
A0: [3179, 4494],
10+
11+
A1: [2245, 3179],
12+
13+
A2: [1587, 2245],
14+
15+
A3: [1123, 1587],
16+
17+
A4: [794, 1123],
18+
}
19+
20+
export const getDimensions = (paperSize: PaperSize) => paperSizes[paperSize]

src/util/createGrain.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Graphics } from 'p5'
2+
import type { P5 } from 'types/CustomP5'
3+
4+
const createGrain = (p5: P5, opacity: number = 10): Graphics => {
5+
const grain = p5.createGraphics(p5.width, p5.height)
6+
grain.loadPixels()
7+
Array.from({ length: p5.width }, (_, i) => {
8+
Array.from({ length: p5.height }, (_, j) => {
9+
grain.set(i, j, p5.color(p5.random(255), opacity))
10+
})
11+
})
12+
grain.updatePixels()
13+
14+
return grain
15+
}
16+
17+
export default createGrain

src/util/createGrid.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const createGrid = (rows: number, cols: number = rows) => {
2+
const points: number[][] = []
3+
4+
Array.from({ length: rows }, (_, u) =>
5+
Array.from({ length: cols }, (_, v) => {
6+
const x = u / (rows - 1)
7+
const y = v / (cols - 1)
8+
points.push([x, y])
9+
})
10+
)
11+
12+
return points
13+
}
14+
15+
export default createGrid

src/util/createOverlay.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Color, Graphics } from 'p5'
2+
import type { ColorValue, P5 } from 'types/CustomP5'
3+
const createOverlay = (
4+
p5: P5,
5+
background: ColorValue = [255, 253, 252],
6+
margin: number = p5.width * 0.1
7+
): Graphics => {
8+
let overlay = p5.createGraphics(p5.width, p5.height)
9+
overlay.background(background as unknown as Color)
10+
overlay.erase()
11+
overlay.rect(margin, margin, p5.width - margin * 2, p5.height - margin * 2)
12+
overlay.noErase()
13+
14+
return overlay
15+
}
16+
17+
export default createOverlay

0 commit comments

Comments
 (0)