-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyalgoviz.js
More file actions
136 lines (116 loc) · 3.36 KB
/
pyalgoviz.js
File metadata and controls
136 lines (116 loc) · 3.36 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
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
let context = null;
let canvas = null;
let None = null;
function init() {
/* Initializes the drawing canvas */
let div = $(".drawing").empty();
let parent = div.parent();
canvas = $("<canvas>")
.attr("width", parseInt(parent.width()))
.attr("height", parseInt(parent.height()))
.appendTo(div);
context = canvas[0].getContext("2d");
}
function clear() {
/* Clears the drawing canvas */
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width(), canvas.height());
}
function n(x, y, label, value, scale=4, color='black') {
/* Draw a number */
text(x, y+10, label);
rect(x+20, y, value*scale, 10, color);
text(x+22+value*scale, y+10, value);
}
function b(x, y, w, h, items, highlight=-1, fill="black", scale=1) {
/* Draw a barchart */
rect(x, y, w, h, '#FDFDF0', "gray");
if (items) {
let d = Math.min(15, Math.floor(w/items.length));
let offset = (w - items.length*d)/2;
items.forEach((item, n) => {
let hitem = item*scale;
rect(offset+x+n*d, y+h-hitem, d-2, hitem, n===highlight ? 'red' : fill, "lightgray");
});
}
}
function t(x, y, txt, size=13, font='Arial', color='black') {
/* Draw a text */
context.fillStyle = color
context.font = `${size}px ${font}`
context.fillText(txt, x, y)
}
function e(msg) {
/* Draw an error message */
$(".log-viz").append(
$("<div>")
.text(msg)
.addClass("error")
);
}
function l(x1, y1, x2, y2, color='black', width=1) {
/* Draw a line */
context.strokeStyle = color
context.lineWidth = width
context.beginPath()
context.moveTo(x1, y1)
context.lineTo(x2, y2)
context.stroke()
context.lineWidth = 1
}
function r(x, y, w, h, fill='white', border='black') {
/* Draw a rectangle */
context.strokeStyle = border
context.strokeRect(x, y, w, h)
context.fillStyle = fill
context.fillRect(x, y, w, h)
}
function c(x, y, radius, fill='white', border='black') {
/* Draw a circle */
context.beginPath()
context.arc(x, y, radius, 0, 2 * Math.PI)
context.fillStyle = fill
context.fill()
context.strokeStyle = border
context.stroke()
}
function a(x, y, radius, startAngle, endAngle, border='black', width=1) {
context.beginPath()
context.arc(x, y, radius, startAngle, endAngle)
context.strokeStyle = border
context.lineWidth = width
context.stroke()
}
function p(string) {
$(".log-viz").append(
$("<div>")
.text(string)
.addClass("log-line")
);
}
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
function s(frequency, duration) {
const oscillator = audioContext.createOscillator()
const gainNode = audioContext.createGain()
oscillator.connect(gainNode)
gainNode.connect(audioContext.destination)
oscillator.frequency.value = frequency
oscillator.type = 'sine'
gainNode.gain.setValueAtTime(0.005, audioContext.currentTime)
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + duration/1000)
oscillator.start()
oscillator.stop(audioContext.currentTime + duration/1000)
}
barchart = b;
text = t;
circle = c;
arc = a;
beep = s;
error = e;
rect = r;
line = l;
number = n;
function render(operations) {
$(".log-viz").empty();
eval(operations)
}