首页 > 其他分享 >【Canvas与诗词】《侠客行》节选

【Canvas与诗词】《侠客行》节选

时间:2024-09-21 13:22:11浏览次数:3  
标签:addColorStop gnt color 侠客行 ctx Canvas 节选 rgb var

【成图】

【Canvas与诗词】《侠客行》节选_诗词

【Canvas与诗词】《侠客行》节选_诗词_02

【Canvas与诗词】《侠客行》节选_侠客行_03

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>437.侠客行</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=180;// 基准尺寸

        // 竖向渐变外框
        ctx.save();
        var r=R*1.20;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(237,198,119)");
        gnt.addColorStop(0.2,"rgb(252,244,172)");
        gnt.addColorStop(0.4,"rgb(147,95,36)");
        gnt.addColorStop(0.5,"rgb(188,138,69)");
        gnt.addColorStop(0.6,"rgb(147,95,36)");
        gnt.addColorStop(0.8,"rgb(252,244,172)");
        gnt.addColorStop(1,  "rgb(237,198,119)");    
        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*1.16;// outer radius
        var ri=R*1.04;// inner radius
        var gnt=ctx.createLinearGradient(0,-ro,0,0);
        gnt.addColorStop(0,  "rgb(253,247,242)");
        gnt.addColorStop(0.5,"rgb(251,212,125)");
        gnt.addColorStop(0.95,"rgb(245,225,190)");
        gnt.addColorStop(1,  "rgb(76,38,17)");
        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*1.16;// outer radius
        var ri=R*1.04;// inner radius
        var gnt=ctx.createLinearGradient(0,0,0,ro);
        gnt.addColorStop(0,  "rgb(76,38,17)");
        gnt.addColorStop(1,"rgb(234,199,133)");
        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*1.04;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,  "rgb(186,132,46)");
        gnt.addColorStop(0.5,"rgb(240,232,155)");
        gnt.addColorStop(1,  "rgb(176,118,64)");    
        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*1.00;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,  "black");
        gnt.addColorStop(1,  "rgb(47,101,114)");    
        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.98;
        ctx.fillStyle="white";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // slash蒙版
        ctx.save();
        r=R*0.98;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,  "rgba(255,255,255,0.25)");
        gnt.addColorStop(0.4,  "rgba(0,0,0,0.95)");
        gnt.addColorStop(0.6,  "rgba(0,0,0,0.95)");
        gnt.addColorStop(1,  "rgba(255,255,255,0.25)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // backslash蒙版
        ctx.save();
        r=R*0.98;
        var gnt=ctx.createLinearGradient(r,-r,-r,r);
        gnt.addColorStop(0,  "rgba(255,255,255,0.35)");
        gnt.addColorStop(0.4,  "rgba(0,0,0,0.75)");
        gnt.addColorStop(0.6,  "rgba(0,0,0,0.75)");
        gnt.addColorStop(1,  "rgba(255,255,255,0.35)");
        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.96;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,  "black");
        gnt.addColorStop(1,  "rgb(47,101,114)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 诗正文
        var r=R*0.96;
        var color="rgb(251,223,159)";
        var sz=R/5.61;
        writeText(ctx,0,-r/4*2.5,"侠客行节选",sz+"32px 方正宋刻本秀楷简体",color);
        var sz=R/11.2;
        writeText(ctx,0,-r/4*2.0,"李白",sz+"16px 方正宋刻本秀楷简体",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);

        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

标签:addColorStop,gnt,color,侠客行,ctx,Canvas,节选,rgb,var
From: https://blog.51cto.com/u_7726611/12074180

相关文章