Last active
September 2, 2019 07:05
-
-
Save mdecourse/b51f4d2a76e394421057a70e7d281b66 to your computer and use it in GitHub Desktop.
ROC Flag in HTML Canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
其他應用, 可以直接在網際繪製來自平面機構分析 (Solvespace) 所得到的座標點位置, 例如: | |
http://cpgx.kmol.info/2016fallcadp_ag100/blog/git-submodule-zai-dian-nao-fu-zhu-she-ji-shang-de-ying-yong.html | |
也可直接在網際解出 Geometric Constrain Solver, 直接根據使用者輸入, 進行平面機構的模擬分析, 例如: | |
http://cpgx.kmol.info/2016fallcadp_ag100/blog/2016fall-ping-mian-ji-gou-mo-ni.html | |
其他參考資料: https://github.com/google/cassowary.dart | |
https://github.com/google/codemirror.dart | |
https://github.com/google/charted | |
https://github.com/google/vector_math.dart | |
https://github.com/google/dart-gif-encoder | |
--> | |
<div id="wrapper"> | |
<canvas id="canvas" width="300" height="200"></canvas> | |
<button id="button">Clear Canvas</button> | |
<button id="roc">Draw ROC</button> | |
<button id="usa">Draw USA</button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:html'; | |
import 'dart:math' as Math; | |
CanvasElement canvas; | |
CanvasRenderingContext2D ctx; | |
int flag_w = 300; | |
int flag_h = 200; | |
num circle_x = flag_w / 4; | |
num circle_y = flag_h / 4; | |
void main() { | |
canvas = querySelector('#canvas'); | |
ctx = canvas.getContext('2d'); | |
drawROC(ctx); | |
querySelector("#roc").onClick.listen((e) => drawROC(ctx)); | |
querySelector("#usa").onClick.listen((e) => drawUSA(ctx)); | |
querySelector("#button").onClick.listen((e) => clearCanvas()); | |
} | |
void drawUSA(ctx){ | |
// 請畫出美國國旗 | |
ctx.clearRect(0, 0, flag_w, flag_h); | |
ctx.font = "30px Arial"; | |
ctx.strokeStyle = 'rgb(255, 0, 0)'; | |
ctx.strokeText("請畫出美國國旗", flag_w/6, flag_w/4); | |
} | |
void drawROC(ctx){ | |
// 先畫滿地紅 | |
ctx.clearRect(0, 0, flag_w, flag_h); | |
ctx.fillStyle = 'rgb(255, 0, 0)'; | |
ctx.fillRect(0, 0, flag_w, flag_h); | |
// 再畫青天 | |
ctx.fillStyle = 'rgb(0, 0, 150)'; | |
ctx.fillRect(0, 0, flag_w / 2, flag_h / 2); | |
// 畫十二道光芒白日 | |
ctx.beginPath(); | |
num star_radius = flag_w / 8; | |
num angle = 0; | |
for (int i = 0; i < 25; i++) { | |
angle += 5 * Math.pi * 2 / 12; | |
num toX = circle_x + Math.cos(angle) * star_radius; | |
num toY = circle_y + Math.sin(angle) * star_radius; | |
// 只有 i 為 0 時移動到 toX, toY, 其餘都進行 lineTo | |
if (i != 0) | |
ctx.lineTo(toX, toY); | |
else | |
ctx.moveTo(toX, toY); | |
} | |
ctx.closePath(); | |
// 將填色設為白色 | |
ctx.fillStyle = '#fff'; | |
ctx.fill(); | |
// 白日:藍圈 | |
ctx.beginPath(); | |
ctx.arc(circle_x, circle_y, flag_w * 17 / 240, 0, Math.pi * 2, true); | |
ctx.closePath(); | |
// 填色設為藍色 | |
ctx.fillStyle = 'rgb(0, 0, 149)'; | |
ctx.fill(); | |
// 白日:白心 | |
ctx.beginPath(); | |
ctx.arc(circle_x, circle_y, flag_w / 16, 0, Math.pi * 2, true); | |
ctx.closePath(); | |
// 填色設為白色 | |
ctx.fillStyle = '#fff'; | |
ctx.fill(); | |
} | |
void clearCanvas(){ | |
ctx.clearRect(0, 0, flag_w, flag_h); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
padding: 0; | |
} | |
#wrapper { | |
width: 300px; | |
margin: auto; | |
border: solid thin black; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment