【成圖】

【Canvas與文字】數列與級數橫匾_canvas

【代碼】

<!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=360;
const HEIGHT=200;

// 舞台對象
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();
        
        var ct=createPt(0,0);// ct=center

        // #1 
        ctx.save();    
        var w=WIDTH-10;
        var h=HEIGHT-10;
        var r=w/2;
        var top=createPt3(ct,r,-Math.PI/2);
        var bottom=createPt3(ct,r,Math.PI/2);
        ctx.fillStyle="black";
        drawRect(ctx,ct.x,ct.y,w,h);
        ctx.fill();
        ctx.restore();

        // #2 
        ctx.save();            
        h-=r*0.01;
        w-=r*0.01;
        var top=createPt3(ct,r,-Math.PI/2);
        var bottom=createPt3(ct,r,Math.PI/2);
        var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y);
        gnt.addColorStop(0,"rgb(255,255,255)");
        gnt.addColorStop(1,"rgb(206,206,206)");
        ctx.fillStyle=gnt;
        drawRect(ctx,ct.x,ct.y,w,h);
        ctx.fill();
        ctx.restore();

        // #3 
        ctx.save();            
        h-=r*0.08;
        w-=r*0.08;
        var top=createPt3(ct,r,-Math.PI/2);
        var bottom=createPt3(ct,r,Math.PI/2);
        var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y);
        gnt.addColorStop(0,"black");
        gnt.addColorStop(1,"lightgrey");
        ctx.fillStyle=gnt;
        drawRect(ctx,ct.x,ct.y,w,h);
        ctx.fill();
        ctx.restore();

        // #4 底色
        ctx.save();            
        h-=r*0.01;
        w-=r*0.01;
        var top=createPt3(ct,r,-Math.PI/2);
        var bottom=createPt3(ct,r,Math.PI/2);
        var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y);
        gnt.addColorStop(0,"rgb(243,96,0)");
        gnt.addColorStop(1,"rgb(255,188,0)");
        ctx.fillStyle=gnt;
        drawRect(ctx,ct.x,ct.y,w,h);
        ctx.fill();
        ctx.restore(); 
        
        // #5
        ctx.save();
        var text="數列與級數";
        writeText(ctx,ct.x,ct.y+r*0.16,text,r*0.36+"px Noto Sans SC Black","black");
        writeText(ctx,ct.x,ct.y+r*0.15,text,r*0.36+"px Noto Sans SC Black","white");
        ctx.restore();

        //writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火製圖","8px consolas","lightgrey");// 版權
    }
}

/*----------------------------------------------------------
基礎函數:用於從角度取得弧度,便於微調
degree:角度值,如30,60,90
返回值:弧度值,如PI/6,PI/3,PI/2
----------------------------------------------------------*/
function getRadian(degree){
    return degree/180*Math.PI;
}

/*----------------------------------------------------------
基礎函數:用於取得兩點間距離
p1:起點
p1:終點
----------------------------------------------------------*/
function getDistance(p1,p2){
    return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

/*----------------------------------------------------------
基礎函數:用於繪製矩形
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();
}

/*----------------------------------------------------------
基礎函數:創建一個二維座標點
base:基準點,入參必需有x和y兩參數
radius:當前點到基準點的距離
theta:當前點到基準點的角度
Pt即Point
----------------------------------------------------------*/
function createPt3(base,radius,theta){
    var retval={};
    retval.x=base.x+radius*Math.cos(theta);
    retval.y=base.y+radius*Math.sin(theta);
    return retval;
}

/*----------------------------------------------------------
基礎函數:創建一個二維座標點
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();
}

/*-------------------------------------------------------------
排名在前 1% 的高中生是靠天賦還是靠努力?

學習的本質是模仿,因此任何學科的學習沒有天賦一説,
最多隻有擅長與不擅長,基本上靠努力都可以,只是學到不同程度而已。

真正的天賦是三個階段:
初級階段是對這門科學有自己的理解,背書做題都是不同形式的模仿;
第二階段是發現這門學科有大量的錯誤或不完整,不管是初級知識或者像進化論及牛頓力學這樣的高級理論;
第三階段是經過以上的思考,猜測出有可能的正確答案,如果有可能在將來再去驗證它。

作者:雕刻時光
鏈接:https://www.zhihu.com/question/22164041/answer/1974439188565872755
來源:知乎
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
--------------------------------------------------------------*/
//-->
</script>

END