tween.js可生成平滑动画效果的js动画库效果演示

tween.js强大的可生成平滑动画效果的js库

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Tween.js / dynamic to</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style>
            body {
                margin: 0px;
            }
            #target {
                font-size: 13px;
                padding: 0px 32px;
            }
        </style>
        <link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="info" style="position: relative;">
            <h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
            <h2>07 _ dynamic to</h2>
            <p>tweening towards a moving target</p>
        </div>

        <div id="target"></div>

        <script src="../src/Tween.js"></script>
        <script src="js/RequestAnimationFrame.js"></script>
        <script>
            init();
            animate();
            function init() {
                var width = 480;
                var height = 320;
                var target = document.getElementById('target');
                var canvas = document.createElement( 'canvas' );
                canvas.width = width;
                canvas.height = height;
                target.appendChild( canvas );
                var context = canvas.getContext( '2d' );
                var rabbit = { x: width - 50, y: 50 };
                new TWEEN.Tween( rabbit ).to( { x: width - 50, y: height - 50 }, 3000 ).onUpdate( function() {
                    // draw background
                    context.fillStyle = "rgb(240,250,240)";
                    context.fillRect( 0, 0, width, height );
                    // draw rabbit
                    context.fillStyle = "rgb(150,150,150)";
                    context.save();
                    context.translate( this.x, this.y );
                    context.beginPath();
                    context.moveTo( 0, 0 );
                    context.bezierCurveTo( 15, 0, 15, -40, 5, -30 );
                    context.lineTo( 0, 0 );
                    context.bezierCurveTo( -15, 0, -15, -40, -5, -30 );
                    context.lineTo( 0, 0 );
                    context.fill();
                    context.beginPath();
                    context.arc( 0, 0, 15, 0, Math.PI * 2, true );
                    context.fill();
                    context.restore();
                } ).start();
                var fox = { x: 50, y: 50 };
                new TWEEN.Tween( fox ).to( rabbit, 3000 ).onUpdate( function() {
                    // draw fox
                    context.fillStyle = "rgb(200,80,80)";
                    context.save();
                    context.translate( this.x, this.y - 13 );
                    context.scale( 1.2, 1.2 );
                    context.beginPath();
                    context.moveTo( 4, 24 );
                    context.lineTo( 8, 16 );
                    context.lineTo( 14, 10 );
                    context.lineTo( 15, 0 );
                    context.lineTo( 9, -10 );
                    context.lineTo( 2, 0 );
                    context.lineTo( -2, 0 );
                    context.lineTo( -9, -10 );
                    context.lineTo( -15, 0 );
                    context.lineTo( -14, 10 );
                    context.lineTo( -8, 16 );
                    context.lineTo( -4, 24 );
                    context.closePath();
                    context.fill();
                    context.restore();
                } ).start();
            }
            function animate( time ) {
                requestAnimationFrame( animate );
                TWEEN.update( time );
            }
            animate();
        </script>
    </body>
</html>
<!DOCTYPE html> 
<html lang="en">
    <head>
        <title>Black and Red / Tween.js</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="info">
            <h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
            <h2>02 _ Black and red</h2>
            <p>4096 continuously changing color cells (in a 64x64 table)</p>
            <p></p>
        </div>
        <div id="target"></div>

        <style>
            body { font-family: Arial, Helvetica, sans; }
            table { border-collapse: collapse; }
            td { width: 5px; height: 5px; }
            #target { position: absolute; top: 4em; right: 3em; }
        </style>

        <script src="../src/Tween.js"></script>
        <script src="js/stats.min.js"></script>
        <script src="js/RequestAnimationFrame.js"></script>
        <script>
            var stats;
            init();
            animate();
            function init() {
                var target = document.getElementById('target');
                stats = new Stats();
                target.appendChild(stats.domElement);
                var t = document.createElement('table');
                var index = 0;
                for(var i = 0; i < 64; i++) {
                    var tr = t.insertRow(-1);
                    for(var j = 0; j < 64; j++) {
                        var td = tr.insertCell(-1);
                        td.style.background = '#000';
                        var x = (i+j) * 0.1;
                        var cell = { 'td': td, value : 0 };
                        var tween = new TWEEN.Tween(cell)
                            .to({ value: 1 }, 8000)
                            .delay((0.001 * index + Math.random()) * 500)
                            .easing(TWEEN.Easing.Elastic.InOut)
                            .onUpdate(function() {
                                var c = Math.floor(this.value * 0xff);
                                this.td.style.background = 'rgb(' + c + ', 0, 0)';
                            });
                        var tweenBack = new TWEEN.Tween(cell)
                            .to({ value: 0 }, 4000)
                            .delay((0.001*index + Math.random()) * 500)
                            .easing(TWEEN.Easing.Elastic.InOut)
                            .onUpdate(function() {
                                var c = Math.floor(this.value * 0xff);
                                this.td.style.background = 'rgb(' + c + ', 0, 0)';
                            });
                        tween.chain(tweenBack);
                        tweenBack.chain(tween);
                        tween.start();
                        index++;
                    }
                }
                target.appendChild(t);
            }
            function animate( time ) {
                requestAnimationFrame( animate );
                TWEEN.update( time );
                stats.update();
            }
        </script>
    </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,three.jstween.js可以一起使用,实现复杂的动画效果,包括飞线动画。以下是一个简单的例子,演示了如何使用three.jstween.js创建一条飞线动画: ```javascript // 创建three.js场景 var scene = new THREE.Scene(); // 创建相机 var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; // 创建渲染器 var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 创建飞线路径 var curve = new THREE.CatmullRomCurve3([ new THREE.Vector3(-10, 0, 0), new THREE.Vector3(-5, 5, 0), new THREE.Vector3(0, 0, 0), new THREE.Vector3(5, -5, 0), new THREE.Vector3(10, 0, 0) ]); // 创建飞线材质 var material = new THREE.LineBasicMaterial({ color: 0xffffff }); // 将飞线路径转换为几何体 var geometry = new THREE.Geometry(); geometry.vertices = curve.getPoints(50); // 创建飞线网格 var line = new THREE.Line(geometry, material); scene.add(line); // 创建飞线动画 var tween = new TWEEN.Tween({t:0}) .to({t:1}, 5000) // 5秒钟 .onUpdate(function() { // 根据tween的进度计算飞线的位置 var position = curve.getPoint(this.t); // 更新飞线网格的位置 line.position.copy(position); }) .start(); // 渲染场景 function render() { requestAnimationFrame(render); TWEEN.update(); // 更新tween动画 renderer.render(scene, camera); } render(); ``` 在这段代码中,我们首先创建了一个three.js场景、相机和渲染器。然后,我们创建了一个CatmullRomCurve3曲线,用于定义飞线路径,并将其转换为three.js几何体。接下来,我们创建了一个TWEEN.Tween对象,将其起始值设置为0,结束值设置为1,表示飞线动画的进度。在Tween对象的 onUpdate 回调函数中,我们根据飞线路径计算当前进度对应的位置,并将飞线网格的位置更新为该位置。最后,我们创建了一个渲染函数,用于在每帧更新Tween动画和渲染场景。 希望这个例子可以帮助你了解如何使用three.jstween.js创建飞线动画
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值