在 OnPathBehavior 类中,calculate 函数用于计算实体在路径上的位置。
calculate(entity, path, speed);
entity: 在路径上移动的实体,类型为 HTMLElement。path: 移动路径,类型为 SVGPathElement。speed: 实体在路径上移动的速度,类型为 number,单位为像素每毫秒。无返回值。
calculate 函数使用 velocityjs 库来计算实体在路径上的位置。该函数将初始位置设置为路径的起点,并按指定速度在路径上移动实体。一旦实体在路径的末尾,calculate 函数会将实体返回到路径的起点,从而在路径上形成闭环运动。
const entity = document.querySelector('.entity');
const path = document.querySelector('.path');
const speed = 2;
OnPathBehavior.calculate(entity, path, speed);
在此示例中,将 entity 设置为类名为 .entity 的 HTML 元素,在形状定义为 .path 的路径上,使用速度为 2 像素/毫秒的速度,通过 OnPathBehavior 类的 calculate 函数进行计算。
该函数在 Yuka 2.0.0 及更高版本中可用。