首页 > 其他分享 >【Canvas与徽章】金穗蓝盾徽章

【Canvas与徽章】金穗蓝盾徽章

时间:2024-09-26 11:51:57浏览次数:3  
标签:function Canvas 蓝盾 ctx canvas HEIGHT WIDTH 徽章 var

【成图】

【Canvas与徽章】金穗蓝盾徽章 _徽章

【Canvas与徽章】金穗蓝盾徽章 _徽章_02

【Canvas与徽章】金穗蓝盾徽章 _徽章_03

【Canvas与徽章】金穗蓝盾徽章 _canvas_04

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>金穗蓝盾 draft1</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body onl oad="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 高宽
const WIDTH=512;
const HEIGHT=512;

// 舞台对象
var stage;

//-------------------------------
// 初始化
//-------------------------------
function init(){
    // 获得canvas对象
    var canvas=document.getElementById('myCanvas');  
    canvas.width=WIDTH;
    canvas.height=HEIGHT;

    // 初始化canvas的绘图环境
    ctx=canvas.getContext('2d');  
    ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移

    // 准备
    stage=new Stage();    
    stage.init();

    // 开幕
    animate();
}

// 播放动画
function animate(){    
    stage.update();    
    stage.paintBg(ctx);
    stage.paintFg(ctx);     

    // 循环
    if(true){
        //sleep(100);
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){
    // 初始化
    this.init=function(){
        
    }

    // 更新
    this.update=function(){    
        
    }

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏    
    }

    // 画前景
    this.paintFg=function(ctx){
        // 底色
        ctx.save();
        ctx.fillStyle = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();

        const R=210;// 基准尺寸

        // 万丈光芒
        ctx.save();
        var r=R*1.20;
        var N=48;
        var PART=Math.PI*2/N;
        var ANGLE=PART/2;
        var gnt=ctx.createRadialGradient(0,0,0,0,0,r);
        gnt.addColorStop(0,"rgb(244,234,113)");
        gnt.addColorStop(0.85,"rgb(244,234,113)");
        gnt.addColorStop(1,"white");
        for(var i=0;i<N;i++){
            var theta=i*PART;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
            var b=createPt(r*Math.cos(theta+ANGLE),r*Math.sin(theta+ANGLE));

            ctx.fillStyle=gnt;
            ctx.beginPath();
            ctx.moveTo(0,0);
            ctx.lineTo(a.x,a.y);
            ctx.lineTo(b.x,b.y);
            ctx.closePath();
            ctx.fill();
        }
        ctx.restore();

        // 橄榄枝
        ctx.save();
        // 设置阴影
        ctx.shadowOffsetY=2;
        ctx.shadowColor="grey";
        ctx.shadowBlur=1;

        var r=R*1.00;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(255,254,237)");
        gnt.addColorStop(0.25,"rgb(255,221,129)");
        gnt.addColorStop(0.5,"rgb(254,235,192)");
        gnt.addColorStop(0.6,"rgb(102,39,4)");
        gnt.addColorStop(0.75,"rgb(254,185,3)");
        gnt.addColorStop(1,"rgb(247,222,129)");
        var N=36;
        // 左橄榄枝
        drawLeftOliver(ctx,0,0,r,N,Math.PI/2,Math.PI/2*3-Math.PI/8,Math.PI/6,Math.PI/4,gnt);
        // 右橄榄枝
        drawRightOliver(ctx,0,0,r,N,-Math.PI/2+Math.PI/8,Math.PI/2,Math.PI/6,Math.PI/4,gnt);
        ctx.restore();

        // 上方三颗星
        var ANGLE=Math.PI/7;// 张角
        var N=3;
        var PART=ANGLE/(N-1);
        var r=R*1.00;
        for(var i=0;i<N;i++){
            var theta=-Math.PI/2-ANGLE/2+PART*i;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
            ctx.save();
            // 设置阴影
            ctx.shadowOffsetY=1;
            ctx.shadowColor="grey";
            ctx.shadowBlur=1;
            ctx.translate(a.x,a.y);
            ctx.rotate(theta+Math.PI/2);
            ctx.strokeStyle="rgb(254,185,3)";
            ctx.fillStyle="gold";
            draw5Star(ctx,0,0,r/16);
            ctx.fill();
            ctx.stroke();
            ctx.restore();
        }

        // 下方一颗星
        ctx.save();
        var r=R*1.00;
        // 设置阴影
        ctx.shadowOffsetY=1;
        ctx.shadowColor="grey";
        ctx.shadowBlur=1;
        ctx.strokeStyle="rgb(254,185,3)";
        ctx.fillStyle="gold";
        draw5Star(ctx,0,r,r/14);
        ctx.fill();
        ctx.stroke();
        ctx.restore();

        // 最外层竖向渐变外框
        ctx.save();
        var r=R*0.86;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(238,230,165)");
        gnt.addColorStop(0.25,"rgb(222,183,62)");
        gnt.addColorStop(0.5,"rgb(255,255,190)");
        gnt.addColorStop(0.6,"rgb(152,97,33)");
        gnt.addColorStop(0.8,"rgb(252,238,165)");
        gnt.addColorStop(1,  "rgb(217,173,74)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 第一层上半圈渐变外框
        ctx.save();
        var ro=R*0.85;// outer radius
        var ri=R*0.78;// inner radius
        var gnt=ctx.createLinearGradient(0,-ro,0,0);
        gnt.addColorStop(0,  "rgb(255,254,250)");
        gnt.addColorStop(0.5,"rgb(253,221,112)");
        gnt.addColorStop(0.9,"rgb(254,235,192)");
        gnt.addColorStop(1,"rgb(254,222,145)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,-Math.PI,true);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,-Math.PI,0,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 第一层下半圈渐变外框
        ctx.save();
        var ro=R*0.85;// outer radius
        var ri=R*0.78;// inner radius
        var gnt=ctx.createLinearGradient(0,0,0,ro);
        gnt.addColorStop(0,  "rgb(254,222,145)");
        gnt.addColorStop(0.10,  "rgb(136,60,0)");
        gnt.addColorStop(0.5,"rgb(245,206,15)");
        gnt.addColorStop(1,"rgb(255,233,165)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,Math.PI,false);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,Math.PI,0,true);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 墨圈
        ctx.save();
        var r=R*0.78;
        ctx.fillStyle="rgb(1,25,37)";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 第2层上半圈渐变外框
        ctx.save();
        var ro=R*0.77;// outer radius
        var ri=R*0.73;// inner radius
        var gnt=ctx.createLinearGradient(0,-ro,0,0);
        gnt.addColorStop(0,  "rgb(254,252,239)");
        gnt.addColorStop(0.5,"rgb(251,218,125)");
        gnt.addColorStop(0.9,"rgb(255,230,165)");
        gnt.addColorStop(1,"rgb(90,17,0)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,-Math.PI,true);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,-Math.PI,0,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 第2层下半圈渐变外框
        ctx.save();
        var ro=R*0.77;// outer radius
        var ri=R*0.73;// inner radius
        var gnt=ctx.createLinearGradient(0,0,0,ro);
        gnt.addColorStop(0,  "rgb(90,17,0)");
        gnt.addColorStop(0.5,"rgb(200,139,0)");
        gnt.addColorStop(1,"rgb(255,233,165)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,Math.PI,false);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,Math.PI,0,true);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 左上到右下渐变蓝圈
        ctx.save();
        var r=R*0.72;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,  "rgb(5,164,232)");
        gnt.addColorStop(0.25,"rgb(0,111,164)");
        gnt.addColorStop(0.5,"rgb(7,60,91)");
        gnt.addColorStop(0.75,"rgb(1,31,57)");
        gnt.addColorStop(1,  "rgb(0,5,25)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 右下弯月蒙版
        ctx.save();
        var r=R*0.72;
        var alpha=Math.PI/3;
        var beta=Math.PI/2-alpha;
        ctx.fillStyle="rgba(0,5,25,0.7)";
        //ctx.strokeStyle="white";
        ctx.beginPath();
        ctx.arc(0,0,r,Math.PI/4-alpha,Math.PI/4+alpha,false);
        ctx.arc(r/2*Math.sqrt(2),r/2*Math.sqrt(2),r,Math.PI/4*5-alpha,Math.PI/4*5+alpha,false);
        ctx.closePath();
        ctx.fill();
        //ctx.stroke();
        ctx.restore();

        // 上下渐变墨圈
        ctx.save();
        var r=R*0.51;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(1,25,37)");
        gnt.addColorStop(0.5,  "rgb(1,25,37)");
        gnt.addColorStop(0.75,  "rgb(3,41,60)");
        gnt.addColorStop(1,"rgb(0,111,164)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 第3层上半圈渐变外框
        ctx.save();
        var ro=R*0.50;// outer radius
        var ri=R*0.44;// inner radius
        var gnt=ctx.createLinearGradient(0,-ro,0,0);
        gnt.addColorStop(0,  "rgb(253,223,135)");
        gnt.addColorStop(0.5,"rgb(255,238,186)");
        gnt.addColorStop(0.8,"rgb(250,208,98)");
        gnt.addColorStop(1,"rgb(117,44,1)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,-Math.PI,true);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,-Math.PI,0,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 第3层下半圈渐变外框
        ctx.save();
        var ro=R*0.50;// outer radius
        var ri=R*0.44;// inner radius
        var gnt=ctx.createLinearGradient(0,0,0,ro);
        gnt.addColorStop(0,  "rgb(117,44,1)");
        gnt.addColorStop(0.10,  "rgb(171,117,11)");
        gnt.addColorStop(0.5,"rgb(250,202,6)");
        gnt.addColorStop(1,"rgb(251,221,123)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,Math.PI,false);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,Math.PI,0,true);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 墨圈
        ctx.save();
        var r=R*0.44;
        ctx.fillStyle="rgb(1,25,37)";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 中心上方上下渐变蓝圆
        ctx.save();
        var r=R*0.43;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(230,240,249)");
        gnt.addColorStop(0.25,  "rgb(41,183,229)");
        gnt.addColorStop(0.5,  "rgb(2,115,159)");
        gnt.addColorStop(1,"navy");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 中心下方波浪圆
        ctx.save();
        var r=R*0.43;        
        var gnt=ctx.createRadialGradient(0,0,0,0,0,r);
        gnt.addColorStop(0,"rgb(187,126,0)");
        gnt.addColorStop(0.25,"rgb(153,100,4)");
        gnt.addColorStop(0.75,"rgb(89,53,1)");
        gnt.addColorStop(1,"rgb(56,36,0)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI,false);
        ctx.quadraticCurveTo(-r/2,r/4,0,0);
        ctx.quadraticCurveTo(r/2,-r/4,r,0);
        ctx.fill();
        ctx.restore();

        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:画顺时针橄榄枝
x:横坐标
y:纵坐标
r:橄榄枝中心半径
n:叶片对数
start:起始角度
end:中止角度
alpha: 叶片中线到圆弧的角度
beta:叶片中线到圆弧的角度
color:叶片颜色
----------------------------------------------------------*/
function drawLeftOliver(ctx,x,y,r,n,start,end,alpha,beta,color){
    const R=r;                // 橄榄枝中心半径
    var   N=n;                // 有N对叶片    
    const START=start;        // 起始角度
    const END=end;            // 中止角度
    const STEP=(END-START)/N;// 步长        
    const ALPHA=alpha;        // 叶片中线到圆弧的角度
    const L=R/8;            // 叶片长度
    const BETA=beta;        // 叶片中线到圆弧的角度
    
    ctx.save();    
    ctx.translate(x,y);
    ctx.rotate(STEP);
    ctx.lineWidth=1;
    ctx.strokeStyle=color;

    // 画中心圈
    ctx.beginPath();
    for(var i=0;i<N;i++){
        var theta=START+STEP*i;
        var r=R;
        var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
        ctx.lineTo(a.x,a.y);            
    }
    ctx.stroke();

    
    // 画外侧叶子
    for(var i=0;i<N;i++){
        var theta=START+STEP*i;
        var r=R;
        var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
        
        var ratio=1-i/N*0.5;
        r=L/9;
        r=r*ratio;
        var angle=theta+ALPHA;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=L/9*4/Math.cos(BETA);
        r=r*ratio;
        angle=theta+ALPHA+BETA;
        var d=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L/9*4/Math.cos(BETA);
        r=r*ratio;
        angle=theta+ALPHA-BETA;
        var e=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L;
        r=r*ratio;
        angle=theta+ALPHA;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
            
        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.quadraticCurveTo(d.x,d.y,c.x,c.y);
        ctx.quadraticCurveTo(e.x,e.y,b.x,b.y);            
        ctx.closePath();            
        ctx.fill();
    }

    // 画内侧叶子
    for(var i=0;i<N;i++){
        var theta=START+STEP*i;
        var r=R;
        var a=createPt(r*Math.cos(theta),r*Math.sin(theta));

        var ratio=1-i/N*0.5;
        
        r=L/9;
        r=r*ratio;
        var angle=theta-ALPHA+Math.PI;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));        

        r=L/9*4/Math.cos(BETA);
        r=r*ratio;
        angle=theta-ALPHA+Math.PI+BETA;
        var d=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L/9*4/Math.cos(BETA);
        r=r*ratio;
        angle=theta-ALPHA+Math.PI-BETA;
        var e=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L;
        r=r*ratio;
        angle=theta-ALPHA+Math.PI;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
            
        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.quadraticCurveTo(d.x,d.y,c.x,c.y);
        ctx.quadraticCurveTo(e.x,e.y,b.x,b.y);            
        ctx.closePath();            
        ctx.fill();
    }    

    ctx.restore();
}

/*----------------------------------------------------------
函数:画逆时针橄榄枝
x:横坐标
y:纵坐标
r:橄榄枝中心半径
n:叶片对数
start:起始角度
end:中止角度
alpha: 叶片中线到圆弧的角度
beta:叶片中线到圆弧的角度
color:叶片颜色
----------------------------------------------------------*/
function drawRightOliver(ctx,x,y,r,n,start,end,alpha,beta,color){
    const R=r;                // 橄榄枝中心半径
    var   N=n;                // 有N对叶片    
    const START=start;        // 起始角度
    const END=end;            // 中止角度
    const STEP=(END-START)/N;// 步长        
    const ALPHA=alpha;        // 叶片中线到圆弧的角度
    const L=R/8;            // 叶片长度
    const BETA=beta;        // 叶片中线到圆弧的角度
    
    ctx.save();
    ctx.translate(x,y);
    ctx.lineWidth=1;
    ctx.strokeStyle=color;

    // 画中心圈
    ctx.beginPath();
    for(var i=0;i<N;i++){
        var theta=START+STEP*i;
        var r=R;
        var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
        ctx.lineTo(a.x,a.y);            
    }
    ctx.stroke();

    // 画外侧叶子
    for(var i=0;i<N;i++){
        var theta=START+STEP*i;
        var r=R;
        var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
        
        var ratio=0.5+i/N*0.5;
        r=L/9;
        r*=ratio;
        var angle=theta-ALPHA;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=L/9*4/Math.cos(BETA);
        r*=ratio;
        angle=theta-ALPHA+BETA;
        var d=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L/9*4/Math.cos(BETA);
        r*=ratio;
        angle=theta-ALPHA-BETA;
        var e=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L;
        r*=ratio;
        angle=theta-ALPHA;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
            
        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.quadraticCurveTo(d.x,d.y,c.x,c.y);
        ctx.quadraticCurveTo(e.x,e.y,b.x,b.y);            
        ctx.closePath();            
        ctx.fill();
    }

    // 画内侧叶子
    for(var i=0;i<N;i++){
        var theta=START+STEP*i;
        var r=R;
        var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
        
        r=L/9;
        var angle=theta+ALPHA-Math.PI;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        var ratio=0.5+i/N*0.5;

        r=L/9*4/Math.cos(BETA);
        r*=ratio;
        angle=theta+ALPHA-Math.PI+BETA;
        var d=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L/9*4/Math.cos(BETA);
        r*=ratio;
        angle=theta+ALPHA-Math.PI-BETA;
        var e=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=L;
        r*=ratio;
        angle=theta+ALPHA-Math.PI;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
            
        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.quadraticCurveTo(d.x,d.y,c.x,c.y);
        ctx.quadraticCurveTo(e.x,e.y,b.x,b.y);            
        ctx.closePath();            
        ctx.fill();
    }    

    ctx.restore();
}

/*--------------------------------------------------
函数:绘制正五角星
ctx:绘图上下文
x:五角星中心横坐标
y:五角星中心纵坐标
R:五角星中心到顶点的距离
---------------------------------------------------*/
function draw5Star(ctx,x,y,R){
    var r=R*Math.sin(Math.PI/10)/Math.sin(Math.PI/10*7);
    
    var arr=[0,0,0,0,0,0,0,0,0,0];

    // 顶五点
    for(var i=0;i<5;i++){
        var theta=i*Math.PI/5*2-Math.PI/10;
        var x1=R*Math.cos(theta)+x;
        var y1=R*Math.sin(theta)+y;
        arr[i*2]=createPt(x1,y1);
    }

    // 内五点
    for(var i=0;i<5;i++){
        var theta=i*Math.PI/5*2+Math.PI/10;
        var x1=r*Math.cos(theta)+x;
        var y1=r*Math.sin(theta)+y;
        arr[i*2+1]=createPt(x1,y1);
    }

    ctx.beginPath();
    for(var i=0;i<arr.length;i++){
        ctx.lineTo(arr[i].x,arr[i].y);
    }
    ctx.closePath();
}

/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
color:填充圆的颜色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){
    ctx.fillStyle=color;
    ctx.beginPath();
    ctx.arc(x,y,r,0,Math.PI*2,false);
    ctx.closePath();
    ctx.fill();
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
    var retval={};
    retval.x=x;
    retval.y=y;
    return retval;
}

/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {
    const date = Date.now();
    let currDate = null;
    while (currDate - date < milliSeconds) {
        currDate = Date.now();
    } 
}

/*----------------------------------------------------------
函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){
    ctx.save();
    ctx.textBaseline="bottom";
    ctx.textAlign="center";
    ctx.font = font;
    ctx.fillStyle=color;
    ctx.fillText(text,x,y);
    ctx.restore();
}

/*-------------------------------------------------------------
愿意放弃自由来换取保障的人,最终既得不到自由,也得不到保障。
                                                      --哈耶克
--------------------------------------------------------------*/
//-->
</script>

END

标签:function,Canvas,蓝盾,ctx,canvas,HEIGHT,WIDTH,徽章,var
From: https://blog.51cto.com/u_7726611/12118278

相关文章

  • 【Canvas与徽章】庆祝建国75周年徽章
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>庆祝建国75周年</title><styletype="text/css"......
  • 手写板 canvas
    <!doctypehtml><html><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge,chrome=1"><metaname="viewport"content="width=device-width">&......
  • Canvas简历编辑器-Monorepo+Rspack工程实践
    Canvas简历编辑器-Monorepo+Rspack工程实践 Canvas简历编辑器-Monorepo+Rspack工程实践在之前我们围绕Canvas聊了很多代码设计层面的东西,在这里我们聊一下工程实践。在之前的文中我也提到过,因为是本着学习的态度以及对技术的好奇心来做的,所以除了一些工具类的库例如 ArcoDe......
  • 【Canvas与诗词】铁马冰河入梦来
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>金红圈铁马冰河入梦来</title><styletype="text/css&quo......
  • 【Unity】UI、背景和3D的Camera和Canvas设置
    目前存在需求背景是指定的图片,该图片始终显示在页面中,不会因场景的视角操控发生尺寸等变化;UI内容显示在页面最上层,同样不会因场景的视角操控发生尺寸等变化,但是当软件整个尺寸发生变化时,会跟随变化,UI内容会覆盖3D物体;3D物体可以随着相机视角的变化而变近变远等,3D物体上可能存在......
  • 【Canvas技法】辐射式多道光影的实现
    【用余弦函数实现辐射四道光】成图:代码:constR=180;//基准尺寸//十字纹ctx.save();r=R;varN=3600;for(vari=0;i<N;i++){vartheta=Math.PI*2/N*i;vara=createPt(r*Math.cos(theta),r*Math.s......
  • 【Canvas与诗词】木兰辞节选
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>金边钢底徽章</title><styletype="text/css"&g......
  • Netty+HTML5+Canvas 网络画画板实时在线画画
    采用Html5的canvas做前端画画板,发送数据到后端Netty服务,实时转发笔迹数据,在线实时同步画笔轨迹,单击绿色小方块,保存画板的图片页面:<!--index.html--><!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>网络画画板</title></head><body&g......
  • WPF Canvas show custom control with ellipse filled with image and text,peridoica
    //customcontrol<UserControlx:Class="WpfApp389.ElpImageTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • 【Canvas与诗词】《侠客行》节选
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>437.侠客行</title><styletype="text/css"&......