【Canvas与徽章】盾形银底红带Best Quality Premium徽章

【成图】

【代码】

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

     <body οnlοad="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=198;// 基准尺寸

        // 1圈
        ctx.save();
        ctx.shadowOffsetY=8; // 设置阴影
        ctx.shadowColor="grey";
        ctx.shadowBlur=6;
        var r=R*1.00;
        ctx.fillStyle="white";
        drawShield(ctx,0,-R*0.02,r);
        ctx.fill();
        ctx.restore();

        // 2圈
        ctx.save();
        var r=R*0.99;
        var gnt1=ctx.createLinearGradient(0,-r,0,r);
        gnt1.addColorStop(0,"rgb(243,243,243)");
        gnt1.addColorStop(0.3,"rgb(171,171,171)");
        gnt1.addColorStop(0.6,"rgb(101,101,101)");
        gnt1.addColorStop(0.7,"rgb(114,114,114)");
        gnt1.addColorStop(0.8,"rgb(97,97,97)");
        gnt1.addColorStop(0.9,"rgb(87,87,87)");
        gnt1.addColorStop(1,"rgb(156,156,156)");
        ctx.fillStyle=gnt1;
        drawShield(ctx,0,0,r);
        ctx.fill();
        ctx.restore();

        // 3圈
        ctx.save();
        var r=R*0.94;
        ctx.fillStyle="black";
        drawShield(ctx,0,0,r);
        ctx.fill();
        ctx.restore();

        // 4圈
        ctx.save();
        var r=R*0.93;
        var gnt1=ctx.createLinearGradient(-r,-r,r,r);
        gnt1.addColorStop(0,"rgb(208,208,208)");
        gnt1.addColorStop(0.33,"rgb(244,244,244)");
        gnt1.addColorStop(0.5,"rgb(203,203,203)");
        gnt1.addColorStop(0.67,"rgb(246,246,246)");
        gnt1.addColorStop(1,"rgb(187,187,187)");
        ctx.fillStyle=gnt1;
        drawShield(ctx,0,0,r);
        ctx.fill();
        ctx.restore();

        // BEST文字
        ctx.save();
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.50+"px Bahnschrift SemiBold SemiConden";
        ctx.fillStyle="black";
        ctx.fillText("BEST",0,-r*0.56);
        ctx.restore();

        // QUALITY文字
        ctx.save();
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.33+"px Bahnschrift SemiBold SemiConden";
        ctx.fillStyle="black";
        ctx.fillText("QUALITY",0,-r*0.13);
        ctx.restore();

        // 红带
        ctx.save();
        var r=R*0.99;        
        var width=r/4*3.4;
        var gnt1=ctx.createLinearGradient(-width,0,width,0);
        gnt1.addColorStop(0,"rgb(111,1,2)");
        gnt1.addColorStop(0.1,"rgb(162,3,7)");
        gnt1.addColorStop(0.25,"rgb(182,2,4)");
        gnt1.addColorStop(0.5,"rgb(208,2,4)");
        gnt1.addColorStop(0.75,"rgb(182,2,4)");
        gnt1.addColorStop(0.9,"rgb(162,3,7)");
        gnt1.addColorStop(1,"rgb(111,1,2)");
        ctx.fillStyle=gnt1;
        drawRect(ctx,0,r/3,2*width,r/2);
        ctx.fill();
        ctx.restore();

        // PRESMIUM文字
        ctx.save();
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.25+"px Bahnschrift SemiBold SemiConden";
        ctx.fillStyle="white";
        ctx.fillText("PRESMIUM",0,r*0.36);
        ctx.restore();

        // 绘制五星
        ctx.save();
        var r=R*0.99;        
        var width=r/4*3.4;
        var margin=width/4;
        var n=5;
        var w=(2*width-2*margin)/(n-1);

        // 中间三星
        for(var i=-1;i<=1;i++){            
            var radius=r*0.15-Math.abs(i)*r/30;
            var a=createPt(i*w,r*0.77+radius*Math.tan(Math.PI/10));

            ctx.save();
            ctx.translate(a.x,a.y);
            var gnt=ctx.createRadialGradient(0,0,0,0,0,radius);
            gnt.addColorStop(0,"red");
            gnt.addColorStop(1,"rgb(117,0,0)");
            ctx.fillStyle=gnt;
            draw5Star(ctx,0,0,radius);
            ctx.fill();
            ctx.restore();
        }

        // 边上两星
        w*=0.9;
        for(var i=-2;i<=2;i+=4){            
            var radius=r*0.15-Math.abs(i)*r/30;
            var a=createPt(i*w,r*0.77+radius*Math.tan(Math.PI/10));

            ctx.save();
            ctx.translate(a.x,a.y);
            var gnt=ctx.createRadialGradient(0,0,0,0,0,radius);
            gnt.addColorStop(0,"red");
            gnt.addColorStop(1,"rgb(117,0,0)");
            ctx.fillStyle=gnt;
            draw5Star(ctx,0,0,radius);
            ctx.fill();
            ctx.restore();
        }
        
        ctx.restore();
                
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*--------------------------------------------------
函数:绘制正五角星
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:矩形中心纵坐标
width:矩形宽
height:矩形高
----------------------------------------------------------*/
function drawRect(ctx,x,y,width,height){
    ctx.beginPath();
    ctx.moveTo(x-width/2,y-height/2);
    ctx.lineTo(x+width/2,y-height/2);
    ctx.lineTo(x+width/2,y+height/2);
    ctx.lineTo(x-width/2,y+height/2);
    ctx.closePath();
}

