-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUtil.js
131 lines (120 loc) · 3 KB
/
Util.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
//@ts-check
'use strict';
/* jshint esversion:6 */
export const Util = {
/**
* @param {SVGPoint} pt1
* @param {SVGPoint} pt2
* @returns {number}
*/
getDistance: function(pt1, pt2) {
return Math.hypot(pt2.x - pt1.x, pt2.y - pt1.y); // see 30 seconds of code
},
/**
* @param {number} n
* @param {string} word
* @returns {string}
*/
plural: function(n, word) {
if ( 0 === n ) {
return `no ${word}s`;
} else if ( 1 === n ) {
return `${n} ${word}`;
} else {
return `${n} ${word}s`;
}
},
/**
* @param {(number|SVGPoint)} x
* @param {number=} y
* @returns {SVGPoint}
*/
newPoint: function(x, y=undefined) {
// https://developer.mozilla.org/en-US/docs/Web/API/SVGPoint
const pt = document.getElementById('baize').createSVGPoint();
if ( typeof x === 'object' ) {
pt.x = x.x;
pt.y = x.y;
} else if ( typeof x === 'number' && typeof y === 'number' ) {
pt.x = x;
pt.y = y;
} else {
throw new TypeError();
}
return pt;
},
/**
* @param {SVGPoint} ptDst
* @param {SVGPoint} ptSrc
*/
copyPoint: function(ptDst, ptSrc) {
ptDst.x = ptSrc.x;
ptDst.y = ptSrc.y;
},
// samePoint: function(pt1, pt2) {
// return ( (pt1.x === pt2.x) && (pt1.y === pt2.y) );
// },
/**
* @param {SVGPoint} pt1
* @param {SVGPoint} pt2
* @param {number=} slack
* @returns {boolean}
*/
nearlySamePoint: function(pt1, pt2, slack=8) {
const xMin = pt1.x - slack;
const xMax = pt1.x + slack;
const yMin = pt1.y - slack;
const yMax = pt1.y + slack;
return ( pt2.x > xMin && pt2.x < xMax && pt2.y > yMin && pt2.y < yMax );
},
/**
* @param {number} x
* @param {number} y
* @returns {SVGPoint}
*/
DOM2SVG: function(x, y) {
// https://www.sitepoint.com/how-to-translate-from-dom-to-svg-coordinates-and-back-again/
const pt = Util.newPoint(x,y);
// https://developer.mozilla.org/en-US/docs/Web/API/SVGGraphicsElement
pt.matrixTransform(document.getElementById('baize').getScreenCTM().inverse());
pt.x = Math.round(pt.x);
pt.y = Math.round(pt.y);
return pt;
},
/**
* @param {Event} event
* @returns {boolean}
*/
absorbEvent: function(event) {
var e = event || window.event;
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
return false;
},
/**
*
* @param {Element} ele
* @param {Object} attribs
*/
setAttributesNS(ele, attribs) {
for ( let a in attribs ) {
ele.setAttributeNS(null, a, attribs[a]);
}
},
/**
* @param {string} id
*/
play(id) {
let ele = /** @type {HTMLMediaElement} */(document.querySelector(`audio#${id}`));
var promise = ele.play();
if (promise !== undefined) {
promise.then(_ => {
// console.log(`Play audio#${id}`);
}).catch(error => {
// console.log(`Autoplay prevented audio#${id}`);
});
}
},
};