-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrosvg.js
71 lines (59 loc) · 1.77 KB
/
microsvg.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
// Micro charts implementation utilities.
//
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD 3-Clause license.
//
// * 08.08.2020 rework.
/**
* Create a SVG element
* @param {SVGAElement} parentNode container node for the new element
* @param {string} tagName tagName of the new element
* @param {Object | undefined} attr attributes of the new element passed as Object
* @param {string | undefined} txt inner text content.
*/
function createSVGNode(parentNode, tagName, attr, txt) {
var n = document.createElementNS("http://www.w3.org/2000/svg", tagName);
if (attr) {
Object.getOwnPropertyNames(attr).forEach(function(p) {
n.setAttribute(p, attr[p]);
});
}
if (txt) { n.textContent = txt; }
parentNode.appendChild(n);
return (n);
} // createSVGNode()
function _removeChilds(p) {
Array.from(p.childNodes).forEach(function (c) {
c.remove();
});
} // _removeChilds()
/**
* Calculate the event position using document units from mouse position.
* @param {*} evt
*/
function eventPoint(evt) {
var svg = document.documentElement;
var pt = svg.createSVGPoint();
pt.x = evt.clientX;
pt.y = evt.clientY;
return pt.matrixTransform(svg.getScreenCTM().inverse());
} // eventPoint
/**
* Calculate a point on the circle
* @param {number} alpha degree of angle
* @param {number} r radius of circlt
* @returns object with x and y coordinates.
*/
function _cPoint(alpha, r) {
return ({
x: (Math.sin(alpha) * r),
y: (-Math.cos(alpha) * r)
});
} // _cPoint()
/**
* Utility function to set options in the api by using an Object
* @param {any} options Object with options as properties.
*/
function _setOptions(options) {
Object.assign(document.api.options, options);
} // _setOptions