svgdotjs / svg.js

The lightweight library for manipulating and animating SVG
https://svgjs.dev
Other
10.95k stars 1.07k forks source link

(documentation) animating shapes along a path example needs rewrite #1303

Closed opheliagame closed 11 months ago

opheliagame commented 1 year ago

https://svgjs.dev/docs/3.0/tutorials/ The 'Animating shapes along a path' on this page doesn't work anymore I think. I would be willing to write the current version of this if required. lmk :) referencing #1134 as well

opheliagame commented 1 year ago

I think this example doesnt work anymore.

var canvas = SVG('drawing').size('100%', '100%').viewbox(0,0,800,1000)
  , rect = canvas.rect(100, 100)
  , path = canvas.path("m 357.64532,453.84097 c 17.62007,8.02216 -2.12058,27.70935 -13.33334,29.28571 -30.3859,4.27185 -48.34602,-29.97426 -45.23807,-55.9524 5.5594,-46.46879 56.1311,-70.59787 98.57145,-61.19043 62.28294,13.8058 93.32728,82.57702 77.1428,141.19051 C 453.21679,585.29693 365.67122,623.42358 290.97859,600.26951 196.98554,571.13248 151.71003,464.56996 181.93108,373.84089 218.53281,263.95583 344.23687,211.49702 450.97875,248.84102 576.77037,292.84963 636.43303,437.76771 591.93099,560.50775 540.55162,702.21597 376.3736,769.09583 237.6452,717.41234 80.01319,658.68628 5.9069261,475.21736 64.788247,320.50751 130.84419,146.94643 333.62587,65.607117 504.31214,131.69819 693.80625,205.0718 782.38357,427.18225 709.07382,613.84113")
  , length = path.length()

path.fill('none').stroke({width:1, color: '#ccc'})

rect.animate(8000, '<>').during(function(pos, morph, eased){
    var p = path.pointAt(eased * length)
    rect.center(p.x, p.y)
}).loop(true, true)

the following would work

var canvas = SVG('drawing').size('100%', '100%').viewbox(0,0,800,1000)
  , rect = canvas.rect(100, 100)
  , path = canvas.path("m 357.64532,453.84097 c 17.62007,8.02216 -2.12058,27.70935 -13.33334,29.28571 -30.3859,4.27185 -48.34602,-29.97426 -45.23807,-55.9524 5.5594,-46.46879 56.1311,-70.59787 98.57145,-61.19043 62.28294,13.8058 93.32728,82.57702 77.1428,141.19051 C 453.21679,585.29693 365.67122,623.42358 290.97859,600.26951 196.98554,571.13248 151.71003,464.56996 181.93108,373.84089 218.53281,263.95583 344.23687,211.49702 450.97875,248.84102 576.77037,292.84963 636.43303,437.76771 591.93099,560.50775 540.55162,702.21597 376.3736,769.09583 237.6452,717.41234 80.01319,658.68628 5.9069261,475.21736 64.788247,320.50751 130.84419,146.94643 333.62587,65.607117 504.31214,131.69819 693.80625,205.0718 782.38357,427.18225 709.07382,613.84113")
  , length = path.length()

path.fill('none').stroke({width:1, color: '#ccc'})

rect.animate(8000).during(function(t){
    var p = path.pointAt(t * length)
    rect.center(p.x, p.y)
}).loop(true, true)
Fuzzyma commented 1 year ago

nice! feel free to create a PR to update the docs with a new example :)

opheliagame commented 1 year ago

@Fuzzyma just checked the documentation repository and there are jsfiddles embedded so they will have to be edited by the owners

Fuzzyma commented 1 year ago

You can just fork them and save a new link

opheliagame commented 1 year ago

@Fuzzyma created a new pr here!