/*----------------------------------------------------------
函数:用于绘制五边形圆角盾牌
ctx:绘图上下文
x:盾牌中心横坐标
y:盾牌中心纵坐标
radius:半径,即盾牌高度的一半
----------------------------------------------------------*/
function drawShield(ctx,x,y,radius){
    const R=radius;// 接参数
    const H=R;// 一半高度
    const H1=H/5;// 隆起高度
    const W=H/4*3.4;// 一半宽度
    const R1=R/20;// 小圆半径
    const R2=2.5*R1;// 大圆半径
    const ALPHA=Math.atan(H1/W);//隆起角度

    var a=createPt(x-W,y-H);
    var r=R1;
    var angle=0;
    var a1=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
    r=R1;
    angle=Math.PI/2;
    var a2=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

    var b=createPt(x-W,y+H);
    r=R1;
    angle=-Math.PI/2;
    var b1=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));
    r=R1;
    angle=ALPHA;
    var b2=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

    var c=createPt(x,y+H+H1);
    r=R2;
    angle=Math.PI+ALPHA;
    var c1=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle));
    r=R2;
    angle=-ALPHA;
    var c2=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle));

    var d=createPt(x+W,y+H);
    r=R1;
    angle=Math.PI-ALPHA;
    var d1=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle));
    r=R1;
    angle=-Math.PI/2;
    var d2=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle));

    var e=createPt(x+W,y-H);
    r=R1;
    angle=Math.PI/2;
    var e1=createPt(e.x+r*Math.cos(angle),e.y+r*Math.sin(angle));
    r=R1;
    angle=-Math.PI;
    var e2=createPt(e.x+r*Math.cos(angle),e.y+r*Math.sin(angle));
    
    
    ctx.beginPath();
    ctx.moveTo(a1.x,a1.y);
    ctx.quadraticCurveTo(a.x,a.y,a2.x,a2.y);
    ctx.lineTo(b1.x,b1.y);
    ctx.quadraticCurveTo(b.x,b.y,b2.x,b2.y);
    ctx.lineTo(c1.x,c1.y);
    ctx.quadraticCurveTo(c.x,c.y,c2.x,c2.y);
    ctx.lineTo(d1.x,d1.y);
    ctx.quadraticCurveTo(d.x,d.y,d2.x,d2.y);
    ctx.lineTo(e1.x,e1.y);
    ctx.quadraticCurveTo(e.x,e.y,e2.x,e2.y);
    ctx.closePath();
    
}

/*----------------------------------------------------------
函数:用于绘制实心圆
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
style:填充圆的方案
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,style){
    ctx.fillStyle=style;
    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值