【成圖】

【Canvas與旗幟】烏克蘭旗圓盤_canvas

【Canvas與旗幟】烏克蘭旗圓盤_canvas_02

【Canvas與旗幟】烏克蘭旗圓盤_旗幟_03

【Canvas與旗幟】烏克蘭旗圓盤_canvas_04

【Canvas與旗幟】烏克蘭旗圓盤_旗幟_05

【代碼】

<!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 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 = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();*/
        
        const R=220;//基準尺寸
        var ct=createPt(0,0);// ct=center

        // #1 底圓
        ctx.save();    
        var r=R*1.00;
        var left=createPt3(ct,r,Math.PI);
        var right=createPt3(ct,r,0);
        var gnt=ctx.createLinearGradient(left.x,left.y,right.x,right.y);
        gnt.addColorStop(0,"rgb(248,214,2)");
        gnt.addColorStop(1,"rgb(233,188,0)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(ct.x,ct.y,r,0,Math.PI,false);
        ctx.closePath();
        ctx.fill();
        var gnt=ctx.createLinearGradient(left.x,left.y,right.x,right.y);
        gnt.addColorStop(0,"rgb(0,107,187)");
        gnt.addColorStop(1,"rgb(0,94,140)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(ct.x,ct.y,r,Math.PI,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();    
        
        // #2 灰圈
        ctx.save();    
        var r=R*0.95;        
        ctx.lineWidth=R/220*4;
        ctx.strokeStyle="rgba(240,240,240,0.8)";
        ctx.beginPath();
        ctx.arc(ct.x,ct.y,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.stroke();
        ctx.restore();
        
        // #3 玻璃光
        ctx.save();    
        var r=R*0.93;        
        ctx.fillStyle="rgba(240,240,240,0.4)";
        ctx.beginPath();
        ctx.arc(ct.x,ct.y,r,Math.PI-Math.PI/6,-Math.PI/6,false);
        var omega=createPt3(ct,r,Math.PI/3);
        ctx.arc(omega.x,omega.y,r*Math.sqrt(2),-Math.PI/12*5,Math.PI+Math.PI/12,true);
        ctx.closePath();
        ctx.fill();
        ctx.restore();
        
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火製圖","8px consolas","lightgrey");// 版權
    }
}


/*------------------------------------------------------------------------
 常用函數:drawEllipse函數的套娃函數
 ctx:繪圖上下文
 x:橢圓中心點橫座標
 y:橢圓中心點縱座標
 width:橢圓寬
 height:橢圓高
------------------------------------------------------------------------*/
function drawTuoYuan(ctx,x,y,width,height){
    drawEllipse(ctx,x-width/2,y-height/2,width,height);
}

/*------------------------------------------------------------------------
 常用函數:使用貝塞爾三次曲線擬近橢圓,
        該方法比原生的ellipse函數消耗小很多。
 ctx:繪圖上下文
 x:橢圓左極點橫座標(注意不是中心點)
 y:橢圓左極點縱座標(注意不是中心點)
 width:橢圓寬
 height:橢圓高
 注:該方法摘錄自 張磊著《HTML5實驗室-Canvas世界》,電子工業出版社出版
------------------------------------------------------------------------*/
function drawEllipse(ctx,x,y,width,height){
    var k=0.55228475;
    var ox=(width/2)*k;
    var oy=(height/2)*k;
    var xe=x+width;
    var ye=y+height;
    var xm=x+width/2;
    var ym=y+height/2;

    ctx.beginPath();
    ctx.moveTo(x,ym);
    ctx.bezierCurveTo(x,ym-oy,xm-ox,y,xm,y);
    ctx.bezierCurveTo(xm+ox,y,xe,ym-oy,xe,ym);
    ctx.bezierCurveTo(xe,ym+oy,xm+ox,ye,xm,ye);
    ctx.bezierCurveTo(xm-ox,ye,x,ym+oy,x,ym);
    ctx.closePath();
}

/*----------------------------------------------------------
基礎函數:用於從角度取得弧度,便於微調
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:扇形圓心縱座標
rIn:扇形內徑
rOut:扇形外徑
startAngle:起始角度
endAngle:中止角度
----------------------------------------------------------*/
function drawFan(ctx,x,y,rIn,rOut,startAngle,endAngle){
    var a=createPt(x+rIn*Math.cos(startAngle),y+rIn*Math.sin(startAngle));
    var b=createPt(x+rOut*Math.cos(startAngle),y+rOut*Math.sin(startAngle));
    var c=createPt(x+rOut*Math.cos(endAngle),y+rOut*Math.sin(endAngle));
    var d=createPt(x+rIn*Math.cos(endAngle),y+rIn*Math.sin(endAngle));
    ctx.beginPath();
    ctx.moveTo(a.x,a.y);
    ctx.lineTo(b.x,b.y);
    ctx.arc(x,y,rOut,startAngle,endAngle,false);
    ctx.lineTo(c.x,c.y);
    ctx.lineTo(d.x,d.y);
    ctx.arc(x,y,rIn,endAngle,startAngle,true);
    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();
}

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

/*-------------------------------------------------------------
有日月朝暮懸,有鬼神掌着生死權。
天地也!只合把清濁分辨,可怎生糊突了盜跖、顏淵?
為善的受貧窮更命短,造惡的享富貴又壽延。
天地也!做得個怕硬欺軟,卻原來也這般順水推船!
地也,你不分好歹何為地!
天也,你錯勘賢愚枉做天!
哎,只落得兩淚漣漣。
--------------------------------------------------------------*/
//-->
</script>

END