fabric-texture
Version:
This JavaScript library enables fast and efficient image distortion transformations using Canvas 2D.
73 lines (47 loc) • 1.35 kB
Markdown
[**fabric-texture**](../../../../README.md)
***
> **calcRelativeCoord**(`p0`, `p1`, `value`): `Coord`
在直线上计算特定距离的点坐标
在由两点确定的直线上,从起点出发计算特定距离处的点坐标。
该函数处理了以下情况:
1. 两点重合的特殊情况
2. 垂直于坐标轴的直线
3. 水平于坐标轴的直线
4. 一般斜线情况
`Coord`
起点坐标
`Coord`
方向参考点坐标
`number`
目标距离
- 正值:向 p1 方向移动
- 负值:向 p1 相反方向移动
- 零值:返回起点坐标
`Coord`
计算得到的新坐标点
```typescript
// 在水平线上移动
const start = { x: 0, y: 0 };
const direction = { x: 10, y: 0 };
// 向右移动5个单位
const point1 = calcRelativeCoord(start, direction, 5);
console.log(point1); // { x: 5, y: 0 }
// 向左移动3个单位
const point2 = calcRelativeCoord(start, direction, -3);
console.log(point2); // { x: -3, y: 0 }
// 在斜线上移动
const diagonal = { x: 3, y: 4 };
const point3 = calcRelativeCoord(start, diagonal, 5);
// 返回在斜线方向上距离为5的点
```
- 如果两点重合,返回起点坐标
- 支持任意方向的直线
- 计算结果精度受 JavaScript 浮点数精度限制