/* * Verrückte Turing-Maschinen — A turing complete game * Copyright (C) 2016-2018 kcf * * 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 . */ module lib_svg { /** * @author kcf */ export const float_precision : int = 4; /** * @author kcf */ export function rotation ( staerke : float ) : string { return ( "rotate" + "(" + (staerke * 360).toFixed(float_precision) + ")" ); } /** * @author kcf */ export function translation ( x : float, y : float ) : string { return ( "translate" + "(" + x.toFixed(float_precision) + ", " + y.toFixed(float_precision) + ")" ); } /** * @author kcf */ export function scaling ( staerke : float ) : string { return ( "scale" + "(" + staerke.toFixed(float_precision) + ")" ); } /** * @author kcf */ export function path_description ( vertices : Array, close : boolean = true ) : string { 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"; return d; } /** * @author kcf */ export function path ( vertices : Array, close : boolean = true, attributes : {[name : string] : string} = {}, ) : lib_xml.type_node { attributes["d"] = path_description(vertices, close); return lib_xml.create_normal("path", attributes); } /** * @author kcf */ 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 { 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, ) ); } }