standalone animated svg

Discuss SVG code, accessible via the XML Editor.
mastupristi
Posts: 6
Joined: Wed Sep 02, 2015 10:52 pm

standalone animated svg

Postby mastupristi » Wed Aug 17, 2016 11:58 pm

I can write a script inside into the svg element and the embed it into an html, for example:

Code: Select all

<html>
<body>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="92.364212mm"
   height="49.764038mm"
   viewBox="0 0 327.27477 176.32927"
   id="svg2"
   version="1.1"
   inkscape:version="0.91 r13725"
   sodipodi:docname="drawing1.svg"
   onload="startAnimation(evt)">
  <defs
     id="defs4" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.98994949"
     inkscape:cx="66.995504"
     inkscape:cy="-99.774488"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1920"
     inkscape:window-height="984"
     inkscape:window-x="0"
     inkscape:window-y="25"
     inkscape:window-maximized="1"
     fit-margin-top="0"
     fit-margin-left="0"
     fit-margin-right="0"
     fit-margin-bottom="0" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(-193.46898,-119.94397)">
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:30, 30;stroke-dashoffset:50;stroke-opacity:1"
       d="M 518.20826,125.04217 C 197.9899,97.768049 195.96959,293.73764 195.96959,293.73764"
       id="path4136"
       inkscape:connector-curvature="0" />
  </g>
<script type="text/ecmascript">
    var timerFunction = null;
    var svgRoot =null;

    function startAnimation(evt) {
        svgRoot = evt.target;
        if(timerFunction == null) {
            timerFunction = setInterval(animate, 20);
        }
    }

    function animate() {
        var curva = svgRoot.getElementById("path4136");
        var x = curva.style['stroke-dashoffset'];
   var len = curva.getTotalLength();
   var p = curva.getPointAtLength(100);
        var newX = 2 + parseInt(x);
        if(newX >= 60) {
            newX = 0;
        }
   curva.style['stroke-dashoffset'] = newX;
    }
</script>
</svg>
</body>
</html>


as you can see the <script> element is a child of the <svg> element.

I wonder if is possible to embed the script into a standalone svg file and then load it from an html as an object or (better) an image.
This because I need to upload animated image into forums or wiki pages, so the html pages are dinamically created and then all animation details (comprise the use of javascript library like jquery or snapsvg) has to be embedded exclusively into the svg file.

best regards
Max

User avatar
Maestral
Posts: 931
Joined: Sat Aug 27, 2011 7:10 am

Re: standalone animated svg

Postby Maestral » Thu Aug 18, 2016 12:06 am

Have you tried?

Some time ago I made a standalone animated svg but am not quite sure if some script or code were added later on. This svg worked and acted like an animated gif.

However, it is possible to add a few lines of script to trigger the animation or other events (mouse over, hover, click..) but you probably already know about that!?
:tool_zoom: <<< click! - but, those with a cheaper tickets should go this way >>> :!:

hulf2012
Posts: 715
Joined: Sat Nov 24, 2012 12:37 pm

Re: standalone animated svg

Postby hulf2012 » Thu Aug 18, 2016 2:55 am

Hello
So you want
- Embed an SVG drawing ,which has embedded a Javascript code, in an HTML page
I think it's possible. It's better to put the javascript code in a cdata block:
http://docstore.mik.ua/orelly/xml/xmlnut/ch02_06.htm

-Now about embeding SVG code inside and "<img>" element... well as far as I know... It works, but in my tests that I've done there are web browsers that doesn't support CSS animations inside IMG elements, so I guess that javascript it's also not supported. I'm talking about Firefox. Of course the worst support for SVG features are shared for Micro Soft web browsers.

More info about SVG:
http://tutorials.jenkov.com/svg/display ... s.html#img

But You know what?... humbly, It's better to learn from the source... Consult here:
https://www.w3.org/TR/SVG/

If you manages to found the solution... please tell us ;)
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

Moini
Posts: 2216
Joined: Mon Oct 05, 2015 10:44 am

Re: standalone animated svg

Postby Moini » Thu Aug 18, 2016 10:56 am

Javascript is not supported in img elements for security reasons. Use <object>,<iframe> or <embed>, or it will probably also work when you use inline svg. Some more info here: http://tavmjong.free.fr/INKSCAPE/MANUAL ... b-Use.html and here http://tavmjong.free.fr/SVG/ANIMATION/

