tween使用(1)

1.使用tween的案例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="target" style="position:absolute; top: 100px; left: 100px; width: 100px; height: 100px; background: #a0dde9; padding: 1em;">
    hello world!
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>
<script>
    var position;
    var target;
    var tween,tweenBack;

    init();
    animate();
    function init(){
        position={x:100,y:100,rotation:0};
        target=document.getElementById('target');
        //動畫從0,0位置到700,200的位置旋轉360
        tween=new TWEEN.Tween(position)
            .to({x:700,y:200,rotation:360},2000)
            .onUpdate(update);
        tween.start();
    }
    function animate( time ) {
        var id = requestAnimationFrame(animate);

        var result = TWEEN.update(time);

        if(!result) cancelAnimationFrame(id);
    }
    function update() {
        target.style.left = position.x + 'px';
        target.style.top = position.y + 'px';
        target.style.webkitTransform = 'rotate(' + Math.floor(position.rotation) + 'deg)';
        target.style.MozTransform = 'rotate(' + Math.floor(position.rotation) + 'deg)';
    }
</script>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章