TweenMax+Rx实现鲜花签名动画效果代码

代码语言:html

所属分类:动画

代码描述:TweenMax+Rx实现鲜花签名动画效果代码

代码标签: TweenMax Rx 鲜花 签名 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <style>
        html, body {
      width: 100%;
      height: 100%;
      overflow: hidden;
      margin: 0;
      padding: 0;
      user-select: none;
      color: #111;
      font-family: serif;
    }
    
    body {
      background-color: #f0f0f0;
      cursor: pointer;
      background-size: cover;
      background-position: center center;
      background-image: url(//repo.bfw.wiki/bfwrepo/image/636dea74def17.png);
    }
    
    #app {
      width: 100%;
      height: 100%;
    }
    
    #stage {
      backface-visibility: visible;
    }
    
    #download-button {
      position: absolute;
      bottom: 0;
      left: 0;
      padding: 10px;
      display: inline-block;
      margin: 10px;
      background-color: #f0f0f0;
      color: #303030;
      border-radius: 4px;
      cursor: pointer;
      border-bottom: solid 3px #f0f0f0;
    }
    #download-button:hover {
      border-bottom: solid 3px #d7d7d7;
    }
    </style>

</head>

<body>
    <!-- partial:index.partial.html -->
    <div id="app">
        <svg id="stage" width="200" height="200" xmlns="http://www.w3.org/2000/svg">
		<g id="branchGroup"></g>
		<g id="thornGroup"></g>
		<g id="leafGroup"></g>
		<g id="flowerGroup"></g>
	</svg>
    </div>

    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/TweenMax.min.js"></script>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/Rx.5.0.1.js"></script>
    <script>
        "use strict";
// Tidier code with webpack and better Typescript in Github
// https://github.com/ste-vg/plant-drawer
console.clear();
var BranchState;
(function (BranchState) {
    BranchState[BranchState["ready"] = 0] = "ready";
    BranchState[BranchState["animating"] = 1] = "animating";
    BranchState[BranchState["ended"] = 2] = "ended";
})(BranchState || (BranchState = {}));
class Branch {
    constructor(stage, settings, grid, placeBehind = null, setPath = null) {
        this.branches = [];
        this.state = BranchState.ready;
        this.branchOut = new Rx.Subject();
        this.thornOut = new Rx.Subject();
        this.flowerOut = new Rx.Subject();
        this.leafOut = new Rx.Subject();
        this.grid = 50; //grid;
        this.stage = stage;
        this.placeBehind = placeBehind;
        settings.width = 2;
        settings.opacity = 1;
        this.state = BranchState.animating;
        let path = setPath ? setPath : this.createLine(settings);
        let branchCount = 2;
        for (let i = 0; i < branchCount; i++) {
            this.createSqwig(i, branchCount, path, JSON.parse(JSON.stringify(settings)));
        }
    }
    createSqwig(index, total, path, settings) {
        let branch = document.createElementNS("http://www.w3.org/2000/svg", 'path');
        branch.setAttribute('d', path);
        branch.style.fill = 'none';
        branch.style.stroke = this.getColor(index);
        branch.style.strokeLinecap = "round";
        settings.length = branch.getTotalLength();
        settings.progress = settings.length;
        branch.style.strokeDasharray = `${settings.length}, ${settings.length}`;
        branch.style.strokeDashoffset = `${settings.length}`;
        this.branches.push({ path: branch, settings: settings });
        if (!this.placeBehind)
            this.stage.appendChild(branch);
        else
            this.stage.insertBefore(branch, this.placeBehind.branches[0].path);
        let widthTarget = settings.sections * 0.8;
        TweenMax.set(branch, { x: -index * 3, y: -index * 3 });
        TweenMax.to(settings, settings.sections * 0.2, {
            progress: 0,
            width: widthTarget,
            ease: Power1.easeOut,
            delay: index * (settings.sections * 0.001),
            onUpdate: () => {
                if (index == 0 && settings.sections > 4) {
                    let choice = Math.random();
                    let length = settings.length - settings.progress;
                    let pos = branch.getPointAtLength(length);
                    let sec = Math.ceil((settings.progress / settings.length) * settings.sections) - 2;
                    if (sec < 4)
                        sec = 4;
                    let out = {
                        position: { x: pos.x, y: pos.y },
                        width: widthTarget,
                        sections: sec
                    };
                    if (choice < 0.02)
                        this.branchOut.next(out);
                    else if (choice < 0.1)
                        this.thornOut.next(out);
                    else if (choice < 0.2)
                        this.flowerOut.next(out);
                    else if (choice < 0.4)
                        this.leafOut.next(out);
                }
            },
            onComplete: () => {
                if (index = total - 1)
                    this.state = BranchState.ended;
                //branch.remove();
            }
        });
    }
    update() {
        this.branches.map((set) => {
            set.path.style.strokeDashoffset = `${set.settings.progress}`;
            set.path.style.strokeWidth = `${set.settings.width}px`;
            //set.path.style.opacity = `${set.settings.opacity}`;
        });
    }
    createLine(settings) {
        let x = settings.x;
        let y = settings.y;
        let dx = settings.directionX;
        let dy = settings.directionY;
        let path = [
            'M',
            '' + x,
            '' + y
        ];
        let steps = settings.sections;
        let s.........完整代码请登录后点击上方下载按钮下载查看

网友评论0