Newer
Older
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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Page Title</title>
<style>
circle {
fill: steelblue;
stroke: #fff;
stroke-width: 3px;
}
path {
fill: none;
stroke: #000;
stroke-width: 1px;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var svg = d3.select("body").append("svg")
.attr("width", 1000)
.attr("height", 500)
.attr("viewBox", "0 0 500 250");
var path = svg.append("path")
.attr("d", "m 82.393993,102.36183 c -13.481701,0.0332 -35.416429,-15.12149 -35.446705,-28.75598 -0.03028,-13.63449 20.096934,-32.30487 35.446705,-32.64203 15.349771,-0.33716 49.390017,61.33466 61.043907,61.39801 11.65389,0.0634 39.30246,-15.12148 39.33273,-28.75598 0.0303,-13.6345 -23.98295,-32.30489 -39.33273,-32.64203 -15.34978,-0.33714 -47.562206,61.36486 -61.043907,61.39801 z"
);
//transition();
function getRandomColorRgb() {
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);
return `rgb(${red}, ${green}, ${blue})`;
}
function pointDist(point, distance, rad) {
return { x: point.x + distance * Math.cos(rad), y: point.y + distance * Math.sin(rad) };
}
class ArowOnPath {
constructor(path, start, length, parts = 3, thickness = 5, angle = 2, padding = 1) {
this.path = path;
this.angle = angle;
this.thickness = thickness;
var partlen = (length - (2 * padding)) / (2*parts);
this.p = [this.pointsAt(start+padding,(-1*padding))];
for (i = 1; i < (2 * parts); i++) {
this.p.push(this.pointsAt(start + (i * partlen)));
}
this.p.push(this.pointsAt(start + length-padding,padding));
}
pointAngle(length) {
var p1 = this.path.getPointAtLength(length);
var p2 = this.path.getPointAtLength(length + 0.01);
return Math.atan2((p2.y - p1.y), (p2.x - p1.x));
}
pointsAt(length, angleoffset = 0) {
var m = this.path.getPointAtLength(length);
var m_angle = this.pointAngle(length + angleoffset)
var a = {
x: m.x + (this.thickness * Math.cos(m_angle + this.angle))
, y: m.y + (this.thickness * Math.sin(m_angle + this.angle))
};
var b = {
x: m.x + (this.thickness * Math.cos(m_angle - this.angle))
, y: m.y + (this.thickness * Math.sin(m_angle - this.angle))
};
return { a: a, m: m, b: b }
}
p2s(point) {
return point.x.toString() + "," + point.y.toString();
}
d3path() {
var path = d3.path();
console.log(this.p.length);
var i = 0
console.log(i);
path.moveTo(this.p[i].m.x, this.p[i].m.y);
path.lineTo(this.p[i].a.x, this.p[i].a.y);
do {
i++;
path.bezierCurveTo(this.p[i].a.x, this.p[i].a.y,
this.p[i].a.x, this.p[i].a.y,
this.p[i + 1].a.x, this.p[i + 1].a.y);
i++;
} while (i < (this.p.length - 1));
console.log(i);
path.lineTo(this.p[i].m.x, this.p[i].m.y);
path.lineTo(this.p[i].b.x, this.p[i].b.y);
do {
i--;
path.bezierCurveTo(this.p[i].b.x, this.p[i].b.y,
this.p[i].b.x, this.p[i].b.y,
this.p[i - 1].b.x, this.p[i - 1].b.y);
i--;
} while (i > 1);
path.closePath();
return path;
}
toString() {
return this.d3path().toString();
}
}
var i;
var s = path.node()
ar = new ArowOnPath(s, 0, 10);
svg.append("path")
.attr("d", ar.toString())
.attr("style", "fill:" + getRandomColorRgb());
var l = s.getTotalLength();
var segs = 8;
var thickness = 5;
</script>
</body>