-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
91 lines (75 loc) · 2.17 KB
/
canvas.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
var canvas = document.getElementById("screen");
var startBtn = document.getElementById("start_btn");
var generateBtn = document.getElementById("generate_btn");
var c = canvas.getContext("2d");
canvas.width = window.innerWidth - 50;
canvas.height = window.innerHeight - 50;
//-----------------------------------------------------------------------------------------------------
var barWidth = 0;
var Bar = function (x, y, value, weight, color) {
this.x = x;
this.y = y;
this.value = value;
this.weight = weight;
this.color = color;
this.draw = function () {
console.log(`${this.x} ${this.y} ${this.weight} ${this.color} ${barWidth}`);
c.beginPath();
c.fillRect(this.x, this.y, barWidth, this.weight);
c.fillStyle = this.color;
c.fill();
};
};
var values = [];
var barsList = [];
var adjPercent;
var getRandom = (min, max) => min + Math.random() * (max - min);
var calculateBarWidth = () => (barWidth = canvas.width / values.length);
var getWeight = (value) => {
return value - value * (adjPercent / 100);
};
function calculateAdjPercent() {
var max = values[0];
for (let i = 1; i < values.length; i++) {
if (values[i] > max) {
max = values[i];
}
}
if (max > canvas.height) adjPercent = ((max - canvas.height) / max) * 100;
else adjPercent = 0;
console.log("Max: " + max);
console.log("Adj: " + adjPercent);
}
function renderBars() {
for (let i = 0; i < values.length; i++) {
var obj = new Bar(
barWidth * i,
canvas.height,
values[i],
-getWeight(values[i]),
`rgba(${Math.round(getRandom(50, 255))}, ${Math.round(
getRandom(50, 255)
)}, ${Math.round(getRandom(50, 255))}, 1)`
);
barsList.push(obj);
obj.draw();
}
console.log(barsList);
}
function generateRandomValues() {
for (let i = 0; i < 100; i++) {
values.push(Math.round(getRandom(1, 1000)));
}
calculateBarWidth();
calculateAdjPercent();
console.log(barWidth);
renderBars();
console.log(values);
}
generateBtn.addEventListener("click", function () {
generateRandomValues();
});
startBtn.addEventListener("click", function () {
// c.fillStyle = "#FFFFFF";
// c.fillRect(0, 615, 10, -75);
});