-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplication_strings.html
More file actions
181 lines (149 loc) · 5.94 KB
/
multiplication_strings.html
File metadata and controls
181 lines (149 loc) · 5.94 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
</head>
<body>
<canvas id='strings' width='650px' height='650px'></canvas>
<div style="float: right">
<div>
<label for='numDots'>Number of Dots</label>
<input type='number' id='numDots'>
<label for='numDotChange'>Change By</label>
<input type='number' id='numDotChange'>
</div>
<div>
<label for='multFactor'>Multiplication Factor</label>
<input type='number' id='multFactor'>
<label for='multFactorChange'>Change By</label>
<input type='number' id='multFactorChange'>
</div>
<div>
<input type='button' id='startPause' value='Start'>
<input type='button' id='reset' value='Reset'>
</div>
</div>
<script>
var canvas = document.getElementById('strings');
var context = canvas.getContext('2d');
context.font = '8pt Sans-Serif';
context.textAlign = 'center';
var CENTER_X = canvas.width/2;
var CENTER_Y = canvas.height/2;
var RADIUS = Math.min(canvas.width, canvas.height)/2 - 25;
var timerId;
function resetSettings () {
settings = {
multFactor: 2,
multFactorChange: 0,
numDots: 20,
numDotChange: 0,
};
document.getElementById('multFactor').value = settings.multFactor;
document.getElementById('multFactorChange').value = settings.multFactorChange;
document.getElementById('numDots').value = settings.numDots;
document.getElementById('numDotChange').value = settings.numDotChange;
}
function startAnimation() {
timerId = window.setInterval(updateStrings, 250);
var button = document.getElementById('startPause');
button.removeEventListener('click', startAnimation);
button.addEventListener('click', pauseAnimation);
button.value = 'Pause';
}
function pauseAnimation() {
window.clearInterval(timerId);
var button = document.getElementById('startPause');
button.removeEventListener('click', pauseAnimation);
button.addEventListener('click', startAnimation);
button.value = 'Start';
}
function drawCircle() {
context.beginPath();
context.arc(CENTER_X, CENTER_Y, RADIUS, 0, 2 * Math.PI);
context.strokeStyle = '#0000ff';
context.stroke();
}
function drawDots() {
var numDots = settings.numDots;
var angleIncrement = 2 * Math.PI / numDots;
context.fillStyle = '#000000';
for (index = 0; index < numDots; ++index) {
var x = RADIUS * Math.cos(2 * Math.PI - angleIncrement * index);
var y = RADIUS * Math.sin(2 * Math.PI - angleIncrement * index);
context.beginPath();
context.arc(x + CENTER_X, y + CENTER_Y, 5, 0, 2 * Math.PI);
context.fill();
context.fillText(index, x * 1.05 + CENTER_X, y * 1.05 + CENTER_Y);
}
}
function drawStrings() {
var numDots = settings.numDots;
var angleIncrement = 2 * Math.PI / numDots;
context.beginPath();
for (index = 0; index < numDots; ++index) {
var x = RADIUS * Math.cos(2 * Math.PI - angleIncrement * index) + CENTER_X;
var y = RADIUS * Math.sin(2 * Math.PI - angleIncrement * index) + CENTER_Y;
var newIndex = index * settings.multFactor;
while (newIndex >= numDots) {
newIndex -= numDots;
}
var newX = RADIUS * Math.cos(2 * Math.PI - angleIncrement * newIndex) + CENTER_X;
var newY = RADIUS * Math.sin(2 * Math.PI - angleIncrement * newIndex) + CENTER_Y;
context.moveTo(x, y);
context.lineTo(newX, newY);
}
context.strokeStyle = '#0000ff';
context.stroke();
}
function drawAll() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawCircle();
drawDots();
drawStrings();
}
function updateStrings() {
if (settings.multFactorChange == 0 && settings.numDotChange == 0) {
// nothing to do
return;
}
settings.multFactor += settings.multFactorChange;
document.getElementById('multFactor').value = settings.multFactor;
settings.numDots += settings.numDotChange;
document.getElementById('numDots').value = settings.numDots;
drawAll();
}
// initialize settings in the code and in the HTML input elements
resetSettings();
// Update settings when their corresponding input box changes
for (setting in settings) {
document.getElementById(setting).addEventListener(
'change',
function() {
var setting = this.id;
var newValue = this.value;
if (setting.substring(0, 6) == 'numDot') {
newValue = parseInt(newValue);
}
else {
newValue = parseFloat(newValue);
}
if (newValue != NaN) {
settings[setting] = newValue;
drawAll();
}
}
);
}
document.getElementById('reset').addEventListener(
'click',
function() {
resetSettings();
drawAll();
}
);
document.getElementById('startPause').addEventListener('click', startAnimation);
drawAll();
</script>
</body>
</html>