-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompositionNotebook.mjs
More file actions
139 lines (127 loc) · 3.45 KB
/
Copy pathcompositionNotebook.mjs
File metadata and controls
139 lines (127 loc) · 3.45 KB
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
import fixValues from "../lib/fixValues.mjs";
import hexToArray from "../lib/hexToArray.mjs";
const optionFields = {
width: {
label: "Width",
nodeName: "INPUT",
type: "number",
min: 1,
max: 2048,
step: 1,
value: 300
},
height: {
label: "Height",
nodeName: "INPUT",
type: "number",
min: 1,
max: 2048,
step: 1,
value: 300
},
color1: {
label: "Color 1",
nodeName: "INPUT",
type: "color",
value: "#000000"
},
density: {
label: "Density of Color 2",
nodeName: "INPUT",
type: "number",
step: "any",
min: 0,
max: 0.5,
value: "0.02"
},
color2: {
label: "Color 2",
nodeName: "INPUT",
type: "color",
value: "#ffffff"
},
growth: {
label: "Growth of Color 2",
nodeName: "INPUT",
type: "number",
step: "any",
min: 0,
max: 0.999,
value: "0.985"
}
},
hexToArrayOpaque = hexToArray(0xff),
sumWhites = (whites, pixel) => whites + pixel,
{
mapToColor
} = {
mapToColor(value){
return this[value];
}
};
export default {
"Composition Notebook": {
title: "Generates a pattern that looks like the cover of a composition notebook",
description: "Generates a pattern that looks like the cover of a <a href=\"https://google.com/search?q=composition+notebook&tbm=isch\">composition notebook</a>.",
options: optionFields,
generator(ctx, { ...options }){
options = fixValues(options, optionFields);
const color1Index = 0,
color2Index = 1,
{
width,
height,
color1,
color2,
density,
growth
} = options,
colors = [
hexToArrayOpaque(color1),
hexToArrayOpaque(color2)
],
isolatedUncolored = [],
pixelMap = new Array(width * height).fill(color1Index),
randomPoints = width * height * density;
Object.assign(ctx.canvas, Object.freeze({
width,
height
}));
for(let i = 0; i < randomPoints; i++){
const dot = {
x: Math.floor(Math.random() * (width - 1)),
y: Math.floor(Math.random() * (height - 1))
};
while(Math.random() < growth){
if(dot.x >= 0 && dot.x < width && dot.y >= 0 && dot.y < height){
pixelMap[width * dot.y + dot.x] = color2Index;
}
dot[(Math.random() < 0.5
? "x"
: "y")] += (Math.random() < 0.5
? -1
: 1);
}
}
for(let y = 1; y < height - 1; ++y){
for(let x = 1; x < width - 1; ++x){
const offset = y * width + x;
if(!pixelMap[offset]){
const coloredNeighbors = [
pixelMap[offset - width],
pixelMap[offset + 1],
pixelMap[offset + width],
pixelMap[offset - 1]
].reduce(sumWhites, 0);
if(coloredNeighbors === 4 || coloredNeighbors === 3 && Math.random() < 0.3){
isolatedUncolored.push(offset);
}
}
}
}
isolatedUncolored.forEach((offset) => pixelMap[offset] = color2Index);
ctx.putImageData(new ImageData(new Uint8ClampedArray(pixelMap.flatMap(mapToColor, colors)), width), 0, 0);
return options;
}
}
};