• canvas绘图实现浏览器等待效果


    一:创建画布

    <canvas width="600" height="600" id="canvas" style="border:1px solid red;"></canvas>
    <input type="button" id="start" value="start">
    <input type="button" id="stop" value="stop">

    二:代码实现

    (function(window){

    var timer;
    var iStop = true;
    var degree = 0;

    var cxt = null;
    var _x = 0;
    var _y = 0;

    var fillStyles = [
    'rgba(255, 0, 0, 1)',
    'rgba(255, 0, 0, 0.6)',
    'rgba(255, 0, 0, 0.3)',
    'rgba(255, 0, 0, 0.1)',
    'rgba(0, 0, 255, 1)',
    'rgba(0, 0, 255, 0.6)',
    'rgba(0, 0, 255, 0.3)',
    'rgba(0, 0, 255, 0.1)'
    ];

    function draw() {
    for(var i=0; i<8; i++){
    cxt.save();
    cxt.beginPath();
    cxt.translate(_x, _y);
    cxt.rotate(-degree*Math.PI/180);
    cxt.moveTo(0, 0);
    cxt.fillStyle = fillStyles[i];
    cxt.arc(0, 0, 100, i*45*Math.PI/180, (i+1)*45*Math.PI/180, false);
    cxt.closePath();
    cxt.fill();
    cxt.restore();

    if(degree++ == 360){
    degree = 0;
    }
    }

    cxt.save();
    cxt.beginPath();
    cxt.fillStyle = 'white';
    cxt.arc(_x, _y, 60, 0, 360*Math.PI/180, false);
    cxt.closePath();
    cxt.fill();
    cxt.restore();
    }

    function erase() {
    cxt.clearRect(0, 0, canvas.width, canvas.height)
    }

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

    window.Ykload = function(canvas){
    cxt = canvas.getContext('2d');

    _x = canvas.width/2;
    _y = canvas.height/2;
    };

    window.Ykload.prototype.start = function(){
    if(iStop == true) {
    iStop = false;
    animate();
    }
    };

    window.Ykload.prototype.end = function(){
    iStop = true;
    };
    })(window);


    var canvas = document.getElementById('canvas');

    var ykload = new Ykload(canvas);

    document.getElementById('start').onclick = function(){
    ykload.start();
    };

    document.getElementById('stop').onclick = function(){
    ykload.end();
    };

  • 相关阅读:
    大道至简阅读笔记03
    团队项目二阶段-个人总结07
    团队项目二阶段-个人总结06
    团队项目二阶段-个人总结05
    学习进度条06
    领扣(LeetCode)单调数列 个人题解
    领扣(LeetCode)数字转换为十六进制数 个人题解
    领扣(LeetCode)字符串相加 个人题解
    领扣(LeetCode)删除链表中的节点 个人题解
    领扣(LeetCode)有效的括号 个人题解
  • 原文地址:https://www.cnblogs.com/liubu/p/7845796.html
Copyright © 2020-2023  润新知