代碼
提交代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>慕課網(wǎng)Wiki</title>
<style>
#imooc{
border:1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="imooc">您的瀏覽器不支持 HTML5 canvas 標(biāo)簽</canvas>
<script>
const canvas = document.getElementById('imooc');
canvas.width=400;
canvas.height=240;
const ctx = canvas.getContext('2d');
function drawBtn(str, x, y ){
var w = ctx.measureText(str).width; // 獲取繪制文本的寬度
var h = parseInt(ctx.font)
ctx.fillStyle="#456795";
ctx.fillRect(x-10,y-10,w+20,h+20)
ctx.fillStyle="#fff";
ctx.textBaseline="hanging"
ctx.fillText(str,x,y)
}
ctx.font="16px 微軟雅黑";
drawBtn("慕課Wiki",40,40)
ctx.font="18px 微軟雅黑";
drawBtn("Imooc教程 Hello World", 40, 90)
ctx.font="20px 微軟雅黑";
drawBtn("確認(rèn)", 40, 140)
</script>
<body>
</html>
運(yùn)行結(jié)果