• canvas 实现弹跳效果


    一:创建画布

    <canvas width="600" height="600" id="canvas"></canvas>

    二:代码实现

    var canvas = document.getElementById('canvas');
    var cxt = canvas.getContext('2d');

    var timer;
    var gravity = 1;
    var buffer = 5;

    var iStop = false;

    var boxes = [
    {'startSpeed':-20,'currentSpeed':-20,'boxTop':500,'boxLeft':0},
    {'startSpeed':-10,'currentSpeed':-10,'boxTop':500,'boxLeft':100},
    {'startSpeed':-30,'currentSpeed':-30,'boxTop':500,'boxLeft':200},
    {'startSpeed':-10,'currentSpeed':-10,'boxTop':500,'boxLeft':300},
    {'startSpeed':-20,'currentSpeed':-20,'boxTop':500,'boxLeft':400}
    ];

    function draw(){
    for(var i=0; i<boxes.length; i++){
    boxes[i].currentSpeed += gravity;
    boxes[i].boxTop += boxes[i].currentSpeed;
    if(boxes[i].boxTop > 500){
    boxes[i].boxTop -= boxes[i].currentSpeed;
    }

    cxt.fillRect(boxes[i].boxLeft, boxes[i].boxTop, 100, 100);
    if(boxes[i].boxTop >= 500){
    boxes[i].startSpeed += buffer;
    boxes[i].currentSpeed = boxes[i].startSpeed;
    }
    }
    }

    function erase(){
    cxt.clearRect(0, 0, 600, 600);
    }

    window.requestAnimationFrame =
    window.requestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.msRequestAnimationFrame;

    window.cancelRequestAnimationFrame =
    window.cancelRequestAnimationFrame ||
    window.mozCancelRequestAnimationFrame ||
    window.webkitCancelRequestAnimationFrame ||
    window.msCancelRequestAnimationFrame;

    function animate() {
    erase();
    draw();

    if(iStop){
    cancelRequestAnimationFrame(timer);
    }else{
    timer = requestAnimationFrame(animate);
    }
    }

    animate();

  • 相关阅读:
    C++头文件,预处理详解
    在VS2013中查看C/C++预处理后的文件
    使用apache.lang包安全简洁地操作Java时间
    FileInputStream 和 FileOutputStream
    【转】彻底搞清计算结构体大小和数据对齐原则
    NDK学习笔记-gdb调试
    NDK学习笔记-gdb调试
    NDK学习笔记-多线程与生产消费模式
    NDK学习笔记-多线程与生产消费模式
    Android-Makefile
  • 原文地址:https://www.cnblogs.com/liubu/p/7846891.html
Copyright © 2020-2023  润新知