How to Animate SVG Strokes

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.ค. 2024
  • SVGs are light-weight code drawings, which means you can animate their different paths or segments. In this short video, I’ll show you how to animate SVG strokes with CSS Keyframes and how to find and update the stroke-dasharray and stroke-dashoffset with JavaScript.
    ---------------------------------------
    🔗 Key Links 🔗
    - Codepen: codepen.io/Coding-in-Public/p...
    ---------------------------------------
    🔗 Other Links 🔗
    - Advent of JS: www.adventofjs.com/
    - Advent of CSS: www.adventofcss.com/
    - My go at the Advent of JS Day 1 challenge: codepen.io/Coding-in-Public/p... (needs some work, but it’s functional and probably 90% the way there with code!)
    - SVGator (web tool to create animated SVGs): www.svgator.com/
    ---------------------------------------
    ⏲️ Timestamps ⏲️
    0:00 Introduction
    1:09 Animating an SVG stroke with CSS
    4:30 Updating SVG stroke animation with JavaScript
    11:17 Drawing an SVG arrow with CSS
    ---------------------------------------
    🌐 Connect With Me 🌐
    - Website: www.codinginpublic.dev
    - Blog: www.chrispennington.blog
    - Twitter: / cpenned
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 22

  • @ryder2356
    @ryder2356 2 หลายเดือนก่อน

    Just in case it helps anyone else:
    In some cases you can use the pathLength attribute on any element in order to use less abstract numbers for your path animations.
    E.g.
    And then you can think of your CSS animation properties in terms of 0-100٪
    Alternatively, you could use 360 for circles in order to animate using ° degrees

  • @christianmottaz7366
    @christianmottaz7366 10 หลายเดือนก่อน

    So clear, thanks lot 😊

    • @CodinginPublic
      @CodinginPublic  10 หลายเดือนก่อน

      You're welcome 😊

  • @_vikramhegde_
    @_vikramhegde_ 2 ปีที่แล้ว

    So happy to see this video, I'm participating in the same challenge and was exactly stuck at animating the svg. Thanks for the tut

    • @_vikramhegde_
      @_vikramhegde_ 2 ปีที่แล้ว

      This might be a dumb question but I'm gonna ask it anyway. What would happen if set the animation duration(in this case animation-duration: 10s) of stroke dash offset as much the time we want it to animate, wouldn't that cut down some JS?

    • @CodinginPublic
      @CodinginPublic  2 ปีที่แล้ว +1

      Not a dumb question. Yes! That would work; but it would not allow you to make it look like ticking seconds. It would just move in one motion around in 10 seconds. So just up to you what you prefer.

    • @_vikramhegde_
      @_vikramhegde_ 2 ปีที่แล้ว

      @@CodinginPublic ah, yes that is true. Ticking part of the animation looks much cooler

  • @lycan2494
    @lycan2494 2 ปีที่แล้ว

    THANK YOU FOR THIS VIDEO!

  • @cooltech5441
    @cooltech5441 2 ปีที่แล้ว

    Really love this video

    • @CodinginPublic
      @CodinginPublic  2 ปีที่แล้ว

      Great! Thanks for saying something!

  • @ArquitetoLocatelli
    @ArquitetoLocatelli 5 หลายเดือนก่อน

    If someone is having problems in Safari: at the moment Safari doesn't accept negative values for stroke-dashoffset.
    Thanks for the nice tutorial!

    • @CodinginPublic
      @CodinginPublic  4 หลายเดือนก่อน

      Ah, yes. I believe that's still the case. I haven't ever found a solution for this. I'm assuming there are some libraries or something that can work cross-browser, but negative stroke-dashoffset do not work properly in Safari. If I remember correctly, they claim to be implementing the speck correctly, but it's still frustrating to have different experiences.

  • @collinsk8754
    @collinsk8754 8 หลายเดือนก่อน

    Great tutorial!

    • @CodinginPublic
      @CodinginPublic  8 หลายเดือนก่อน +1

      Gal you enjoyed it!

  • @jpdeseixas
    @jpdeseixas ปีที่แล้ว

    Great video!

  • @user-eo4pv7ir3m
    @user-eo4pv7ir3m 6 หลายเดือนก่อน

    Thanks you're awesome not even the ia can tell me how this exactly work in a way that I could understand god bless you man

    • @CodinginPublic
      @CodinginPublic  6 หลายเดือนก่อน

      💪 take that, Artificial Intelligence! lol

  • @sovereignlivingsoul
    @sovereignlivingsoul 4 หลายเดือนก่อน

    i'm just getting into working with svg's making myself some logos, i am using the circle a lot, and was earlier thinking about how to cut the top off without using line or bezier curve, trying to animate a rotation is hard enough, this will help, thanks

  • @federicoraulmaciasaparicio1339
    @federicoraulmaciasaparicio1339 2 ปีที่แล้ว

    Great video. I didn't know the $0 trick to get the total length of the circle.

    • @CodinginPublic
      @CodinginPublic  2 ปีที่แล้ว +1

      Thanks! Yeah! It’s a useful little trick for interacting with things in the DOM from the browser console.