vtm/source/helpers/svg.ts
Christian Fraß 931b6f61d3 update
2018-03-29 22:00:42 +02:00

154 lines
3.1 KiB
TypeScript

/*
* Verrückte Turing-Maschinen — A turing complete game
* Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module lib_svg
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export const float_precision : int = 4;
/**
* @author kcf <vidofnir@folksprak.org>
*/
export const shape_arrow : string = "M +4 0 L 0 +1 L 0 -1 Z";
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function rotation
(
staerke : float
)
: string
{
return (
"rotate"
+ "("
+ (staerke * 360).toFixed(float_precision)
+ ")"
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function translation
(
x : float,
y : float
)
: string
{
return (
"translate"
+ "("
+ x.toFixed(float_precision)
+ ", "
+ y.toFixed(float_precision)
+ ")"
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function skalierung
(
staerke : float
)
: string
{
return (
"scale"
+ "("
+ staerke.toFixed(float_precision)
+ ")"
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function path
(
vertices : Array<lib_vector.type_vector>,
close : boolean = true,
attributes : {[name : string] : string} = {},
)
: lib_xml.type_node
{
let d : string = "";
vertices.forEach
(
(vertex, index) =>
{
let c : string = ((index <= 0) ? "M" : "L");
let x : string = vertex.x.toFixed(float_precision);
let y : string = vertex.y.toFixed(float_precision);
d += [c, x, y].join(" ");
}
)
;
if (close)
d += "Z";
attributes["d"] = d;
return (lib_xml.create_normal("path", attributes));
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function root
(
from_x : float,
from_y : float,
to_x : float,
to_y : float,
height : int = 500,
width : int = 500,
children : Array<lib_xml.type_node> = []
)
: lib_xml.type_node
{
return (
lib_xml.create_normal
(
"svg",
{
"xmlns": "http://www.w3.org/2000/svg",
"xmlns:xlink": "http://www.w3.org/1999/xlink",
"width": width.toFixed(0),
"height": height.toFixed(0),
"viewBox": [from_x.toFixed(4), from_y.toFixed(4), (to_x-from_x).toFixed(4), (to_y-from_y).toFixed(4)].join(" "),
},
children,
)
);
}
}