-
Notifications
You must be signed in to change notification settings - Fork 15
/
path.js
144 lines (131 loc) · 4.36 KB
/
path.js
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
import { create } from "d3-selection";
const d3 = Object.assign(
{},
{
create,
}
);
// path
export function path(selection, width, height, options = {}, clipid) {
let style = options.style ? options.style : "fit";
let margin = options.margin ? options.margin : 0;
let d = options.d;
let rotate = options.rotate ? options.rotate : 0;
let fill = options.fill ? options.fill : "#4a4a4a";
let strokeLinecap = options.strokeLinecap ? options.strokeLinecap : "round";
let strokeLinejoin = options.strokeLinejoin
? options.strokeLinejoin
: "round";
let strokeDasharray =
options.strokeDasharray != undefined ? options.strokeDasharray : "none";
let stroke = options.stroke ? options.stroke : "#2e2e2e";
let strokeWidth = options.strokeWidth != undefined ? options.strokeWidth : 1;
let fillOpacity = options.fillOpacity != undefined ? options.fillOpacity : 1;
let strokeOpacity =
options.strokeOpacity != undefined ? options.strokeOpacity : 1;
let x = options.x ? options.x : 0;
let y = options.y ? options.y : 0;
let scale = options.scale ? options.scale : 1;
let tmp = selection.append("path").attr("d", d);
let objsize = getsize(tmp);
tmp.remove();
if (!Array.isArray(margin)) {
margin = [margin, margin, margin, margin];
}
// Fit in the top left corner
let sel = selection
.append("g")
.attr("class", options.id)
.attr("data-layer", JSON.stringify({ _type: "path" }));
if (style == "fit") {
sel
.append("path")
.attr("fill", fill)
.attr("fill-opacity", fillOpacity)
.attr("stroke-opacity", strokeOpacity)
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth)
.attr("stroke-linecap", strokeLinecap)
.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-dasharray", strokeDasharray)
.attr("d", d)
.attr(
"transform",
`translate(${x - objsize.x * scale + margin[3]},${
y - objsize.y * scale + margin[0]
}) scale(${scale}) rotate(${rotate} ${objsize.x + objsize.width / 2} ${
objsize.y + objsize.height / 2
})`
);
}
// Display as it is
if (style == "raw") {
sel
.append("path")
.attr("fill", fill)
.attr("fill-opacity", fillOpacity)
.attr("stroke-opacity", strokeOpacity)
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth)
.attr("stroke-linecap", strokeLinecap)
.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-dasharray", strokeDasharray)
.attr("d", d)
.attr(
"transform",
`translate(${x},${y}) scale(${scale}) rotate(${rotate} ${
objsize.x + objsize.width / 2
} ${objsize.y + objsize.height / 2} )`
);
}
// Repeat the path on the page
if (style == "repeat") {
let nbx = width / (objsize.width * scale + margin[1] + margin[3]);
let nby = height / (objsize.height * scale + margin[0] + margin[2]);
let mypatern = sel.attr(
"clip-path",
clipid == null ? `none` : `url(#clip_${clipid})`
);
for (let i = 0; i <= nbx; i++) {
for (let j = 0; j <= nby; j++) {
mypatern
.append("path")
.attr("fill", fill)
.attr("fill-opacity", fillOpacity)
.attr("stroke-opacity", strokeOpacity)
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth)
.attr("stroke-linecap", strokeLinecap)
.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-dasharray", strokeDasharray)
.attr("d", d)
.attr(
"transform",
`translate(${
x +
margin[3] -
objsize.x * scale +
i * (objsize.width * scale + margin[1] + margin[3])
},${
y +
margin[0] -
objsize.y * scale +
j * (objsize.height * scale + margin[0] + margin[2])
}) scale(${scale}) rotate(${rotate} ${
objsize.x + objsize.width / 2
} ${objsize.y + objsize.height / 2})`
);
}
}
}
}
function getsize(elt) {
const clonetxt = elt.clone(true);
const svg = d3.create("svg");
svg.node().appendChild(clonetxt.node());
document.body.appendChild(svg.node());
const { x, y, width, height } = clonetxt.node().getBBox();
document.body.removeChild(svg.node());
let dims = { x, y, width, height };
return dims;
}