forked from shorstok/3k-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels.js
359 lines (268 loc) · 9.32 KB
/
labels.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
window.addEventListener('load', function ()
{
//add label renderer to mode-independent callback list
modeIndependentRenderers.push(drawLabels);
});
function createLabel(xstart, ystart, ctx) {
var colFill = null;
var tpFilltype = ChordHighlightType.None;
//If circle-of-fifths-specific 'sector highlight' checkbox is ticked
if(document.querySelector("#label-background-highlight").checked)
{
colFill = document.querySelector("#chord-color").value; //set in 'chord highlight' section
tpFilltype = ChordHighlightType.Sector;
}
return {
header: document.querySelector("#label-text").value,
color: document.querySelector("#label-color").value,
size: document.querySelector("#label-size").value,
fill : colFill,
filltype : tpFilltype,
x: xstart,
y: ystart,
angle: 0,
_drawRotationHandle : null,
}
}
var _rotationModeKeycode = 16;
var _arbitraryAngleKeycode = 17;
/**
* Kinda state machine for switching between moving/rotating modes
*/
var LabelEditModes = Object.freeze({
None: {
mousemove: null,
mousedown: function (x, y, canvas) {
if (document.querySelector("#label-text").value.length < 1) {
document.querySelector("#label-text-validation").style.display = 'block';
redraw();
return;
}
//If 'override chord name' mode is on, find clicked chord and override
//its name
if(document.querySelector("#label-override-co5-chord-name").checked){
overrideCo5Label(x,y,canvas);
return;
}
document.querySelector("#label-text-validation").style.display = 'none';
labelMode = LabelEditModes.Moving;
activeLabels.push(createLabel(x, y, canvas));
redraw();
},
mouseup: null
},
Moving: {
mousemove: function(x,y,canvas) {
if (activeLabels.length < 1)
labelMode = LabelEditModes.None;
activeLabels[activeLabels.length - 1].x = x;
activeLabels[activeLabels.length - 1].y = y;
redraw();
},
keydown: function (code) {
if (code === _rotationModeKeycode)
labelMode = LabelEditModes.Rotating;
},
mousedown: function(x,y,c) {
labelMode = LabelEditModes.None;
redraw();
},
mouseup: null
},
Rotating: {
snap45 : true,
mousemove: function (x, y, canvas) {
if (activeLabels.length < 1)
labelMode = LabelEditModes.None;
var dx = x - activeLabels[activeLabels.length - 1].x;
var dy = y - activeLabels[activeLabels.length - 1].y;
var angle = Math.atan2(dy, dx);
if (this.snap45)
angle = Math.round(angle / (Math.PI / 4)) * (Math.PI / 4);
activeLabels[activeLabels.length - 1].angle = angle
activeLabels[activeLabels.length - 1]._drawRotationHandle = {x:x,y:y};
redraw();
},
keydown: function (code) {
if (code === _arbitraryAngleKeycode)
this.snap45 = false;
},
keyup: function (code) {
if (code === _arbitraryAngleKeycode)
this.snap45 = true;
},
mousedown: function (x, y, c) {
this.snap45 = true;
labelMode = LabelEditModes.Moving;
activeLabels[activeLabels.length - 1]._drawRotationHandle = null;
redraw();
},
mouseup: null
}
});
var activeLabels = [];
var labelMode = LabelEditModes.None;
/***/
function createLabelsHandler(px, py, canvas, evtype, keyCode) {
switch (evtype) {
case 'mousedown':
if(labelMode.mousedown!=null)
labelMode.mousedown(px, py, canvas, keyCode);
break;
case 'mousemove':
if (labelMode.mousemove != null)
labelMode.mousemove(px, py, canvas, keyCode);
break;
case 'mouseup':
case 'mouseleave':
if (labelMode.mouseup != null)
labelMode.mouseup(px, py, canvas, keyCode);
break;
case 'keydown':
if (typeof labelMode.keydown != 'undefined')
labelMode.keydown(keyCode);
break;
case 'keyup':
if (typeof labelMode.keyup != 'undefined')
labelMode.keyup(keyCode);
break;
default:
break;
}
}
function removeLastLabel() {
activeLabels.splice(activeLabels.length - 1, 1);
redraw();
}
function removeAllLabels() {
activeLabels = [];
restoreOriginalCo5ChordNames();
redraw();
}
/**
*
* @param {Integer} x Click position (x)
* @param {IntersectionObserverInit} y Click position (y)
* @param {Canvas} canvas
*/
function overrideCo5Label(x,y,canvas){
var chord = getChordDefinitionFromPosition(x,y,canvas.clientWidth,
canvas.clientHeight);
if(null == chord)
return;
if(!chord.isCustomName)
{
chord.originalName = {
base: chord.base,
mod: chord.baseMod
};
}
chord.base = document.querySelector("#label-text").value;
chord.baseMod = null;
chord.isCustomName = true;
redraw();
}
function restoreOriginalCo5ChordNames(){
for (let chord of chordDefinitions)
{
if(!chord.isCustomName)
continue;
chord.base = chord.originalName.base;
chord.baseMod = chord.originalName.mod;
chord.isCustomName = false;
}
}
function normalizeAngle(a){
while(a < -Math.PI)
a+=Math.PI*2;
while(a > Math.PI)
a-=Math.PI*2;
return a;
}
function getAngleDifference(a1,a2){
return normalizeAngle(normalizeAngle(a1)-normalizeAngle(a2));
}
/**
* Main label drawing function
* @param {Object<>} ctx context
* @param {number} width
* @param {number} height
* @returns {}
*/
function drawLabels(ctx, width, height) {
for (let label of activeLabels) {
ctx.font = label.size + "px Arial";
var dim = {
x: ctx.measureText(label.header).width,
y: ctx.measureText("M").width
}
if(label.filltype === ChordHighlightType.Sector){
var sector = getSectorNumberAndRadiusFromPixelPosition(label.x,label.y,width,height);
var bounds = getChordBoundsFromChordPosition(sector.sector,2);
var xc = width/2;
var yc = height/2;
var r = Math.min(xc, yc) - circleParameters.marginPx;
var sectorSpan = circleParameters.sectorRadians / 2;
var maxDeltaAngle = 0;
//Check all four bounding-box (AABB) corners for angle violations
for(var ix = 0; ix!==2; ++ix)
{
for(var iy =0; iy!==2; ++iy)
{
var probe = Math.atan2(
label.y - yc + (iy*2-1)*dim.y/2,
label.x - xc + (ix*2-1)*dim.x/2);
maxDeltaAngle = Math.max(maxDeltaAngle,
Math.abs(getAngleDifference(bounds.angle,probe)));
}
}
//Limit sector expansion to 3 sectors
var maxDeltaAngle = Math.min(maxDeltaAngle, circleParameters.sectorRadians*3/2);
sectorSpan = Math.max(sectorSpan,maxDeltaAngle);
var amin = bounds.angle - sectorSpan;
var amax = bounds.angle + sectorSpan;
//Fill sector
ctx.globalAlpha = 0.4;
ctx.fillStyle = label.fill;
ctx.beginPath();
ctx.arc(xc,yc,bounds.offmax * r,amin,amax);
ctx.arc(xc,yc,bounds.offmin * r,amax,amin,true);
ctx.fill();
}
if (label._drawRotationHandle!=null) {
ctx.globalAlpha = 1;
ctx.strokeStyle = 'rgb(200,200,200)';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(label.x, label.y);
ctx.lineTo(label._drawRotationHandle.x, label._drawRotationHandle.y);
ctx.stroke();
ctx.fillStyle = 'rgb(200,200,200)';
ctx.beginPath();
ctx.arc(label._drawRotationHandle.x, label._drawRotationHandle.y, 4, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(label.x, label.y, 4, 0, Math.PI * 2);
ctx.fill();
}
ctx.save();
ctx.translate(label.x, label.y);
ctx.rotate(label.angle);
ctx.translate(-dim.x / 2, dim.y / 2);
//Only stroke if no fill is active
if(label.filltype === ChordHighlightType.None)
{
ctx.globalAlpha = 0.15;
ctx.fillStyle = 'white';
var strokeSize = 4;
for (var offx = -strokeSize; offx < strokeSize; ++offx)
for (var offy = -strokeSize; offy < strokeSize; ++offy)
ctx.fillText(label.header, offx, offy);
}
ctx.globalAlpha = 1;
ctx.fillStyle = label.color;
// ctx.fillText(label.header, label.x - dim.x / 2, label.y + dim.y / 2);
ctx.fillText(label.header, 0, 0);
ctx.restore();
}
}