JavaScript-用键盘控制动画

(一)添加keydown事件


<html>
    <head>
    <title>Keyboard input</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    </head>
    <body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script src="https://code.jquery.com/jquery-2.1.0.js"></script>
        <script>
        //创建一个keyNames对象
           var keyNames = {
            32 : "space",
            37 : "left",
            38 : "up",
            39 : "right",
            40 : "down"
           };
           //添加响应按键事件
           $("body").keydown(function(event){
            //在控制台打印答案
            console.log(keyNames[event.keyCode]);
           });
        </script>
    </body>
</html>


(二)用键盘移动一个球


<html>
    <head>
    <title>Keyboard input</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    </head>
    <body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script src="https://code.jquery.com/jquery-2.1.0.js"></script>
        <script>
        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        var width = canvas.width;
        var height = canvas.height;
        var circle = function(x,y,radius,fillCircle){
            ctx.beginPath();
            ctx.arc(x,y,radius,0,Math.PI*2,false);
            if(fillCircle){
                ctx.fill();
            } else {
                ctx.stroke();
            }
        };
        //创建Ball的构造方法
        var Ball = function(){
            this.x = width/2;
            this.y = height/2;
            this.xSpeed = 5;
            this.ySpeed = 0;
        };

        //定义移动函数
        Ball.prototype.move = function(){
            this.x += this.xSpeed;
            this.y += this.ySpeed;
            //移动到边界时从另外一边出来
            if(this.x<0){
                this.x = width;
            }else if(this.x>width){
                this.x = 0;
            }else if(this.y<0){
                this.y = height;
            }else if(this.y>height){
                this.y = 0;
            }
        };

        //定义draw方法
        Ball.prototype.draw = function(){
            circle(this.x,this.y,10,true);
        };

        //创建setDirection方法
        Ball.prototype.setDirection = function(direction){
            if(direction === "up"){
                this.xSpeed = 0;
                this.ySpeed = -5;
            }else if(direction === "down"){
                this.xSpeed =0;
                this.ySpeed = 5;
            }else if(direction === "left"){
                this.xSpeed = -5;
                this.ySpeed = 0;
            }else if(direction === "right"){
                this.xSpeed = 5;
                this.ySpeed = 0;
            }else if(direction === "stop"){
                this.xSpeed = 0;
                this.ySpeed = 0;
            }
        };
        //创建一个Ball对象
        var ball = new Ball();
        //创建一个keyNames对象
           var keyNames = {
            32 : "stop",
            37 : "left",
            38 : "up",
            39 : "right",
            40 : "down"
           };
           //添加响应按键事件
           $("body").keydown(function(event){
            var direction = keyNames[event.keyCode];
            ball.setDirection(direction);
           });
           //实现球的动画
           setInterval(function(){
            ctx.clearRect(0,0,width,height);
            ball.draw();
            ball.move();
            ctx.strokeRect(0,0,width,height);
           },30);
        </script>
    </body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
three.js是一种用于创建3D场景和动画JavaScript库。它提供了丰富的功能,可以创建各种效果和交互式场景。在three.js中,我们可以使用3D模型和键盘控制来实现人物动作和在场景中行走的动画。 首先,我们需要导入一个人物模型的3D对象。我们可以使用Blender等软件创建并导出一个包含人物动画的模型文件。然后,我们可以使用three.js加载这个模型,并将其添加到场景中。 一旦人物模型加载完成,我们可以使用人物模型的动画控制器来控制人物的动作。通过设置骨骼动画的关键帧,我们可以创建各种动作,如行走、跑步、跳跃等。通过调整动画控制器的播放速度和循环模式,我们可以实现各种不同的动作效果。 另外,我们可以使用键盘控制人物在场景中行走的动画。通过监听键盘的按键事件,我们可以根据按下的方向键来调整人物的移动方向和速度。例如,按下前进键时,我们向人物的前方移动一定距离;按下后退键时,我们向人物的后方移动一定距离。可以通过更新人物的位置和朝向来实现平滑的行走动画。 最后,我们还可以添加一些其他的功能来提升交互性。例如,可以添加碰撞检测,避免人物穿过物体或墙壁;在场景中添加其他的可交互元素,如道具或NPC角色,与人物进行互动等。 综上所述,借助three.js,我们可以实现人物动作和使用键盘控制在场景中行走的动画。通过创建人物模型和动画控制器,以及监听键盘事件来控制人物的移动,我们可以创建出令人印象深刻的交互式3D场景。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值