-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
154 lines (137 loc) · 4.42 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
// Visual parameters
var width = window.innerWidth;
var height = window.innerHeight;
const backgroundColor = 'rgb(255,255,255)';
const terrainColor = 'rgba(115, 195, 115, 0.2)';
const lineColor = 'rgba(62,104,139,1)';
const urlBase = "https://datazoomamazonia.com.br/tag/"
const tablePath = 'assets/categorias.csv';
var showNet = false;
// Global variables
var table;
var net;
var rivers = [];
var canvas;
var bg;
// Function to create terrain
function createTerrain() {
var noiseScale = 20 / width;
var cell = 10;
var cutoff = 0.4;
var border = 0.1;
// Create new graphics element
t = createGraphics(width,height)
t.stroke(terrainColor)
// loop the canvas
for (var x = 0; x < width; x+=cell) {
for (var y = 0; y < height; y+=cell) {
// get noise value for each
var noiseVal = noise(noiseScale * x, noiseScale * y);
// get position in %
var xPerc = x/width;
var yPerc = y/height;
// only show point if above cutoff
if(noiseVal > cutoff) {
// fade if close to the border
if(xPerc < border) noiseVal = noiseVal*xPerc/border;
if(yPerc < border) noiseVal = noiseVal*yPerc/border;
if(xPerc > (1-border)) noiseVal = noiseVal*(1-xPerc)/border;
if(yPerc > (1-border)) noiseVal = noiseVal*(1-yPerc)/border;
t.strokeWeight(noiseVal*cell);
t.point(x,y)
}
}
}
return t;
}
// Preload table and font
function preload() {
table = loadTable(tablePath, 'csv', 'header');
}
// Setup
function setup() {
// Canvas setup
width = window.innerWidth;
height = window.innerHeight;
canvas = createCanvas(width, height);
canvas.parent("canvas_div");
fill(255);
textAlign(CENTER, CENTER);
textSize(0.025 * min(width, height));
// Load network
net = new Network();
net.networkFromCSV(table);
net.fastForward(1000);
// Create first river
let rio = new River(0, 0.5 * height);
rivers.push(rio);
// Create background;
bg = createTerrain();
}
// Draw loop
function draw() {
// draw background as image
background(255);
image(bg,0,0);
// update/show network
net.update();
if (showNet) net.show();
// loop rivers
for (let rio of rivers) {
let targetNode = net.nodes[rio.targetIndex];
let target = targetNode.pos;
// update rivers that are not "complete"
if (!rio.complete) {
// update rivers that have not arrived to target/destination
if (!rio.arrived) {
// apply wander to target
let force = rio.wanderToTarget(target);
rio.applyForce(force);
rio.update();
}
// from rivers that arrived to target, create new rivers
else {
// loop all source-target combinations
for (let link of net.links) {
// if river arrived to the source of another link
if (rio.targetIndex == link.source) {
// create new river for this link
let newRiver = rio.replicate();
newRiver.targetIndex = link.target;
rivers.push(newRiver);
}
}
// mark this river as done
rio.complete = true;
}
} else {
net.showHover(targetNode)
// if there is no next river, draw dot
if(!net.links.find(l => l.source == rio.targetIndex)) {
rio.showDot(x=target.x, y=target.y);
}
}
rio.show();
}
}
// handle mouse click event
function mouseClicked() {
// on click open url for closest node
let node = net.getClosestNode(mouseX, mouseY);
// extract name of the node clicked
let tag = node.name;
// turn into lowercase
tag = tag.toLowerCase();
// turn spaces " " into hypens "-"
tag = tag.replace(/\s+/g,"-");
// remove accents
tag = tag.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
// paste into base URL
let url = urlBase + tag;
// when user clicks on the Data Zoom node, the corresponding link does not exist
if (tag == "data-zoom") {
// instead, link to Data Zoom page
url = "https://www.econ.puc-rio.br/datazoom/index.html"
}
window.open(url);
}