• SVG—初识3


    SVG动画

    • Transform(scale、rotate、translate 、skew)
    • 路径动画 (path)
    • 描边动画(stroke)
    • 形状变化(Morphing)
    • 蒙版动画(Mask)
    • 颜色控制(color / background-color / opacity)

    使用GSAP制作动画

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <svg width="960px" height="900px" viewBox="0 0 960 900">
        <g id="shape-page">
            <circle r="80" cx="120" cy="120" fill="#60CCFD" id="circle"/>
        g>
    svg>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.1/gsap.min.js">script>
    <script>
        function moveTo(){
            var gsap1=gsap.to('#circle', {
                x: 320,
                y: 0,
                duration: 2
            })
    
        }
        moveTo()
    script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    请添加图片描述

    缓动动画

    GSAP内置缓动动画,默认ease的值为power1.out,让 circle 像球一样弹起运动。只需设定属性 ease: 'bounce'

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <svg width="960px" height="900px" viewBox="0 0 960 900">
        <g id="shape-page">
            <circle r="80" cx="120" cy="120" fill="#60CCFD" id="circle"/>
        g>
    svg>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.1/gsap.min.js">script>
    <script>
        function moveTo(){
            var gsap1=gsap.to('#circle', {
                x: 320,
                y: 0,
                duration: 2,
                ease: 'bounce'
            })
    
        }
        moveTo()
    script>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    请添加图片描述

    时间轴线

    gsap.timeline()
    拥有操纵时间轴线的能力,会让动画有序进行

    <svg width="960px" height="900px" viewBox="0 0 960 900">
    
        <g id="shape2-page">
            <circle id="circle1" r="40" cx="40" cy="100" fill="#60CCFD" />
            <circle id="circle2" r="40" cx="150" cy="100" fill="pink" />
            <circle id="circle3" r="40" cx="260" cy="100" fill="greenyellow" />
            <circle id="circle4" r="40" cx="370" cy="100" fill="red" />
        g>
    svg>
    <script>
    function move2(){
            var gsap1=gsap.timeline({
                repeat: -1,
                repeatDelay: 0.3,
                defaults:{
                    duration: 0.66,
                }
    
            })
            gsap1
                .to("#circle1",{
                x:500
            })
                .to("#circle2",{
                x:500
            })
                .to("#circle3",{
                x:500
            })
                .to("#circle4",{
                x:500
            })
        }
        move2()
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    请添加图片描述
    下面例子时间轴动画时长为 duration: 0.66 ,下面让第 2、3、4 个 circle 使用相对时间,减去动画时长,那么制造了多个 circle 一同运动的效果。

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <svg width="960px" height="500px" viewBox="0 0 960 500">
        <g id="shape2-page">
            <circle id="circle1" r="40" cx="40" cy="100" fill="#60CCFD" />
            <circle id="circle2" r="40" cx="150" cy="100" fill="pink" />
            <circle id="circle3" r="40" cx="260" cy="100" fill="greenyellow" />
            <circle id="circle4" r="40" cx="370" cy="100" fill="red" />
        g>
    svg>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.1/gsap.min.js">script>
    <script>
        function moveTo(){
            var tl=gsap.timeline({
                repeat: -1,
                repeatDelay: 1,
                defaults:{
                    duration: 0.66
                }
            })
            tl.to("#circle1",{
                x:500
            },"-=0.66")   //减去相对时间
            .to("#circle2",{
                x:500
            },"-=0.66")
            .to("#circle3",{
                x:500
            },"-=0.66")
                .to("#circle4",{
                x:500
            },"-=0.66")
        }
        moveTo()
    script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    请添加图片描述
    升级版:

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <svg width="1500px" height="900px" viewBox="0 0 1500 900" >
        <g id="shape-page" width="1000px">
            <circle id="circle1" cx="50" cy="200" r="40" style="fill: orange" />
            <circle id="circle2" cx="150" cy="200" r="40" style="fill: orangered" />
            <circle id="circle3" cx="250" cy="200" r="40" style="fill: orange" />
            <circle id="circle4" cx="350" cy="200" r="40" style="fill: orangered" />
            <circle id="circle5" cx="450" cy="200" r="40" style="fill: orange" />
            <circle id="circle6" cx="550" cy="200" r="40" style="fill: orangered" />
            <circle id="circle7" cx="650" cy="200" r="40" style="fill: orange" />
            <circle id="circle8" cx="750" cy="200" r="40" style="fill: orangered" />
            <circle id="circle9" cx="850" cy="200" r="40" style="fill: orange" />
            <circle id="circle10" cx="950" cy="200" r="40" style="fill: orangered" />
        g>
    svg>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.1/gsap.min.js">script>
    <script>
        function moveTo(){
            var tl=gsap.timeline({
                repeat: -1,
                repeatDelay: 0.05,
                defaults:{
                    duration: 0.2,
                }
            })
            tl.to("#circle1",{
                y:-100,
            }).to("#circle2",{
                y:-100
            }).to("#circle3",{
                y:-100
            }).to("#circle4",{
                y:-100
            }).to("#circle5",{
                y:-100
            }).to("#circle6",{
                y:-100
            }).to("#circle7",{
                y:-100
            }).to("#circle8",{
                y:-100
            }).to("#circle9",{
                y:-100
            }).to("#circle10",{
                y:100
            }).to("#circle9",{
                y:100
            }).to("#circle8",{
                y:100
            }).to("#circle7",{
                y:100
            }).to("#circle6",{
                y:100
            }).to("#circle5",{
                y:100
            }).to("#circle4",{
                y:100
            }).to("#circle3",{
                y:100
            }).to("#circle2",{
                y:100
            }).to("#circle1",{
                y:100
            }).to("#circle1",{
                y:100
            },"-=0.2").to("#circle2",{
                y:100
            },"-=0.2").to("#circle3",{
                y:100
            },"-=0.2").to("#circle4",{
                y:100
            },"-=0.2").to("#circle5",{
                y:100
            },"-=0.2").to("#circle6",{
                y:100
            },"-=0.2").to("#circle7",{
                y:100
            },"-=0.2").to("#circle8",{
                y:100
            },"-=0.2").to("#circle9",{
                y:100
            },"-=0.2").to("#circle10",{
                y:100
            },"-=0.05").to("#circle1",{
                y:-100
            }).to("#circle3",{
                y:-100
            }).to("#circle5",{
                y:-100
            }).to("#circle7",{
                y:-100
            }).to("#circle9",{
                y:-100
            })
        }
        moveTo()
    script>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    请添加图片描述

    时间交错

    gsap.to 提供了一个用于控制时间交错的属性:stagger ,这个属性同样是对动画时间的控制,比如让多个 circle 同时以 0.2s 的时间错开进行Y轴位移运动

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <svg width="960px" height="500px" viewBox="0 0 960 500">
        <g id="shape2-page">
            <circle id="circle" r="40" cx="50" cy="200" fill="#60CCFD" />
            <circle id="circle" r="40" cx="150" cy="200" fill="#FFB966" />
            <circle id="circle" r="40" cx="250" cy="200" fill="#92CF94" />
            <circle id="circle" r="40" cx="350" cy="200" fill="#F089AF" />
            <circle id="circle" r="40" cx="450" cy="200" fill="#92CFCF" />
            <circle id="circle" r="40" cx="550" cy="200" fill="orange" />
            <circle id="circle" r="40" cx="650" cy="200" fill="#F089FF" />
            <circle id="circle" r="40" cx="750" cy="200" fill="#F025AF" />
            <circle id="circle" r="40" cx="850" cy="200" fill="#0089AF" />
        g>
    svg>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.1/gsap.min.js">script>
    <script>
        var tl=gsap.to("circle",{
            repeat: -1,
            delay: 0.5,
            y:function(i,elem,boxes){
                return i%2==0?-50:50;
            },
            stagger: 0.2,
            duration:1,
            yoyo: true
        })
    script>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    请添加图片描述

  • 相关阅读:
    JavaScript:日期对象
    java----内部类(四种内部类详解)
    (web前端网页制作课作业)使用HTML+CSS制作非物质文化遗产专题网页设计与实现
    小程序学习
    Springboot 集成 MongoDB
    python神经网络框架有哪些,python调用神经网络模型
    2023年了,还不知道你的事务为什么会失效吗?
    @Builder使用遇到的坑
    2500家门店倒闭:实体店败给电商已成定局?
    8.tomcat优化
  • 原文地址:https://blog.csdn.net/qq_34306228/article/details/126927599