87 lines
3.3 KiB
JavaScript
87 lines
3.3 KiB
JavaScript
SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(toElement) {
|
|
|
|
return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());
|
|
|
|
};
|
|
|
|
function ns_elem () { if (typeof arguments[0] === "undefined") { console.log('There was an error in the element assist function. Called with no valid tag name!');} var elem; if (typeof arguments[1] === "string") { elem = document.createElementNS(arguments[1], arguments[0]); for (var i=2; i<arguments.length; i+=2) { if (typeof arguments[i+1] === "undefined") { for (var key in arguments[i]) { elem.setAttribute(key, arguments[i][key]); } break; } else { for (var key in arguments[i]) { elem.setAttributeNS(arguments[i+1], key, arguments[i][key]); } } } } else { elem = document.createElement(arguments[0]); for (var i=1; i<arguments.length; i+=2) { if (typeof arguments[i+1] === "undefined") { for (var key in arguments[i]) { elem.setAttribute(key, arguments[i][key]); } break; } else { for (var key in arguments[i]) { elem.setAttributeNS(arguments[i+1], key, arguments[i][key]); } } } } return elem;}
|
|
|
|
|
|
var svg_ns = "http://www.w3.org/2000/svg";
|
|
var xlink_ns = "http://www.w3.org/1999/xlink";
|
|
|
|
function norm (vector) {
|
|
var norm = Math.sqrt(vector.x*vector.x + vector.y*vector.y);
|
|
return {x: vector.x / norm, y: vector.y / norm};
|
|
}
|
|
function len (vector) {
|
|
return Math.sqrt(vector.x*vector.x + vector.y*vector.y);
|
|
}
|
|
function sub (a, b) { return {x: a.x-b.x, y: a.y-b.y}; }
|
|
function add (a, b) { return {x: a.x+b.x, y: a.y+b.y}; }
|
|
function mul (a, b) {
|
|
if ("x" in a) { return {x: a.x*b, y: a.y*b}; }
|
|
return a*b;
|
|
}
|
|
|
|
function remove_class(element, classname) {
|
|
var classlist = $(element).attr("class");
|
|
if (classlist) {
|
|
classlist = classlist.split(" ");
|
|
}
|
|
var newclasslist = "";
|
|
for (const x in classlist) {
|
|
newclasslist += (classname != classlist[x]) ? " " + classlist[x] : "";
|
|
}
|
|
$(element).attr("class", newclasslist);
|
|
}
|
|
|
|
function add_class(element, classname) {
|
|
const classlist = $(element).attr("class");
|
|
$(element).attr("class", classlist + " " + classname);
|
|
}
|
|
|
|
function svg_from_dom(point, svg) {
|
|
const pt = svg.createSVGPoint();
|
|
pt.x = point.x;
|
|
pt.y = point.y;
|
|
|
|
return pt.matrixTransform(svg.getScreenCTM().inverse());
|
|
}
|
|
function dom_from_svg(point, svg) {
|
|
const pt = svg.createSVGPoint();
|
|
pt.x = point.x;
|
|
pt.y = point.y;
|
|
|
|
return pt.matrixTransform(svg.getScreenCTM());
|
|
}
|
|
function point_from_circle(circle) {
|
|
return {x:parseFloat(circle.attr('cx')), y:parseFloat(circle.attr('cy'))};
|
|
}
|
|
var previous_mouse_position = {x:0,y:0};
|
|
|
|
function set_transform(element, point, rotation) {
|
|
const transform_string = `translate(${point.x},${point.y}) rotate(${180*rotation/Math.PI})`;
|
|
|
|
return $(element).attr("transform", transform_string);
|
|
}
|
|
|
|
function remove(list, element) {
|
|
const index = list.indexOf(element);
|
|
if (index > -1) { // only splice array when item is found
|
|
list.splice(index, 1); // 2nd parameter means remove one item only
|
|
}
|
|
}
|
|
|
|
function appropriate_angle(point) {
|
|
return -Math.atan2(point.x, point.y);
|
|
}
|
|
|
|
function newsvg(element) {
|
|
document.createElementNS(svg_ns, element);
|
|
}
|
|
function set_loc(elem, point) { return $(elem).attr("cx", point.x).attr("cy",point.y)}
|
|
|
|
function get_global_point_at_length(svg, path, distance) {
|
|
return path.getPointAtLength(distance).matrixTransform(path.getTransformToElement(svg));
|
|
} |