@leafer-in/corner 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/helper.ts ADDED
@@ -0,0 +1,95 @@
1
+ import { BezierHelper } from '@leafer-ui/draw'
2
+
3
+
4
+ // 获取圆角切线距离
5
+ export function getTangentDistance(r: number, x: number, y: number, lastX: number, lastY: number, nextX: number, nextY: number): number {
6
+ // 1. 构造入射向量 U (P -> lastP) 和出射向量 V (P -> nextP)
7
+ const ux = lastX - x, uy = lastY - y
8
+ const vx = nextX - x, vy = nextY - y
9
+
10
+ // 2. 计算向量长度(平方和开方)
11
+ const lenU = Math.sqrt(ux * ux + uy * uy)
12
+ const lenV = Math.sqrt(vx * vx + vy * vy)
13
+
14
+ // 3. 边界防御:若线段长度几乎为0,则不产生圆角
15
+ if (lenU < 0.001 || lenV < 0.001) return 0
16
+
17
+ // 4. 计算向量夹角余弦值 cos(θ) = (U·V) / (|U|*|V|)
18
+ const cosTheta = (ux * vx + uy * vy) / (lenU * lenV)
19
+
20
+ // 5. 精度钳位:防止浮点误差导致 cos 越界 [-1, 1],避开分母为0的情况
21
+ const safeCos = Math.max(-0.99999, Math.min(0.99999, cosTheta))
22
+
23
+ // 6. 核心几何推导:d = r * sqrt((1 - cosθ) / (1 + cosθ))
24
+ let d = r * Math.sqrt((1 - safeCos) / (1 + safeCos))
25
+
26
+ // 7. 【关键性能与安全约束】:最大距离不得超过相邻边长的一半
27
+ // 这样可以确保相邻顶点的圆角不会发生几何重叠或路径自交。
28
+ const maxD = Math.min(lenU / 2, lenV / 2)
29
+ return Math.min(d, maxD)
30
+ }
31
+
32
+ // 精准定位贝塞尔曲线参数 t
33
+ export function getCorrectT(d: number, x: number, y: number, x1: number, y1: number, x2: number, y2: number, toX: number, toY: number): number {
34
+ if (d <= 0) return 0
35
+
36
+ // 1. 预估总弦长(起点到终点的直线距离)
37
+ const chordLen = Math.sqrt((toX - x) ** 2 + (toY - y) ** 2)
38
+
39
+ // 2. 边界保护:若要求的距离超过弦长,为了路径安全,强制限制在曲线 50% 处
40
+ if (d >= chordLen) return 0.5
41
+
42
+ // 3. 初始猜测值估算:利用 t=0 处的瞬时速度矢量 v(0) = 3*(P1 - P0)
43
+ const vx0 = 3 * (x1 - x)
44
+ const vy0 = 3 * (y1 - y)
45
+ let v0mag = Math.sqrt(vx0 * vx0 + vy0 * vy0)
46
+
47
+ // 【优化点】:若起点与控制点重合(初速度为0),降级使用弦长线性比例,防止 NaN
48
+ if (v0mag < 1e-6) v0mag = chordLen || 1
49
+
50
+ let t = Math.min(0.5, d / v0mag)
51
+ const tempP = { x: 0, y: 0 }
52
+
53
+ // 4. 牛顿迭代 (Newton-Raphson) 循环
54
+ for (let i = 0; i < 5; i++) {
55
+ // 获取当前参数 t 对应的坐标点
56
+ BezierHelper.getPointAndSet(t, x, y, x1, y1, x2, y2, toX, toY, tempP)
57
+
58
+ const dx = tempP.x - x
59
+ const dy = tempP.y - y
60
+ const currentDist = Math.sqrt(dx * dx + dy * dy)
61
+
62
+ // 数值稳定性检查:若当前点过于靠近起点,尝试向外探测
63
+ if (currentDist < 1e-6) {
64
+ t = t * 1.5
65
+ continue
66
+ }
67
+
68
+ // 计算当前 t 处的导数向量 (x', y')
69
+ const vxt = BezierHelper.getDerivative(t, x, x1, x2, toX)
70
+ const vyt = BezierHelper.getDerivative(t, y, y1, y2, toY)
71
+
72
+ // 计算目标函数的导数 f'(t) = (dx*x' + dy*y') / currentDist
73
+ const f_prime = (dx * vxt + dy * vyt) / currentDist
74
+
75
+ // 如果导数过小,说明进入平台期或计算异常,停止迭代
76
+ if (Math.abs(f_prime) < 1e-6) break
77
+
78
+ // 牛顿法公式:t_next = t - f(t) / f'(t)
79
+ const deltaT = (currentDist - d) / f_prime
80
+ const nextT = t - deltaT
81
+
82
+ // 【收敛域保护】:强制限制在 [0, 0.6] 范围内寻找最优解,防止迭代跳出贝塞尔曲线有效区间
83
+ const safeNextT = Math.max(0, Math.min(0.6, nextT))
84
+
85
+ // 精度满足 1e-5 (像素级精度) 提前退出
86
+ if (Math.abs(safeNextT - t) < 1e-5) {
87
+ t = safeNextT
88
+ break
89
+ }
90
+ t = safeNextT
91
+ }
92
+
93
+ // 最终返回结果,上限截断在 0.5 处,确保圆弧不会占据整条曲线导致形状崩坏
94
+ return Math.max(0, Math.min(0.5, t))
95
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Plugin } from '@leafer-ui/draw'
2
+ import './PathCorner'
3
+
4
+
5
+ Plugin.add('corner')
@@ -0,0 +1,2 @@
1
+
2
+ export { };