This commit is contained in:
djerom
2022-04-22 18:34:31 +05:00
commit 813abcd9d4
24 changed files with 1228 additions and 0 deletions

77
libs/bbox.js Normal file
View File

@@ -0,0 +1,77 @@
class BBox {
static fromLTWH(l, t, w, h){
return BBox.fromLTRB(l, t, l + w, t + h)
}
static fromLTRB(l, t, r, b){
return Object.assign(new BBox(), {l: Math.min(l, r), t: Math.min(t, b), r: Math.max(l, r), b: Math.max(t, b)})
}
static from_array(arr_xy){
const bbox = BBox.fromLTWH(arr_xy[0].x, arr_xy[0].y, 0, 0)
return bbox.append_many(arr_xy)
}
w(){
return this.r - this.l
}
h(){
return this.b - this.t
}
toLTWH(){
const {l, t, r, b} = this
return {l, t, w: r - l, h: b - t }
}
toLTRB(){
const {l, t, r, b} = this
return {l, t, r, b}
}
toLTRBarr(){
const {l, t, r, b} = this
return [l, t, r, b]
}
append(x, y){
this.l = Math.min(this.l, x)
this.t = Math.min(this.t, y)
this.r = Math.max(this.r, x)
this.b = Math.max(this.b, y)
return this
}
append_many(arr_xy){
arr_xy.forEach(e => this.append(e.x, e.y))
return this
}
scale(k){
this.r = this.l + (this.r - this.l) * k
this.b = this.t + (this.b - this.t) * k
return this
}
move(dx, dy){
this.l += dx
this.t += dy
this.r += dx
this.b += dy
return this
}
moveto(x, y){
let dx = x - this.l
let dy = y - this.t
return this.move(dx, dy)
}
clone(){
return BBox.fromLTRB(this.l, this.t, this.r, this.b)
}
}
module.exports = BBox
module.exports.default = BBox

58
libs/coord-system.js Normal file
View File

@@ -0,0 +1,58 @@
class CoordSystem{
/**
* Создает ненормированную систему координат с началом в x0,y0 и направлением x1,y1
* @param {*} x0
* @param {*} y0
* @param {*} x1
* @param {*} y1
* @returns
*/
constructor(x0, y0, x1, y1) {
this.x0 = x0
this.y0 = y0
this.x1 = x1
this.y1 = y1
}
scale(k){
this.x1 *= k
this.y1 *= k
return this
}
move(dx, dy){
this.x0 += dx
this.y0 += dy
this.x1 += dx
this.y1 += dy
return this
}
moveto(x, y){
let dx = x - this.x0
let dy = y - this.y0
return this.move(dx, dy)
}
flipx(){
let x = this.x0
this.x0 = this.x1
this.x1 = x
// this.x0 += 500
// this.x1 = 2 * this.x0 - this.x1 + 500
return this
}
flipy(){
this.y1 = 2 * this.y0 - this.y1
return this
}
clone(){
return new CoordSystem(this.x0, this.y0, this.x1, this.y1)
}
};
module.exports = CoordSystem
module.exports.default = CoordSystem

91
libs/file.js Normal file
View File

@@ -0,0 +1,91 @@
export default {
/**
* Downloads file
* @param {*} filename
* @param {*} data
*/
download(filename, data) {
var element = document.createElement("a");
element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(data));
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
/**
* Convert data of file selected in input to base64 string
* @param {*} input
*/
async toBase64(input) {
return new Promise((resolve) => {
if (input.files.length == 0){
resolve(null)
}
else if (input.files.length == 1){
this.fileToBase64(input.files[0]).then(resolve)
}
else
Promise.all(Array(...input.files).map(this.fileToBase64)).then(resolve)
}
);
},
/**
* Convert data of file selected in input to base64 string
* @param {*} input
*/
async fileToBase64(file) {
return new Promise((resolve, reject) => {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
reject(error)
};
});
},
/**
* Read data of file/files selected in input as string
* @param {*} input
*/
async read(input) {
return new Promise((resolve) => {
if (input.files.length == 0){
resolve(null)
}
else if (input.files.length == 1){
this.readFile(input.files[0]).then(resolve)
}
else
Promise.all(Array(...input.files).map(this.readFile)).then(resolve)
}
);
},
/**
* Read data of file/files selected in input as string
* @param {*} input
*/
async readFile(file) {
return new Promise((resolve, reject) => {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
reject(error)
};
});
},
};

13
libs/math.js Normal file
View File

@@ -0,0 +1,13 @@
export default {
/**
* Создает данные дл круговой диаграммы из набора значений v0,v1,v2,v3 -> [{a0: 0, a1: v0}, {a0: v0, a1: v0+v1}, {a0: v0+v1, a1: v0+v1+v2},]
* @param { [Number] } values Значения долей
* @param { boolean } norm Нормировать ли значения диапазонов в отрезок 0-1
* @returns Массив с диапазонами долей 1,2,3,4 -> [{0,0.1},{0.1,0.3},{0.3,0.6},{0.6,1}]
*/
make_ranges(values, norm = true) {
let ranges = values.reduce((s, c, i) => [...s, {...c, a0: i && s[i - 1].a1, a1: c + (i && s[i - 1].a1)}], [])
let max = ranges[ranges.length - 1].a1
return norm ? ranges.map(x => ({a0: x.a0 / max, a1: x.a1 / max})) : ranges
},
};

33
libs/transform.js Normal file
View File

@@ -0,0 +1,33 @@
class Transfrom {
static fromCoordSyses(cs1, cs2){
let tr = new Transfrom()
tr.cs1 = cs1
tr.cs2 = cs2
let dx1 = cs1.x1 - cs1.x0;
let dy1 = cs1.y1 - cs1.y0;
let dx2 = cs2.x1 - cs2.x0;
let dy2 = cs2.y1 - cs2.y0;
tr.kx12 = dx1 ? dx2 / dx1 : 1;
tr.ky12 = dy1 ? dy2 / dy1 : 1;
return tr
}
trx(x){
return (x - this.cs1.x0) * this.kx12 + this.cs2.x0;
}
try(y){
return (y - this.cs1.y0) * this.ky12 + this.cs2.y0;
}
trp(p){
return {x: this.trx(p.x), y: this.trx(p.y)}
}
}
module.exports = Transfrom
module.exports.default = Transfrom