html5 canvas學習--繪製文字
1.繪製填充文字:context.fillText(text,x,y,[maxWidth])
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function draw(){
var ctx = document.getElementById("myCanvas").getContext("2d");
ctx.font = "italic 35px 黑體";
ctx.fillStyle = "Red";
ctx.fillText("繪製填充文字",30,60,200);
ctx.font = "bold 35px 宋體";
ctx.fillStyle = "blue";
ctx.fillText("繪製填充文字",30,100);
ctx.font = "25px 隸書";
ctx.fillStyle = "Green";
ctx.fillText("繪製填充文字",30,130);
}
window.onload = function(){
draw();
}
</script>
</head>
<body>
<canvas id="myCanvas" width="350" height="200" style="border:solid 1px gray;"></canvas>
</body>
</html>
2. 繪製輪廓文字:context.strokeText(text,x,y,[maxWidth]); 測量文字的長度:context.measureText(text);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function draw(){
var ctx = document.getElementById("myCanvas").getContext("2d");
ctx.font = "bold 20px 黑體";
ctx.fillStyle = "Blue";
var txt1 = "滾滾長江東逝水,浪花淘盡英雄";
ctx.fillText(txt1,10,40);
var txt2 = "以上字串的寬度為";
var mtx1 = ctx.measureText(txt1);
var mtx2 = ctx.measureText(txt2);
ctx.font = "bold 15px 宋體";
ctx.fillStyle = "Red";
ctx.fillText(txt2,10,80);
ctx.fillText(mtx1.width,mtx2.width,80);
var message = "這是文字";
// 繪製空心文字
ctx.font = "25px 隸書";
ctx.strokeStyle = "Green";
ctx.strokeText(message,30,130);
}
window.onload = function(){
draw();
}
</script>
</head>
<body>
<canvas id="myCanvas" width="350" height="200"></canvas>
</body>
</html>
相關文章
- canvas 繪製文字Canvas
- canvas 繪製文字詳解Canvas
- canvas繪製圖片drawImage學習Canvas
- html5中canvas繪製矩形HTMLCanvas
- canvas 按住滑鼠拖動 繪製文字Canvas
- html5中canvas繪製圓形HTMLCanvas
- html5中canvas繪製線段HTMLCanvas
- HTML5使用canvas繪製圖形HTMLCanvas
- html5中canvas線段繪製太陽花HTMLCanvas
- 使用 HTML5 Canvas 繪製的水滴效果HTMLCanvas
- HTML5 Canvas ( 矩形的繪製 ) rect, strokeRect, fillRectHTMLCanvas
- html5中canvas繪製貝塞爾曲線HTMLCanvas
- HTML5 Canvas使用路徑——繪製圓形HTMLCanvas
- canvas學習筆記-繪製矩形及路徑(一)Canvas筆記
- canvas核心技術-如何繪製圖片和文字Canvas
- HTML5 Canvas(實戰:繪製餅圖2 Tooltip)HTMLCanvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- html5中canvas貝塞爾曲線繪製菊花HTMLCanvas
- html5中的canvas繪製橢圓的方法HTMLCanvas
- canvas 繪製矩形Canvas
- canvas 繪製扇形Canvas
- 繪製SVG內容到Canvas的HTML5應用SVGCanvasHTML
- 小程式利用Canvas繪製圖片和豎排文字Canvas
- canvas 繪製線條Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas繪製直線Canvas
- HTML5 快速學習二 CanvasHTMLCanvas
- Flutter Canvas學習之繪圖篇FlutterCanvas繪圖
- Canvas 繪製雷達圖Canvas
- canvas繪製動畫的技巧Canvas動畫
- canvas 繪製立體圓環Canvas
- canvas 繪製圓角矩形Canvas
- canvas 繪製矩形缺角Canvas
- canvas 繪製雙線技巧Canvas
- canvas繪製風車效果Canvas
- canvas繪製笑臉表情Canvas
- canvas繪製太陽系Canvas