【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();
        ctx.fillStyle = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();

        const R=210;// 基准尺寸

        // 最外边
        ctx.save();        
        ctx.shadowOffsetY=8; // 设置阴影
        ctx.shadowColor="grey";
        ctx.shadowBlur=6;
        var r=R*1.00;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(252,231,149)");
        gnt.addColorStop(0.25,"rgb(246,231,152)");
        gnt.addColorStop(0.50,"rgb(255,251,180)");
        gnt.addColorStop(0.6,"rgb(144,100,26)");
        gnt.addColorStop(0.75,"rgb(207,139,46)");
        gnt.addColorStop(1,"rgb(154,83,16)");
        ctx.fillStyle=gnt;
        drawRoundSixPolygon(ctx,0,0,r,R/20);
        ctx.fill();
        ctx.restore();

        // 平边
        ctx.save();        
        var r=R*0.98;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(224,194,112)");
        gnt.addColorStop(0.25,"rgb(239,221,142)");
        gnt.addColorStop(0.50,"rgb(254,248,174)");
        gnt.addColorStop(0.6,"rgb(154,91,19)");
        gnt.addColorStop(0.75,"rgb(221,157,64)");
        gnt.addColorStop(1,"rgb(164,99,25)");
        ctx.fillStyle=gnt;
        drawRoundSixPolygon(ctx,0,0,r,R/20);
        ctx.fill();
        ctx.restore();

        // 内边
        ctx.save();        
        var r=R*0.92;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(154,83,16)");
        gnt.addColorStop(0.25,"rgb(207,139,46)");
        gnt.addColorStop(0.50,"rgb(144,100,26)");
        gnt.addColorStop(0.6,"rgb(255,251,180)");
        gnt.addColorStop(0.75,"rgb(246,231,152)");
        gnt.addColorStop(1,"rgb(252,231,149)");
        ctx.fillStyle=gnt;
        drawRoundSixPolygon(ctx,0,0,r,R/20);
        ctx.fill();
        ctx.restore();

        // 渐变底
        ctx.save();        
        var r=R*0.90;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"black");
        gnt.addColorStop(1,"grey");
        ctx.fillStyle=gnt;
        drawRoundSixPolygon(ctx,0,0,r,R/20);
        ctx.fill();
        ctx.restore();        

        // 红黑底
        ctx.save();        
        var r=R*0.89;
        var gnt=ctx.createRadialGradient(0,0,0,0,0,r);
        gnt.addColorStop(0,"rgb(22,74,159)");
        gnt.addColorStop(1,"rgb(7,4,20)");
        ctx.fillStyle=gnt;
        drawRoundSixPolygon(ctx,0,0,r,R/20);
        ctx.fill();
        ctx.restore();

        // 诗正文
        var r=R*0.88;
        var color="rgb(251,223,159)";
        var sz=R/5.61;
        writeText(ctx,0,-r/4*2.5,"苔花",sz+"px 方正宋刻本秀楷简体",color);
        var sz=R/12.2;
        writeText(ctx,0,-r/4*2.0,"袁枚",sz+"px 方正宋刻本秀楷简体",color);
        // 诗歌正文
        var start=createPt(0,-r/3.5);
        var gap=r/3.5;
        var sz=R/6.6;
        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();        
        var r=R*0.88;

        var angle=-Math.PI/6;
        var a=createPt(r*Math.cos(angle),r*Math.sin(angle));

        angle=-Math.PI/2;
        var b=createPt(r*Math.cos(angle),r*Math.sin(angle));

        var e=createPt(a.x/2+b.x/2,a.y/2+b.y/2);

        angle=-Math.PI/6*5;
        var c=createPt(r*Math.cos(angle),r*Math.sin(angle));

        angle=-Math.PI-Math.PI/6;
        var d=createPt(r*Math.cos(angle),r*Math.sin(angle));        

        var f=createPt(c.x/2+d.x/2,c.y/2+d.y/2);

        var h=createPt(f.x/2+e.x/2,f.y/2+e.y/2);

        r=R*0.22;
        angle=-Math.PI/3*2;
        var g=createPt(h.x+r*Math.cos(angle),h.y+r*Math.sin(angle));        
        ctx.fillStyle="rgba(190,190,190,0.1)"; // 填充半透明色
        ctx.beginPath();
        ctx.moveTo(e.x,e.y);
        ctx.lineTo(b.x,b.y);
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(f.x,f.y);
        ctx.quadraticCurveTo(g.x,g.y,e.x,e.y);
        ctx.lineTo(e.x,e.y);
        ctx.closePath();
        ctx.fill();

        ctx.restore();


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

/*----------------------------------------------------------
函数:绘制正多边形
n:正多边形的边数
x:正多边形中心的横坐标
y:正多边形中心的纵坐标
r:正多边形中心到顶点的距离
----------------------------------------------------------*/
function drawPolygon(ctx,n,x,y,r){
    var polyArr=[];
    for(var i=0;i<n;i++){
        var theta=Math.PI*2/n*i+Math.PI/6;
        var pt={};
        pt.x=r*Math.cos(theta)+x;
        pt.y=r*Math.sin(theta)+y;
        polyArr.push(pt);
    }

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

/*----------------------------------------------------------
函数:用于绘制圆角六边形
ctx:绘图上下文
x:六边形中心横坐标
y:六边形中心纵坐标
R:六边形半径
r:圆角半径
----------------------------------------------------------*/
function drawRoundSixPolygon(ctx,x,y,R,r){
    // 取点
    const N=6;// 六边形边数
    const ROUND=r;// 圆角半径
    
    var arr=[];// 放顶点三点的数组
    for(var i=0;i<N;i++){
        var theta=Math.PI*2/N*i-Math.PI/6;

        var r=R;
        var a=createPt(x+r*Math.cos(theta),y+r*Math.sin(theta));

        r=ROUND/Math.sqrt(3);
        var angle=theta+Math.PI/3*2;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=ROUND/Math.sqrt(3);
        angle=theta-Math.PI/3*2;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        var arrInner=[b,a,c];
        arr.push(arrInner);
    }

    // 绘图
    ctx.save();
    ctx.beginPath();
    for(var i=0;i<arr.length;i++){
        var b=arr[i][0];
        var a=arr[i][1];
        var c=arr[i][2];

        ctx.lineTo(b.x,b.y);
        ctx.quadraticCurveTo(a.x,a.y,c.x,c.y);
        ctx.lineTo(c.x,c.y);
    }
    ctx.closePath();
    ctx.restore();
}


/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
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、付费专栏及课程。

余额充值