-
Notifications
You must be signed in to change notification settings - Fork 43
/
sketch.js
186 lines (154 loc) · 5.28 KB
/
sketch.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
178
179
180
181
182
183
184
185
186
let nameInput,
selectionInput,
canvas,
generators = [],
generator,
bounds,
formContainer = document.querySelectorAll(".form-contain")[0],
titleElement = document.getElementById("attrib-title"),
authorElement = document.getElementById("author-name"),
backgroundColor = "#77B5FE",
download = document.getElementById("download"),
canvasSvg,
theSeed;
// Resizes the canvas to match the CSS
function resize() {
let parStyle = window.getComputedStyle(canvas.elt.parentNode),
cWidth = parseInt(parStyle.width),
cHeight = parseInt(parStyle.height);
resizeCanvas(cWidth, cHeight, true);
}
// When the window resizes
function windowResized() {
resize();
redraw();
}
function keyReleased() {
if (keyCode == UP_ARROW || keyCode == LEFT_ARROW) {
selectionInput.previousItem();
} else if (keyCode == DOWN_ARROW || keyCode == RIGHT_ARROW) {
selectionInput.nextItem();
}
return false;
}
// On setup
function setup() {
// Default canvas size
canvas = createCanvas(window.innerWidth, window.innerHeight);
canvas.parent("sketch-contain");
// When clicked
canvas.elt.addEventListener("click", redraw);
noLoop();
resize();
// Create list of selectable items
// Begin selectable options object
let selectOptions = {
"random": {
value : "Random",
group : "_default", // Added underscore to sort before "community"
default : true /** Group names should be used solely
* to differentiate visual groups.
* the default property allows users to
* set a default, but keep it inline
* with whichever group they're using.
*/
}
};
// Finish populating selectOptions
generators.forEach((n, i) => {
selectOptions[n.name.toLowerCase()] = {
value: n.name,
group: "community"
};
});
// Generate selector
selectionInput = new MaterialSelect(selectOptions, "", true, redraw);
// Add to clouds form
select("#clouds-form-generator").elt.appendChild(selectionInput.nodesRef);
// Material Design input field
nameInput = new MaterialText("", "", "", true, "user_name", "Name", "");
// Add to clouds form
select("#clouds-form-options").elt.appendChild(nameInput.nodesRef);
// Listen for value changes to redraw()
nameInput.inputNode.addEventListener("input", redraw);
}
function updateBg(color) {
backgroundColor = color.toHEXString();
redraw();
}
function handleDrawing(){
push();
document.getElementsByTagName("body")[0].style.background = backgroundColor;
background(backgroundColor);
scale(.7);
translate(width * .2, height * .2);
// Establish our default cloud drawing paremeters.
rectMode(CORNER);
ellipseMode(CENTER);
angleMode(RADIANS);
strokeWeight(10);
stroke("#000");
fill("#FFF");
// Render the chosen cloud and
bounds = generator.fn();
if (bounds) {
// Reset styles for the text
fill(bounds.length > 4 ? bounds[4] : "#000");
strokeWeight(0);
textSize(16);
textAlign(CENTER, CENTER);
textSize(100);
// Output the name (Hopefully within the bounds)
let theName = nameInput.validInput ? nameInput.validInput : "Example Name";
text(theName, bounds[0], bounds[1], bounds[2], bounds[3]);
} else {
console.log(generator.name + " by " + generator.creator + ", did not return bounds.")
}
// Describe which design
titleElement.innerHTML = generator.name;
authorElement.innerHTML = generator.creator;
pop();
}
function genRandomSeed(min, max) {
return Math.random() * (max - min) + min;
}
function setSvgDownload(e){
let tmpContext = canvas.drawingContext;
let canStyleWidth = parseInt(canvas.elt.style.width),
canStyleHeight = parseInt(canvas.elt.style.height);
canvasSvg = new C2S(canStyleWidth, canStyleHeight);
canvas.drawingContext = canvasSvg;
randomSeed(theSeed);
handleDrawing();
let theSVG = canvasSvg.getSerializedSvg(true),
svgBlob = new Blob([theSVG], {type:"image/svg+xml;charset=utf-8"}),
svgUrl = URL.createObjectURL(svgBlob);
download.setAttribute("href", svgUrl);
canvas.drawingContext = tmpContext;
}
function draw() {
download.removeEventListener("mousedown", setSvgDownload);
theSeed = genRandomSeed(1, 100);
if (selectionInput.curOpt !== 'random') {
// Get generator chosen
generator = generators[selectionInput.PresortIndex - 1];
} else {
// Chose a random generator
generator = random(generators);
}
randomSeed(theSeed);
noiseSeed(theSeed);
handleDrawing();
download.setAttribute("download", generator.name.split(' ').join('_') + ".svg");
download.addEventListener("mousedown", setSvgDownload);
randomSeed();
noiseSeed();
}
// Register a new cloud generator.
function register(fn, name, creator) {
generators.push({
fn: fn,
name: name,
creator: creator
});
}