SMIL however works in img elements (but may soon be removed from standard browser support (https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/5o0yiO440LM%5B1-25%5D) , still, there's an (incomplete) polyfill: https://github.com/ericwilligers/svg-animation).

See a nice example of a SMIL animation here: https://inkscape.org/en/~IK/%E2%98%85an ... e-koi-carp (works with current Firefox, don't know about others).
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

hulf2012
Posts: 715
Joined: Sat Nov 24, 2012 12:37 pm

Re: standalone animated svg

Postby hulf2012 » Fri Aug 19, 2016 3:13 am

Moini

Image

Off topic:
By the way, How can I put an animation in the gallery of Inkscape.org, like in you example. But just SVG and CSS embedded

On topic:
In the image shown, which uses SMIL. Those effects of the fish moving its tails is made using "morphing paths" ... (I guess). And the path they follow it's made using a "follow path" effect. Those effect can only be done using SMIL. Or using Javascript, but from my point of view it's more complicated.
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

Moini
Posts: 2216
Joined: Mon Oct 05, 2015 10:44 am

Re: standalone animated svg

Postby Moini » Fri Aug 19, 2016 6:56 am

@hulf2012:

Off topic:
By the way, How can I put an animation in the gallery of Inkscape.org, like in you example. But just SVG and CSS embedded


You can't ;-) (or rather, only admins can do that for official images, which can be inserted in an <embed> tag, see an example here: https://inkscape.org/en/~doctormo/%E2%9 ... roup-photo - it's not possible for 'normal' users and 'normal' images.)

If we'd allow that for normal users, this could - accidentally or intentionally - change the web page's CSS and / or JS, which for security reasons is disallowed.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

mastupristi
Posts: 6
Joined: Wed Sep 02, 2015 10:52 pm

Re: standalone animated svg

Postby mastupristi » Sun Aug 21, 2016 12:56 am

One little step ahead:

html:

Code: Select all

<!DOCTYPE html>
<html>
<body>

<object data="curve.svg">
</object>

</body>
</html>


and curve.svg:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="92.364212mm"
   height="49.764038mm"
   viewBox="0 0 327.27477 176.32927"
   id="svg2"
   version="1.1"
   inkscape:version="0.91 r13725"
   sodipodi:docname="drawing1.svg"
   onload="startAnimation(evt)">
  <defs
     id="defs4" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.98994949"
     inkscape:cx="66.995504"
     inkscape:cy="-99.774488"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1920"
     inkscape:window-height="984"
     inkscape:window-x="0"
     inkscape:window-y="25"
     inkscape:window-maximized="1"
     fit-margin-top="0"
     fit-margin-left="0"
     fit-margin-right="0"
     fit-margin-bottom="0" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(-193.46898,-119.94397)">
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:30, 30;stroke-dashoffset:50;stroke-opacity:1"
       d="M 518.20826,125.04217 C 197.9899,97.768049 195.96959,293.73764 195.96959,293.73764"
       id="path4136"
       inkscape:connector-curvature="0" />
  </g>
<script type="text/ecmascript">
    var timerFunction = null;
    var svgRoot =null;

    function startAnimation(evt) {
        svgRoot = evt.target;
        if(timerFunction == null) {
            timerFunction = setInterval(animate, 20);
        }
    }

    function animate() {
        var curva = svgRoot.getElementById("path4136");
        var x = curva.style['stroke-dashoffset'];
   var len = curva.getTotalLength();
   var p = curva.getPointAtLength(100);
        var newX = 2 + parseInt(x);
        if(newX >= 60) {
            newX = 0;
        }
   curva.style['stroke-dashoffset'] = newX;
    }
</script>
</svg>


Now I have to load javascript library like jquery or snapsvg within svg file (remember that html pages are dinamically created by wiki engine or forums)

best regards

hulf2012
Posts: 715
Joined: Sat Nov 24, 2012 12:37 pm

Re: standalone animated svg

Postby hulf2012 » Mon Aug 22, 2016 7:40 am

Hello

Ok... but don't forget to put between CDATA!...

Anyway, if possible when uploaded to web, l'd wish to see the final results.

If there are any question or comment please share here in the forum

Greetings
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.


Return to “SVG / XML Code”

Who is online

Users browsing this forum: No registered users and 1 guest