-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch_barchart_02_animated.js
76 lines (58 loc) · 2.28 KB
/
sketch_barchart_02_animated.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
let data, maxValue, barX, barY, barHeight;
let margin = 400;
// Standard P5.js function: Will be called one time on load
function setup() {
// Create the canvas with provided width & height
createCanvas(1000, 1000);
// Set the background color of the canvas (default: RGB values)
background(10, 5, 10);
// The data
data = [
{key: 'bar 1', value: 10},
{key: 'bar 2', value: 40},
{key: 'bar 3', value: 20},
{key: 'bar 4', value: 50},
{key: 'bar 5', value: 60},
]
// Get the max value from the data array
// We will need this later for calculating the length of the bars
maxValue = max(data.map(d => d.value))
}
// Standard P5.js function: Will be called in a loop for
// every new frame (unless deactivated via noLoop())
function draw() {
// Calculate
let step = frameCount / 100;
// Reset the canvas with every new frame
// Remove if you don't want to reset but 'pack' each frame on top of the last
background(10, 5, 10);
// For each entry in data draw a rectange that represents
// the coreesponding value through its height
data.forEach((d, i) => {
barWidth = 50 + 40 * sin(step);
barMaxWidth = height-margin;
barBorderRadius = barWidth/2;
// Calculate the bar height: map the current value d.value to the height of the canvas
// (substract the defined margin, so the bars don't touch the edges of the canvas)
barHeight = map(d.value, 0, maxValue, 0, barMaxWidth);
// Distrbute bars horizontaly: map the current index i to the width of the canvas
// (substract the defined margin, so the bars don't touch the edges of the canvas)
barX = map(i, 0, data.length-1, margin, width-margin);
// Center all bars horizontally
rectMode(CENTER)
barY = height/2;
// Draw the bar at the calculated position with the calcualted height
push();
translate(barX, barY);
rotate(sin(step)*PI/2)
rect(0, 0, barWidth, barHeight, barBorderRadius);
pop();
});
}
// Custom utils function
function keyPressed() {
// Stop animation
if (key == 'p') (isLooping()) ? noLoop() : loop();
// Download canvas as png
if (key == 's' || key == 'S') saveCanvas(canvas, 'creative-data-viz-p5js-workshop', 'png');
}