javascript 如何沿具有贝塞尔曲线的路径为图像设置动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13612942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:16:57  来源:igfitidea点击:

How to animate an image along a path with Bezier curves

javascriptinternet-explorerjquery-animatebezier

提问by Bruno

My goal:

我的目标:

  • Move/animate an image along a path like the drawing below (Could be connecting bezier curves).
  • Must work in IE7+, don't what to build multiple solutions.
  • I can pause/resume the moving image.
  • The image will keep moving along the path (repeat).
  • 沿着如下图所示的路径移动/动画图像(可能是连接贝塞尔曲线)。
  • 必须在IE7+下工作,不要什么构建多个解决方案。
  • 我可以暂停/恢复运动图像。
  • 图像将继续沿路径移动(重复)。

the path

路径

What I have considered

我所考虑的

  • CANVAS: not supported in IE7+8, haven't tested explorercanvas yet! Foresee some z-index issues.
  • SVG, not supported in IE7+8.
  • jQuery.path, a plugin that extends the jQuery animate function. Can't figure out the resume part and I want to use CSS transforms when supported.
  • CANVAS:IE7+8不支持,还没有测试过explorercanvas!预见一些 z-index 问题。
  • SVG,IE7+8 不支持。
  • jQuery.path,一个扩展jQuery animate 功能的插件。无法弄清楚简历部分,我想在支持时使用 CSS 转换。

My plan

我的计划

  • Animate the image with CSS 3D transform, CSS 2d transform or jQuery.animate (what supported) and requestAnimationFrame.
  • Calculate all the coordinates and simple move the image pixel by pixel.
  • 使用 CSS 3D 变换、CSS 2d 变换或 jQuery.animate(支持的内容)和 requestAnimationFrame 为图像设置动画。
  • 计算所有坐标并简单地逐像素移动图像。

My question

我的问题

  • Does my plan sound like madness? Better suggestions?
  • Do you foresee some performance issues? I might end up with 5K or 10K coordinates.
  • Do you know a smart way, a program, a function or anything similar to calculate all the coordinates?
  • 我的计划听起来很疯狂吗?更好的建议?
  • 您是否预见到一些性能问题?我最终可能会得到 5K 或 10K 坐标。
  • 你知道计算所有坐标的聪明方法、程序、函数或类似的东西吗?

采纳答案by xavier.seignard

I would recommend you to use GSAP : http://www.greensock.com/get-started-js/

我建议您使用 GSAP:http: //www.greensock.com/get-started-js/

With that you can handle timelines, and here is a bezier plugin : http://api.greensock.com/js/com/greensock/plugins/BezierPlugin.html

有了它你可以处理时间线,这里有一个贝塞尔插件:http: //api.greensock.com/js/com/greensock/plugins/BezierPlugin.html

regards

问候

回答by vsync

There's a tiny script (SVG based), just for animation which isn't in straight lines,
called pathAnimator(2kb), It's very very small and very efficient.
No jQuery required.

有一个很小的脚本(基于 SVG),仅用于非直线的动画,
称为pathAnimator(2kb),它非常非常小且非常高效。
不需要jQuery。

For example:

例如:

var path = "M150 0 L75 200 L225 200 Z";         // an SVG path
    pathAnimator = new PathAnimator( path ),    // initiate a new pathAnimator object
    speed = 6,              // seconds that will take going through the whole path
    reverse = false,        // go back or forward along the path
    startOffset = 0,        // between 0% to 100%
    easing = function(t){ t*(2-t) };    // optional easing function


pathAnimator.start( speed, step, reverse, startOffset, finish, easing);

function step( point, angle ){
    // do something every "frame" with: point.x, point.y & angle
}

function finish(){
    // do something when animation is done
}


(can even get more efficient using fastdom)

(甚至可以使用fastdom提高效率)