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();
    };

  • 相关阅读:
    网格视图
    使用box-shadow 属性用来可以创建纸质样式卡片:
    css 按钮动画
    vue父组件向自定义组件传递参数过程
    vue中如何使用 v-model 实现双向数据绑定?
    vue中是如何实现响应键盘回车事件
    vue中如何实现 样式绑定?
    webpack require.Context功能作用
    Personal tips for success
    my blog frist
  • 原文地址:https://www.cnblogs.com/liubu/p/7845796.html
Copyright © 2011-2022 走看看