-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
97 lines (69 loc) · 2.94 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
/*
#makevember day 6
I wanted to make another "pattern explorer" with an option to
save an SVG file. This time I went with some radial symmetry
and altought I could draw other shapes besides an ellipse I decided
to keep things simple. Could be used to explore animation ideas and
values too.
Using https://github.com/bit101/quicksettings for the GUI
Using https://github.com/zenozeng/p5.js-svg for SVG export
Federico
*/
var canvas;
var panel; //GUI object
function setup() {
//create canvas using SVG library
canvas = createCanvas(480, 480, SVG);
//create quicksettings panel
panel = QuickSettings.create(120, 90, "Settings")
.addRange ("distance between centers", 1, 100, 30, 1 ) // slider
.addRange ("center offset", -3, 3, 0.5, 0.01) // slider
.addRange ("number of radii", 1, 12, 2, 1 ) // slider
.addRange ("stroke weight", 0.2, 10, 0.2, 0.1 ) // slider
.addButton("reset values", defaultValues) // reset button
.addButton("download svg", downloadSVG) // download button
.addButton("download png", downloadPNG) // download button
.addHTML ("Navigation","<a href=https://wolfcatworkshop.github.io/code-sketchbook/makevember/day-5/><< previous</a><a href=https://wolfcatworkshop.github.io/code-sketchbook/>   home   </a><a href=https://wolfcatworkshop.github.io/code-sketchbook/makevember/day-7/>next >></a>")
;
}
function draw() {
background(240);
noFill();
// set stroke weight
var strokeValue = panel.getValue("stroke weight")
strokeWeight(strokeValue);
// set number of "spokes" like the radius of a wheel by dividing TWO_PI
var spokes = TWO_PI / (panel.getValue('number of radii'));
// draw a set of spokes with a given angle
for (var a = 0; a < TWO_PI; a += spokes){
ellipseSpoke(a);
}
}
//draw a "spoke" of ellipses with each ellipse center moving away from the center of the canvas
//the function takes an angle of rotation
function ellipseSpoke(rotation){
//get values
var step = panel.getValue('distance between centers')
var offset = panel.getValue('center offset');
push();
translate(width/2, height/2);
rotate(rotation);
for (var r = step; r < height * 2; r+= step) {
ellipse(0, r * offset, r, r);
}
pop();
}
//resets the values of the drawing, used with reset button
function defaultValues(){
panel.setValue("distance between centers", 30)
.setValue("center offset", 0.5)
.setValue("number of radii", 2)
.setValue("stroke weight", 0.2);
}
//save svg when the button is pressed, requires svg library, used with save button
function downloadSVG(){
save('radialPattern.svg');
}
function downloadPNG(){
saveSVG('radialPattern', 'png');
}