zoukankan      html  css  js  c++  java
  • 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();
    };

  • 相关阅读:
    javaweb消息中间件——rabbitmq入门
    virtual box 桥接模式(bridge adapter)下无法获取ip(determine ip failed)的解决方法
    Apache Kylin本地启动
    git操作
    Java学习总结
    Java中同步的几种实现方式
    hibernate exception nested transactions not supported 解决方法
    vue 中解决移动端使用 js sdk 在ios 上一直报invalid signature 的问题解决
    cookie 的使用
    vue 专门为了解决修改微信标题而生的项目
  • 原文地址:https://www.cnblogs.com/liubu/p/7845796.html
Copyright © 2011-2022 走看看