-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsketch.js
More file actions
292 lines (243 loc) · 5.34 KB
/
Copy pathsketch.js
File metadata and controls
292 lines (243 loc) · 5.34 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var h = 360,
s = 0,
b = 100,
a = 100;
var h2 = 12,
s2 = 40,
b2 = 23,
a2 = 100;
var h3 = 0,
s3 = 0,
b3 = 15,
a3 = 100,
a3Wipe = 40;
// vars for line
var eX, eY, // position
eX2, eY2,
eW = 1; // width
// for bounces on wall
var bounces = 0,
bouncesMax,
bouncesTotal = 0; // current total amount of bounces
// for conversion from polar to cartesian coordinates.
// basically getting x and y from an angle and a radius.
// angle: 0 to 360 ... duh
var angle,
radius ; // sets the speed
var angleOff = 0.0;
var quadrant;
var wall; // which wall? t,r,b,l?
// for Dot
var dot = [];
// for saving position of corner of triangle
var tWallPosX = [],
tWallPosY = [],
triangle = [];
// for filling whole document
var w = window,
wX = w.innerWidth/2,
wY = w.innerHeight/2;
/*
* only executed once
*/
function setup(){
// change color mode to hsl
colorMode(HSB, 360, 100, 100, 100);
var bgrColor = color(h3, s3, b3);
var c = createCanvas(wX, wY);
c.parent("container");
// start line at...
eX = random(0,width);
eY = 0;
angle = radians(random(1,179));
radius = 20; //speed of line drawing
bouncesMax = 4;
// rectangle/background
background(bgrColor);
}
/*
* this will be executed all the time
*/
function draw(){
// angleOff += 0.01;
// var nOff = noise(angleOff)*10;
// jitter
eW = random(0.5,1.5);
angle += radians(random(-1,1));
// translate from angle: polar to cartesian coordianates
eX += cartesianX(radius,angle);
eY += cartesianY(radius,angle);
// DRAW LINE
stroke(h,s,b,a);
strokeWeight(eW);
line(eX,eY, eX2,eY2);
// quadrants:
// 1: 0° - 90°
// 2: 90° - 180°
// 3: 180° - 270°
// 4: 270° - 360°
if (angle >= radians(0) && angle < radians(90)) {
quadrant = 1;
}
else if (angle >= radians(90) && angle < radians(180)) {
quadrant = 2;
}
else if (angle >= radians(180) && angle < radians(270)) {
quadrant = 3;
}
else if (angle >= radians(270) && angle < radians(360)) {
quadrant = 4;
}
// wall sides
if (eY < 0) {
wall = 1;
}
else if (eX > width) {
wall = 2;
}
else if (eY > height) {
wall = 3;
}
else if (eX < 0) {
wall = 4;
}
// if bounce from specific wall from specific angle
// 0° - 90°
if (quadrant === 1 && wall === 2) { // right
angle += radians(90);
bounces++;
eX = width;
}
else if (quadrant === 1 && wall === 3) { // bottom
angle -= radians(90);
bounces++;
eY = height;
}
// 90° - 180°
else if (quadrant === 2 && wall === 3) { // bottom
angle += radians(90);
bounces++;
eY = height;
}
else if (quadrant === 2 && wall === 4) { // left
angle -= radians(90);
bounces++;
eX = 0;
}
// 180° - 270°
else if (quadrant === 3 && wall === 4) { // left
angle += radians(90);
bounces++;
eX = 0;
}
else if (quadrant === 3 && wall === 1) { // top
angle -= radians(90);
bounces++;
eY = 0;
}
// 270° - 360°
else if (quadrant === 4 && wall === 1) { // top
angle += radians(90);
bounces++;
eY = 0;
}
else if (quadrant === 4 && wall === 2) { // right
angle -= radians(90);
bounces++;
eX = width;
}
// stay in 360°
if (angle >= radians(360)) {
angle -= radians(360);
}
else if (angle < radians(0)) {
angle += radians(360);
}
if (frameCount === 1) {
dot[bouncesTotal] = new Dot();
dot[bouncesTotal].show();
}
// if specific amount of bouncing, reset position
if (bounces > bouncesMax) {
noStroke();
// eX = random(0,width);
// eY = 0;
angle = radians(random(0,180));
bounces = 0; // reset bounces counter
frameCount = 1;
}
// if at wall-bounce, add thing
if (eX === width || eY === height || eX === 0 || eY === 0) {
// save position at wall
tWallPosX[bouncesTotal] = eX;
tWallPosY[bouncesTotal] = eY;
// create new Dot at wall
dot[bouncesTotal] = new Dot();
dot[bouncesTotal].show();
triangle[bouncesTotal] = new Triangle();
// draw triangle, start at third bounce
if (bouncesTotal >= 2) {
triangle[bouncesTotal].show();
}
// add count
bouncesTotal++;
}
// fadeout effect
background(h3, s3, b3, 1);
// get last coordinate for drawing a line
eX2 = eX;
eY2 = eY;
console.log(angle);
}
/*
* custom functions
*/
// create cartesian coordinates
function cartesianX(radius,angle) {
var x = radius*cos(angle);
return x;
}
function cartesianY(radius,angle) {
var y = radius*sin(angle);
return y;
}
/*
* Classes
*/
// Dot constructor
var Dot = function() {
this.x = eX;
this.y = eY;
// this.width = eW;
// this.color = color(h,s,b,a);
};
// show method
Dot.prototype.show = function() {
noStroke();
// fill(this.color);
// ellipse(this.x,this.y, this.width*5,this.width*5);
};
// Triangle constructor
var Triangle = function() {
this.x1 = tWallPosX[bouncesTotal];
this.y1 = tWallPosY[bouncesTotal];
this.x2 = tWallPosX[bouncesTotal-2];
this.y2 = tWallPosY[bouncesTotal-2];
// this.colorFill = color(h2,s2,b2,10);
this.colorStroke = color(h,s,b,a);
};
Triangle.prototype.show = function() {
stroke(this.colorStroke);
// noFill();
// noStroke();
// fill(this.colorFill);
// triangle(
// this.x1,this.y1,
// this.x2,this.y2,
// this.x3,this.y3
// );
line(
this.x1,this.y1,
this.x2,this.y2
);
};