-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch.js
163 lines (141 loc) · 4.38 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
/********
Ported from the Processing Sketch by
Bárbara Almeida
https://www.openprocessing.org/sketch/296103/
with minor changes
A generative Kolam pattern
********/
var link;
var nlink;
var idx;
var pg;
var bgcolor;
var kolam;
var gui_kolam;
/**/
function setup() {
createCanvas(windowWidth, windowHeight);
console.log("I think the window dimensions are "+windowWidth+" x "+windowHeight);
// make Dat.GUI control with four options
kolam = new Kolam();
gui_kolam = new dat.GUI();
gui_kolam.add(kolam, 'tsize', 30, 60).name('Size').onChange(function() {
setupTiles();
});
gui_kolam.add(kolam, 'margin', 2, 200).name('Margin').onChange(function() {
setupTiles();
});
gui_kolam.add(kolam, 'tnumber').name('Tiles').min(3).max(20).step(1).onChange(function() {
setupTiles();
});
gui_kolam.add(kolam, 'rotation').name('Rotation').min(0).max(2*Math.PI).step(QUARTER_PI/4).onChange(function() {
setupTiles();
});
gui_kolam.add(kolam, 'refreshRate').name('Refresh Rate').min(10).max(200).step(10);
bgcolor = color(random(50), random(50), random(50));
setupTiles();
configTiles();
}
/**/
function draw() {
if (idx <= 1) drawTile();
push();
translate(width / 2, height / 2);
rotate(kolam.rotation);
imageMode(CENTER);
image(pg, 0, 0);
pop();
if (frameCount % kolam.refreshRate == 0) {
configTiles();
}
}
/**/
function Kolam() {
this.tsize = 45;
this.margin = 5;
this.tnumber = 5;
this.refreshRate = 100;
this.rotation = QUARTER_PI;
}
/**/
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
setupTiles();
}
/**/
function setupTiles() {
background(bgcolor);
rectMode(CORNERS);
textSize(32);
fill(255);
text('Kolam', 30, 60);
textSize(12);
text('"Kolam is a form of drawing that is drawn by using rice flour, chalk, chalk powder or rock powder, \
often using naturally or synthetically colored powders, in Sri Lanka, the Indian states of Tamil Nadu,\
Karnataka, Telangana, Andhra Pradesh, Kerala and some parts of Goa, Maharashtra as well as Indonesia, \
Malaysia, Thailand and a few other Asian countries. A Kolam is a geometrical line drawing composed of \
curved loops, drawn around a grid pattern of dots." \n\nTaken as-is from Wikipedia - Kolam', 30, 70, 400, 200);
text('No Rights Reserved; Ported from a Processing Sketch by Bárbara Almeida', 30, windowHeight - 30);
pg = createGraphics(
kolam.tsize * kolam.tnumber + 2 * kolam.margin,
kolam.tsize * kolam.tnumber + 2 * kolam.margin
);
link = [];
nlink = [];
// populate the array with 1s
for (var i = 0; i < (kolam.tnumber + 1); i++) {
var pushThis = [];
for (var j = 0; j < (kolam.tnumber + 1); j++) {
pushThis.push(1);
}
link.push(pushThis);
nlink.push(pushThis);
}
}
function configTiles() {
idx = 0;
var i, j;
// update links
for (i = 0; i < link.length; i++) {
for (j = 0; j < link[0].length; j++) {
link[i][j] = nlink[i][j]
}
}
// create new links
var limit = random(0.4, 0.7);
for (i = 0; i < nlink.length; i++) {
for (j = 0; j < nlink.length / 2; j++) {
// randomly link or unlink
let l = 0;
if (random(1) > limit) l = 1;
nlink[i][j] = l;
nlink[i][nlink.length - j - 1] = l;
nlink[j][i] = l;
nlink[nlink.length - j - 1][i] = l;
nlink[nlink.length - 1 - i][j] = l;
nlink[nlink.length - 1 - i][nlink.length - j - 1] = l;
nlink[j][nlink.length - 1 - i] = l;
nlink[nlink.length - 1 - j][nlink.length - 1 - i] = l;
}
}
}
function drawTile() {
pg.background(bgcolor);
pg.noFill();
pg.stroke(255);
pg.strokeWeight(5);
for (var i = 0; i < kolam.tnumber; i++) {
for (var j = 0; j < kolam.tnumber; j++) {
if ((i + j) % 2 == 0) {
var top_left = kolam.tsize / 2 * lerp(link[i][j], nlink[i][j], idx);
var top_right = kolam.tsize / 2 * lerp(link[i + 1][j], nlink[i + 1][j], idx);
var bottom_right = kolam.tsize / 2 * lerp(link[i + 1][j + 1], nlink[i + 1][j + 1], idx);
var bottom_left = kolam.tsize / 2 * lerp(link[i][j + 1], nlink[i][j + 1], idx);
pg.rect(i * kolam.tsize + kolam.margin, j * kolam.tsize + kolam.margin, kolam.tsize, kolam.tsize, top_left, top_right, bottom_right, bottom_left);
pg.point(i * kolam.tsize + kolam.tsize / 2 + kolam.margin, j * kolam.tsize + kolam.tsize / 2 + kolam.margin);
}
}
}
idx += 0.02;
idx = constrain(idx, 0, 1);
}