-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
149 lines (135 loc) · 3.95 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
var time;
var timeStep=10;
var upL1=9;
var upL2=20;
var coll=[];
var randomMotion=false; //can set random spread
function setup(){
createCanvas(windowWidth-50,windowHeight-50);
// set the frame rate
frameRate(60);
time=10;
//initLine();
//noLoop();
//create new collection of lines objects
collection=new collectionLines([windowWidth-windowWidth/3,windowHeight/4],30);
collection2=new collectionLines([windowWidth-windowWidth/2,windowHeight-windowHeight/5],30);
collection3=new collectionLines([windowWidth/7,windowHeight/3],50);
collection4=new collectionLines([windowWidth/2,windowHeight/6],50);
//push them into the list to keeep track
coll.push(collection);
coll.push(collection2);
coll.push(collection3);
coll.push(collection4);
}
function draw(){
//set the mode of agle to degrees (more intuitive)
angleMode(DEGREES);
//number to keep track of the length of the array
var l= coll.length;
//loop though all the collections of lines
for (var i=0; i<l; i++){
coll[i].updateLines();
coll[i].drawLines();
}
}
// add a new collection of lines when clicked
function mouseClicked(){
coll.push(new collectionLines([mouseX, mouseY],30));
}
// add a new collection of lines when touched (seems that this is not working well)
function touchStarted(){
coll.push(new collectionLines([touchX, touchY],30));
}
// create a class collectionLines, made of randomLine objects
function collectionLines(xy, N){
this.d=random(.2,2); //size of the circles
this.lines=[]; //lines array
this.N=N; //number of lines (passed in)
// loop through and create lines
for (var i=0; i<N; i++){
this.lines.push(new randomLine(xy,this.d));
}
// add point to the line
this.updateLines=function(){
for (var i=0; i<this.N; i++){
this.lines[i].addPoint();
}
}
// draw the new line
this.drawLines=function(){
for (var i=0; i<this.N; i++){
this.lines[i].drawCurve();
}
}
}
//Class for the randomLine
function randomLine(xy, d){
this.x=[]; //empty x array
this.x.push(xy[0]);
this.y=[]; //empty y array
this.y.push(xy[1]);
this.theta=random(360); // initialize theta
var l=this.x.length;
this.up=0;
// initialize the first 4 values to make it easier to plot (need 4 for the curve() )
while (l<4){
var l=this.x.length;
this.x.push(d*sin(this.theta)+this.x[l-1]);
this.y.push(d*cos(this.theta)+this.y[l-1]);
var nn=noiseTheta(this.theta, this.up);
this.theta=nn[0];
if (nn[1]){
this.up=0;
}
else{
this.up+=1;
}
}
// add points
this.addPoint=function(){
var l=this.x.length;
this.x.push(d*sin(this.theta)+this.x[l-1]);
this.y.push(d*cos(this.theta)+this.y[l-1]);
var nn=noiseTheta(this.theta, this.up);
this.theta=nn[0];
if (nn[1]){
this.up=0;
}
else{
this.up+=1;
}
}
// draw the curve (only draw last 4 points, since we do not draw over)
this.drawCurve=function(){
stroke(255, 102, 0);
strokeWeight(1);
var l=this.x.length;
curve(this.x[l-4], this.y[l-4], this.x[l-3], this.y[l-3],this.x[l-2], this.y[l-2],this.x[l-1], this.y[l-1]);
}
}
function randomPosition(){ //random start
//we do not want to start at the very edges
return [floor(random(50,width-50)),floor(random(50, height-50))];
}
// how to decide whether we want to change direction in the small wobble in the lines
function noiseTheta(th, up){
time+=timeStep;
var change=false;
if (up<upL1){
sign=1;
}
else if (up<upL2){
sign=-1;
print(sign);
}
else{
change=true;
sign=1;
print(sign);
}
if (randomMotion){
return [sign*noise(time)*20+th, change];
}
return [sign*10+th, change];
}