tanghaolin
2025-03-06 2fdfc7907920e9003c62445b6000dc3c9edd6edb
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import window from "./ElementPrototype";
var extend = function (c, o) {
    if (o == null) {
        return c;
    }
    ;
    if (c == null) {
        c = {};
    }
    ;
    for (var key in o) {
        c[key] = o[key];
    }
    ;
    return c;
};
 
var svgObject = function () {
    var elementType = {
        "svg": "svg",
        "path": 'path',
        "rect": "rect",
        "g": "g",
        "circle": "circle",
        "text": "text",
        "image": "image"
    };
    
    var root_node = null; 
    var namespaceURI = "http://www.w3.org/2000/svg";
    //
    this.createArea = function (attr, style) {
        var areaAttr = extend({}, attr);
        var areaStyle = extend({}, style);
        var area =   document.createElementNS(namespaceURI, elementType.rect);
        root_node.appendChild(area 
            .setAttr(areaAttr).setStyle(areaStyle));
        return area;
    };
    //
    this.createRect = function (attr, style, name) {
        var areaAttr = extend({}, attr);
        var    areaStyle = extend({}, style);
        var  rect = document.createElementNS(namespaceURI, elementType.rect).setAttr(areaAttr).setStyle(areaStyle);
        rect.createKey(name);
        //paths[name] = rect.setAttr();
        root_node.appendChild(rect);
        return rect;
    }
 
    this.createText = function (attr, style, txt, zIndex, name, transform) {
        var g = document.createElementNS(namespaceURI, elementType.g);
        var textNode = document.createTextNode(txt); 
        var text = document.createElementNS(namespaceURI, elementType.text);
        text.createKey(name); //就是ID
        if (transform != null)
            text.setAttr("transform", transform);
        text.setAttr(attr).setAttr("style", style || "").appendChild(textNode);
        g.setAttr("zIndex", (zIndex || 7)).appendChild(text);
        root_node.appendChild(g);
 
    };
 
    this.createImage = function (attr, style, name, pict_path) {
        var areaAttr = extend({}, attr);
        var areaStyle = extend({}, style);
        areaAttr.href = pict_path;
        var imag = document.createElementNS(namespaceURI, "image");
        imag.setAttr(areaAttr);//.setStyle(areaStyle);
        if(name)
            imag.createKey(name);
        root_node.appendChild(imag);
    }
 
    this.removeByName = function (name) {
        var elm = this.getPathByName(name);
        if (typeof elm === "Element") {
            root_node.remove(this.getPathByName(name));
        }
        //if (paths[name] != null)
        //    root_node.removeChild(paths[name])
    };
 
    this.createCircle = function (name, attr, style) {
        var circle = document.createElementNS(namespaceURI, elementType.circle);
        if(name)
        circle.createKey(name); 
        if (style != null) {
            circle.setAttr(attr);
        }
        else {
            circle.setAttr(attr).setAttr("style", style);
        } 
        root_node.appendChild(circle);
        return circle;
    };
 
    this.createPath = function (name, value, attr, style) {
        //console.log(name)
        var path = document.createElementNS(namespaceURI, elementType.path);
        if(name)
            path.createKey(name);
        root_node.appendChild(path);
        this.setPathValue(path, value, attr, style)
        return path;
    };
 
    this.getPathByName = function (name) {
        return document.getElementById(name);
    };
 
    this.setPathValue = function (path, value, attr, style) {
        if (value == null) {
            return;
        }
        path = typeof path === "string" ? this.getPathByName(path) : path;
        path.setAttr("d", value).setAttr(attr || {}).setStyle(style || {});
    };
 
    this.getRootNode = function () {
        return root_node;
    };
 
    this.init = function (divName, divWidth, divHeight, svgClick, svgMove, svgID) {
        var container = document.getElementById(divName);
        var size = {
            "width": divWidth  || 800,
            "height": divHeight  || 600
        };
        //
        if (root_node == null) {
            root_node = document.createElementNS(namespaceURI, elementType.svg);
            root_node.setAttribute("xmlns", "http://www.w3.org/2000/svg")
            root_node.setAttribute("shape-rendering", "geometricPrecision");
            root_node.setAttribute("version", "1.1");
            if (svgID != null)
                root_node.setAttribute("id", svgID);
 
            root_node.on("mousemove", function (e) {
                if (typeof svgMove == "function") {
                    var e = e || window.event;
                    svgMove(e, root_node);
                }
            });
 
            root_node.on("click", function (e) {
                if (typeof svgClick == "function") {
                    var e = e || window.event;
                    svgClick(e, root_node);
                }
            });
            //root_node.setAttribute("preserveAspectRatio","xMinYMin meet" );
            //root_node.setAttribute("viewBox","0 0 "+divWidth*2  +" "+divHeight*2  );
        }
        root_node.setAttr(size);
        //console.log(divName,size,root_node);
        container.appendChild(root_node);
        return this;
    };
};
export default svgObject