Bezier Curves and Arcs in jQuery

September 22, 2009

The animation engine in jQuery is focussed on single dimensional animation – hence it’s difficult to animate two variables along a path.

jQuery.path provides a method of multidimensional animation, and in particular provides a method for animating along bezier curves and arcs.

Bezier

Example: animate along a bezier path. See demo

var bezier_params = {
    start: {
      x: 185,
      y: 185,
      angle: 10
    },
    end: {
      x:540,
      y:110,
      angle: -10,
      length: 0.25
    }
  }

$("my_elem").animate({path : new $.path.bezier(bezier_params)})

Bezier curves are made form a start point, an end point each with a control point

  • start is starting point
  • end is the final point
  • x,y indicate the coordinates at that point. Required
  • angle is the angle of the control point from the line joining the start and end. Optional, default is 0
  • length is the distance from the point to it’s control point as a ratio of the distance from start to end. Optional, default is 1/3

Arc

Exampe: animate along an arc. See demo

var arc_params = {
    center: [285,185],
        radius: 100,
        start: 30,
        end: 200,
        dir: -1
  }

$("my_elem").animate({path : new $.path.arc(arc_params)})
  • center is the coords of the centre of an imaginary circle that contains the start and end point
  • radius is the radius of this circle
  • start is the angle of the start point. 0 is “North”, measured clockwise
  • end is the angle of the start point. 0 is “North”, measured clockwise
  • dir is the direction to move in. Only required if not obvious from start and end (e.g. if start is 100, end is 30, but you want to animate clockwise)

Other Paths

It is trivial to create other paths, or even animate other parameters. E.g:

var SineWave = function() {
  this.css = function(p) {
    var s = Math.sin(p*20)
    var x = 500 - p * 300
    var y = s * 50 + 150
    var o = ((s+2)/4+0.1)
    return {top: y + "px", left: x + "px", opacity: o}
  }
};

$("my_elem").animate({path : new SineWave})

Links


26 Responses to “Bezier Curves and Arcs in jQuery”


  1. Cool animations.

    Why do I think we’re going to see more and more visual games in the browser?


  2. [...] jQuery Path können Objekte einer HTML-Seite relativ leicht auf Pfaden animiert werden. Dies Erweitert die [...]

  3. Mohammad Hussein Says:

    Awesome.
    Will jQuery replace Flash in everything? :)

  4. Nikola Says:

    Beautiful!


  5. Very Useful Great Effect

  6. Jai Says:

    Interesting approach to using jQuery for effects which I so far thought it can only be achieved using Flash.


  7. [...] Bezier Curves and Arcs in jQuery [...]

  8. Daveed Says:

    Great work on this.
    Can I use these effects on none SVG elements?
    I’ve spent quite a bit of time not able to make it work with anything but SVG elements.

  9. foxparker Says:

    There’s nothing particular to SVG that it’s using there.

    The circles are just div’s with a large border-radius.

  10. Russ Says:

    I can’t see to get this to work…. I’m trying to just get an image to move across the screen and it’s not budging. Any one have a simple demo of this running?

  11. leggo-my-eggo Says:

    Would there be a way, using your (amazing) plugin, to get the object to only travel *part* of the way along the bezier curve? So, for instance, I’d like to set a specific curve, and then tell the script to move the object 47% of the distance along that curve. Make sense?

    • foxparker Says:

      Should be. e.g. with the sine wave, just pass in a parameter:

      var SineWave = function(percentage) {
        this.css = function(p) {
           p *= percentage;
          var s = Math.sin(p*20)
          var x = 500 - p * 300
          var y = s * 50 + 150
          var o = ((s+2)/4+0.1)
          return {top: y + "px", left: x + "px", opacity: o}
        }
      };

      I think that should work

  12. leggo-my-eggo Says:

    Thank you, that works perfectly.


  13. [...] Ha hecho un ejemplo con los controles básicos de movilidad, es decir movernos hacia delante, atras y saltar con las flechas del teclado, y para entrar en una página o acceder a un link lo hacemos chocando con los bloques interrogantes suspendidos en el aire, tal y como se hace en el videojuego. Para conseguirlo se ha basado en jQuery, el plugin BackgroundPosition, Easing y añadiendo algunas curvas y arcos. [...]


  14. [...] of about a half hour to produce.  While searching for a plugin that could rotate the object I also found a cool bezier, curve, arc plugin and really it is kind of fun to play around with jQuery.  This [...]

  15. Motyar Says:

    Thanx a lot. Its that i was searching for my animation game http://motyar.com/angeldreams

    Thanx again, you helped me a lot.

  16. Mark Reale Says:

    Hello -

    Super awesome plugin -

    I have been playing with this and trying to get some sort of bezier animation working on my scrollbars, as though the browser window were travelling on a bezier path.

    Any ideas as to how I can tweak this to get it done?

    I have been playing with scrollTop and scrollLeft but haven’t been able to have success -

    Any ideas?

    Thanks -

  17. gr33d Says:

    This is fantastic! I’m trying to animate a lot of bubbles, and I’m running into some positioning oddities. Each successive that I add inside the animation boundary moves the “x” position of the start and end properties of the Bezier curve. How can I always refer to the upper left as (0,0) or handle this adjustment more elegantly than hard coding adjusted values?

    Thanks in advance!

  18. gr33d Says:

    My above post didn’t include the HTML tags I had hoped.

    edit: Each successive “img” tag that I add…

  19. foxparker Says:

    you’ll have to recalculate at the beginning and ending of each of the parts. Or you could write a new path functions (return the required position for p = 0 to 1)

  20. gr33d Says:

    How can I recalculate? Currently, I call myanimation(); which then calls each individual bubbleX(); function. Each of those loops indefinitely. How can each bubble function be aware of other elements? Or do I need a different approach?

    I don’t understand your second suggestion at all, can you elaborate?

    Thank you!

  21. Moritz Graf Says:

    Heyo ;)

    First of all: Great Plugin it simplified the whole thing!

    Now I have a question. Is there a way to pause the animation on hover and play it from it’s recent position on hoverout?

    Thanks. Greetz

    • foxparker Says:

      probably — you’ll have to check how to do that in jQuery in general — same method should work ok .


Leave a Reply