我需要在多边形上开发2个动画:
我正试图用这段代码做到这一点:
// onMouseMove
tool.onMouseMove = function (event) {
mousePoint = event.point;
};
// onFrame
view.onFrame = function (event) {
// Animation 1: Automatic circular rotation of each segment
for (var i = 0; i < polygon.segments.length; i++) {
var offsetRotation = new Point(Math.cos(event.time + i * 2), Math.sin(event.time + i * 2)).multiply(10);
polygon.segments[i].point = polygonCached.segments[i].point.add(offsetRotation);
}
// Animation 2: the polygon moves following the mouse movements with transition
var delta = mousePoint.subtract(polygon.position).divide(15);
polygon.position = polygon.position.add(delta);
};
这是完整的代码:https://codepen.io/anon/pen/YMgEEe?editors=0010
使用上面的代码,每个段的自动旋转起作用,多边形根本不跟随鼠标,也没有过渡。相反,注释自动旋转动画它正确跟随鼠标转换。
要检查转换是否有效,我将鼠标光标移动到浏览器窗口之外,然后从另一个点返回。现在,当多边形移动时,您将看不到任何过渡。
哪里我错了?
只需移动polygonCached
。
polygonCached.position = polygonCached.position.add(delta);
https://codepen.io/arthursw/pen/LvKPXo
多边形的缓存版本没有移动,因此每次旋转点时,它们的位置都会重置。