【Canvas与旗帜】条纹版大明三辰旗

【成图】

1

2

3

4

5

【代码】

<!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 onload="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 = "rgb(9,41,178)";
        //ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        //ctx.restore();
        
        const R=210;//基准尺寸

        // 第一圈
        ctx.save();
        ctx.shadowOffsetX=-R/210*6; // 阴影
        ctx.shadowOffsetY=R/210*6; 
        ctx.shadowColor="lightgrey";
        ctx.shadowBlur=R/210*6;

        var r=R*1.00;
        var w=2*r;// 高宽
        var h=w/3*2.1;
        var gnt=ctx.createLinearGradient(0,-h/2,0,h/2);
        gnt.addColorStop(0,"white");
        gnt.addColorStop(0.1,"lightgrey");
        gnt.addColorStop(0.5,"lightgrey");
        gnt.addColorStop(0.9,"lightgrey");
        gnt.addColorStop(1,"lightgrey");
        ctx.fillStyle=gnt;
        drawRoundRect(ctx,0,0,w,h,R/6+0.02*R);// 淡灰外框
        ctx.fill();
        ctx.restore();

        // 第二圈
        ctx.save();
        var r=R*1.00;
        var w=2*r;// 高宽
        var h=w/3*2;

        drawRoundRect(ctx,0,0,w,h,R/8);// 限制范围
        ctx.clip();

        fillHorizontalZebraRect(ctx,0,0,w,h,13,"rgb(166,31,37)","rgb(203,203,203)");// 底色 223

        drawSolidCircle(ctx,0,0,w*0.25,"rgb(239,188,68)");// 黄圈
        drawSolidCircle(ctx,0,0,w*0.23,"rgb(254,244,220)");// 淡黄圈

        ctx.lineWidth=R/210*4;// 十二星
        ctx.strokeStyle="rgb(159,12,12)";
        ctx.fillStyle="black";
        var radius=w*0.20;
        drawNStar(ctx,0,0,12,radius,radius*0.65);
        ctx.fill();
        ctx.stroke();

        drawSolidCircle(ctx,0,0,w*0.14,"rgb(159,12,12)");// 红圈
        drawSolidCircle(ctx,0,0,w*0.13,"rgb(255,240,211)");// 淡黄圈
        drawSolidCircle(ctx,0,0,w*0.12,"rgb(60,108,162)");// 蓝圈
        drawSolidCircle(ctx,-w*0.03,0,w*0.09,"rgb(255,240,211)");// 左偏淡黄圈
        drawSolidCircle(ctx,-w*0.03,0,w*0.085,"rgb(239,188,68)");// 左偏黄圈
    
        ctx.restore();
                
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*--------------------------------------------------
函数:绘制标准正N角星轮廓,可描边,可填充
ctx:绘图上下文
x:轮廓中心横坐标
y:轮廓中心纵坐标
n:角数
rout:中心到外角尖的距离
rin:中心到内角尖的距离
---------------------------------------------------*/
function drawNStar(ctx,x,y,n,rout,rin){
    var arr=new Array(2*n);

    for(var i=0;i<n;i++){
        var theta=Math.PI*2/n*i;
        var p=createPt2(x,y,rout,theta);
        arr[i*2]=p;
    }

    for(var i=0;i<n;i++){
        var theta=Math.PI*2/n*i+Math.PI/n;
        var p=createPt2(x,y,rin,theta);
        arr[i*2+1]=p;
    }

    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:矩形高
count:条带数
color1:偶数色(从0开始)
color2:奇数色
----------------------------------------------------------*/
function fillHorizontalZebraRect(ctx,x,y,width,height,count,color1,color2){
    ctx.save();
    for(var i=0;i<count;i++){
        ctx.fillStyle=(i % 2==0)?color1:color2;
        ctx.fillRect(x-width/2, y-height/2+height/count*i, width, height/count);         
    }
    ctx.restore();    
}

/*----------------------------------------------------------
函数:用于绘制圆角矩形
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
radius:圆角半径
----------------------------------------------------------*/
function drawRoundRect(ctx,x,y,width,height,radius){
    ctx.beginPath();
    ctx.moveTo(x-width/2+radius,y-height/2);
    ctx.lineTo(x+width/2-radius,y-height/2);
    ctx.arcTo(x+width/2,y-height/2,x+width/2,y-height/2+radius,radius);
    ctx.lineTo(x+width/2,y-height/2+radius);
    ctx.lineTo(x+width/2,y+height/2-radius);
    ctx.arcTo(x+width/2,y+height/2,x+width/2-radius,y+height/2,radius);
    ctx.lineTo(x+width/2-radius,y+height/2);
    ctx.lineTo(x-width/2+radius,y+height/2);
    ctx.arcTo(x-width/2,y+height/2,x-width/2,y+height/2-radius,radius);
    ctx.lineTo(x-width/2,y+height/2-radius);
    ctx.lineTo(x-width/2,y-height/2+radius);
    ctx.arcTo(x-width/2,y-height/2,x-width/2+radius,y-height/2,radius);
    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:矩形中心纵坐标
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();
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
baseX:基准点横坐标
baseY:基准点纵坐标
radius:当前点到基准点的距离
theta:当前点到基准点的角度
Pt即Point
----------------------------------------------------------*/
function createPt2(baseX,baseY,radius,theta){
    var retval={};
    retval.x=baseX+radius*Math.cos(theta);
    retval.y=baseY+radius*Math.sin(theta);
    return retval;
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
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();
}

/*-------------------------------------------------------------
明朝人民有出版、结社、舆论、罢工罢市、游行示威的自由。

明朝除食盐外,所有工商业都是私营的,公平竞争,
官营手工业只为满足军队、皇宫的需求,不参与市场竞争,
本身就是为防止军队、皇宫扰民而设的。

只有明朝全面贯彻了儒家治国,按周礼的要求:
①复兴衣冠礼乐;
②普及教育,历代汉人王朝的基本国策,但明朝贯彻得最好,大明法律规定八岁不上学,父母有罪。;
③贯彻国野制精神,创立卫所制;
④重建以汉人为中心的华夷秩序;
⑤贯彻轻徭薄赋的精神,设立永不起科的祖制;
⑥贯彻不与民争利的精神,全面废除垄断国营经济;
⑦贯彻学而优则仕的精神,全面科举取仕,几乎所有文官都是科举出身,60%科举进士出身于三代没有出过做官的人的家族,政府向人民开放;
⑧保障人民的议政权和出版、言论自由,人民可以骂皇帝与内阁大臣,甚至造谣丑化皇帝都没人管,明朝没有出版审查;
⑨贯彻政务公开,财政收支公开,接受人民监督;⑩严格管制军队,防止军队暴力害民。

明朝是华夏文明的颠峰,也是中国国际地位最高的时代,
有148个藩属国,是当时独一无二的地球霸主。
明朝是民生最富裕、民权最有保证,
汉民族获得了包括欧洲人在内的世界各族人崇拜、景仰。
--------------------------------------------------------------*/
//-->
</script>

END

posted @ 2019-11-22 08:29  逆火狂飙  阅读(21)  评论(0)    收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东