-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.js
More file actions
50 lines (44 loc) · 1.19 KB
/
canvas.js
File metadata and controls
50 lines (44 loc) · 1.19 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
const canvas = document.querySelector("#indicator");
const ctx = canvas.getContext("2d");
function canvasDrawMiddle() {
// draw vertical line in middle of canvas
ctx.strokeStyle = "#FFF";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(canvas.width / 2, 10);
ctx.lineTo(canvas.width / 2, canvas.height - 10);
ctx.stroke();
}
function resizeCanvas() {
const parent = canvas.parentElement;
canvas.width = parent.clientWidth;
canvas.height = parent.clientHeight;
canvasDrawMiddle();
}
function resetCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvasDrawMiddle();
}
function canvasDrawFreq(freq, target) {
resetCanvas();
const diff = target - freq;
const y = canvas.height / 2;
let x = canvas.width / 2 - (diff);
if (Math.abs(diff) > canvas.width / 2) {
x = 0;
}
let r = 20;
ctx.fillStyle = "#FFF";
if (Math.abs(diff) < 40) {
ctx.fillStyle = "#008800";
r = 30;
} else if (Math.abs(diff) < 100) {
ctx.fillStyle = "#F4C41A";
r = 25;
}
ctx.strokeStyle = ctx.fillStyle;
ctx.beginPath();
ctx.arc(x, y, 20, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
}