【Canvas与诗词】《登科后》唐.孟郊

【成图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>昔日龌龊不足夸</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();
        var bgColor=ctx.createRadialGradient(-WIDTH/2,-HEIGHT/2,0,-WIDTH/2,-HEIGHT/2,WIDTH);
        bgColor.addColorStop(0,"grey");
        bgColor.addColorStop(0.5,"rgb(6,13,41)");
        bgColor.addColorStop(1,"rgb(6,13,41)");
        ctx.fillStyle = bgColor;
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();

        // 画斜纹
        ctx.save();
        ctx.lineWidth=0.5;
        ctx.strokeStyle="grey";
        const ALPHA=Math.PI/6;
        const OFFSET=10;
        const N=Math.ceil(HEIGHT*(1+1/Math.sqrt(3))/OFFSET);
        for(var i=0;i<N;i++){
            var start=createPt(-WIDTH/2,-HEIGHT/2-HEIGHT/Math.sqrt(3)+i*OFFSET);
            var end=createPt(WIDTH/2,-HEIGHT/2+i*OFFSET);

            ctx.beginPath();
            ctx.moveTo(start.x,start.y);
            ctx.lineTo(end.x,end.y);
            ctx.stroke();
        }
        ctx.restore();
        
        const R=180;// 基准尺寸

        // 横向渐变外框
        ctx.save();
        var r=R*1.12;
        var gnt=ctx.createLinearGradient(-r,0,r,0);
        gnt.addColorStop(0,"rgb(151,124,0)");
        gnt.addColorStop(0.4,"gold");
        gnt.addColorStop(0.5,"white");
        gnt.addColorStop(0.6,"gold");
        gnt.addColorStop(1,"rgb(151,124,0)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();
        
        // 竖向渐变外框
        ctx.save();
        r=R*1.10;
        var gnt1=ctx.createLinearGradient(0,-r,0,r);
        gnt1.addColorStop(0,"rgb(151,124,0)");
        gnt1.addColorStop(0.45,"gold");
        gnt1.addColorStop(0.5,"white");
        gnt1.addColorStop(0.55,"gold");
        gnt1.addColorStop(1,"rgb(151,124,0)");
        ctx.fillStyle=gnt1;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 内圈一
        ctx.save();
        r=R*1.08;
        var gnt2=ctx.createLinearGradient(-r,-r,r,r);
        gnt2.addColorStop(0,"rgb(6,13,41)");
        gnt2.addColorStop(1,"rgb(48,63,82)");
        ctx.fillStyle=gnt2;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 内圈二
        ctx.save();
        r=R*1.04;
        var gnt2=ctx.createLinearGradient(-r,-r,r,r);
        gnt2.addColorStop(0,"rgb(48,63,82)");
        gnt2.addColorStop(1,"rgb(6,13,41)");
        ctx.fillStyle=gnt2;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 铜盘
        ctx.save();
        r=R;
        var gnt3=ctx.createLinearGradient(-r,-r,r,r);
        gnt3.addColorStop(0,"rgb(255,225,83)");
        gnt3.addColorStop(0.2,"rgb(255,225,83)");
        gnt3.addColorStop(0.3,"rgb(255,240,172)");
        gnt3.addColorStop(0.4,"rgb(255,225,83)");
        gnt3.addColorStop(1,"rgb(234,193,0)");
        ctx.fillStyle=gnt3;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 暗盈月圈
        ctx.save();
        var omega=createPt(R*Math.sqrt(2),R*Math.sqrt(2));
        ctx.fillStyle="rgb(255,211,6)";
        const ANGLE=Math.asin(0.25);
        ctx.beginPath();
        ctx.arc(omega.x,omega.y,2*R,Math.PI/4*5-2*ANGLE,Math.PI/4*5+2*ANGLE,false);
        ctx.arc(0,0,R,Math.PI/4-(Math.PI/2-ANGLE),Math.PI/4+(Math.PI/2-ANGLE),false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 诗正文
        var color="rgb(6,13,41)";
        var sz=R/5.6;
        writeText(ctx,0,-115,"登科后",sz+"32px 方正宋刻本秀楷简体",color);
        var sz=R/11.2;
        writeText(ctx,85,-95,"孟郊",sz+"16px 方正宋刻本秀楷简体",color);
        // 诗歌正文
        var start=createPt(0,-50);
        var gap=54;
        var sz=R/5.4;
        writeText(ctx,start.x,start.y,"昔日龌龊不足夸",sz+"px 方正宋刻本秀楷简体",color);
        writeText(ctx,start.x,start.y+gap,"今朝放荡思无涯",sz+"px 方正宋刻本秀楷简体",color);
        writeText(ctx,start.x,start.y+2*gap, "春风得意马蹄疾",sz+"px 方正宋刻本秀楷简体",color);
        writeText(ctx,start.x,start.y+3*gap,"一日看尽长安花",sz+"px 方正宋刻本秀楷简体",color);

        // 蒙版
        ctx.save();
        r=R;
        ctx.fillStyle="rgba(9,9,9,0.25)";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

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


/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值