-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvgplotlib.js
79 lines (73 loc) · 2.78 KB
/
svgplotlib.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
/*
File name: svgplotlib.js
Copyright (c) 2023-2025 Neruthes.
This file is released with the MIT license.
*/
function drawstar(opt) {
let myshape_POINTS = [];
const VERTEX_QTY = opt.vert || 12;
for (let itr = 0; itr < VERTEX_QTY; itr += 1) {
const vertexDistance = (itr % 2 === 0) ? opt.long : opt.short;
const theta_rad = Math.PI * 2 / VERTEX_QTY * itr;
const x1 = Math.round(100 * vertexDistance * Math.sin(theta_rad)) / 100;
const y1 = Math.round(100 * vertexDistance * Math.cos(theta_rad)) / 100;
myshape_POINTS.push(x1, y1);
};
const DEFAULT_ATTRS = {
'stroke-width': '1.00'
};
let attrs = ''
if (opt.attrs) {
let new_attr_obj = {};
Object.keys(DEFAULT_ATTRS).map(function (attrname) { new_attr_obj[attrname] = DEFAULT_ATTRS[attrname] }); // Initialize content
Object.keys(opt.attrs).map(function (attrname) { new_attr_obj[attrname] = opt.attrs[attrname] }); // Overwrite
attrs = Object.keys(new_attr_obj).map(function (attrname) {
return ` ${attrname}="${new_attr_obj[attrname]}" `;
}).join(' ');
} else {
attrs = Object.keys(DEFAULT_ATTRS).map(function (attrname) {
return ` ${attrname}="${DEFAULT_ATTRS[attrname]}" `;
}).join(' ');
};
let tmpnode = `<polygon ${attrs} points="${myshape_POINTS.join(' ')}" />`;
return tmpnode;
};
function drawpolarcircle(opt) {
let myshape_POINTS = [];
for (let itr = 0; itr < 360; itr += opt.step || 1) {
const theta_rad = itr / 360 * Math.PI * 2;
const vertexDistance = opt.func(theta_rad);
const x1 = Math.round(100 * vertexDistance * Math.sin(theta_rad)) / 100;
const y1 = Math.round(100 * vertexDistance * Math.cos(theta_rad)) / 100;
myshape_POINTS.push(x1, y1);
};
const DEFAULT_ATTRS = {
'stroke-width': '1.00'
};
let attrs = ''
if (opt.attrs) {
let new_attr_obj = Object.assign({}, DEFAULT_ATTRS, opt.attrs);
attrs = flatten_attr_obj(new_attr_obj);
} else {
attrs = flatten_attr_obj(DEFAULT_ATTRS);
};
// let tmpnode = `<polygon ${attrs} points="${myshape_POINTS.join(' ')}" />`;
let tmpnode = `<polygon ${attrs} points="${_join_points(myshape_POINTS)}" />`;
return tmpnode;
};
function _join_points(data_arr) {
let counter = 0;
let tmpstr = '';
while (counter < data_arr.length) {
const separator = (counter % 100 === 0) ? '\n' : ' ';
tmpstr += data_arr[counter] + separator;
counter += 1;
};
return tmpstr;
}
function flatten_attr_obj(attr_obj) {
return Object.keys(attr_obj).map(function (attrname) {
return ` ${attrname}="${attr_obj[attrname]}" `;
}).join('');
};
module.exports = { drawstar, drawpolarcircle };