HTML5Canvas如何實(shí)現(xiàn)文本對齊的方法

這篇文章主要介紹HTML5 Canvas如何實(shí)現(xiàn)文本對齊的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、靖安ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的靖安網(wǎng)站制作公司

水平對齊textAlign

JavaScript Code復(fù)制內(nèi)容到剪貼板

context.textAlign="center|end|left|right|start";

其中各值及意義如下表。

<!DOCTYPE html>   
<html lang="zh">   
<head>   
    <meta charset="UTF-8">   
    <title>textAlign</title>   
    <style>   
        body { background: url("./images/bg3.jpg") repeat; } 
        #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }   
    </style>   
</head>   
<body>   
<p id="canvas-warp">   
    <canvas id="canvas">   
        你的瀏覽器居然不支持Canvas?!趕快換一個吧!!   
    </canvas>   
</p>   
  
<script>   
    window.onload = function(){   
        var canvas = document.getElementById("canvas");   
        canvas.width = 800;   
        canvas.height = 600;   
        var context = canvas.getContext("2d");   
        context.fillStyle = "#FFF";   
        context.fillRect(0,0,800,600);   
  
        // 在位置 400 創(chuàng)建藍(lán)線   
        context.strokeStyle="blue";   
        context.moveTo(400,100);   
        context.lineTo(400,500);   
        context.stroke();   
  
  
        context.fillStyle = "#000";   
        context.font="50px Arial";   
  
        // 顯示不同的 textAlign 值   
        context.textAlign="start";   
        context.fillText("textAlign=start", 400, 120);   
        context.textAlign="end";   
        context.fillText("textAlign=end", 400, 200);   
        context.textAlign="left";   
        context.fillText("textAlign=left", 400, 280);   
        context.textAlign="center";   
        context.fillText("textAlign=center", 400, 360);   
        context.textAlign="right";   
        context.fillText("textAlign=right", 400, 480);   
    };   
</script>   
</body>   
</html>
描述
start默認(rèn)。文本在指定的位置開始。
end文本在指定的位置結(jié)束。
center文本的中心被放置在指定的位置。
left文本左對齊,
right文本右對齊。

我們通過一個例子來直觀的感受一下。

JavaScript Code復(fù)制內(nèi)容到剪貼板

<!DOCTYPE html>   
<html lang="zh">   
<head>   
    <meta charset="UTF-8">   
    <title>textBaseline</title>   
    <style>   
        body { background: url("./images/bg3.jpg") repeat; }  
        #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }   
    </style>   
</head>   
<body>   
<div id="canvas-warp">   
    <canvas id="canvas">   
        你的瀏覽器居然不支持Canvas?!趕快換一個吧!!   
    </canvas>   
</div>   
  
<script>   
    window.onload = function(){   
        var canvas = document.getElementById("canvas");   
        canvas.width = 800;   
        canvas.height = 600;   
        var context = canvas.getContext("2d");   
        context.fillStyle = "#FFF";   
        context.fillRect(0,0,800,600);   
  
        //在位置 y=300 繪制藍(lán)色線條   
        context.strokeStyle="blue";   
        context.moveTo(0,300);   
        context.lineTo(800,300);   
        context.stroke();   
  
        context.fillStyle = "#00AAAA";   
        context.font="20px Arial";   
  
        //在 y=300 以不同的 textBaseline 值放置每個單詞   
        context.textBaseline="top";   
        context.fillText("Top",150,300);   
        context.textBaseline="bottom";   
        context.fillText("Bottom",250,300);   
        context.textBaseline="middle";   
        context.fillText("Middle",350,300);   
        context.textBaseline="alphabetic";   
        context.fillText("Alphabetic",450,300);   
        context.textBaseline="hanging";   
        context.fillText("Hanging",550,300);   
    };   
</script>   
</body>   
</html>

運(yùn)行結(jié)果:

HTML5 Canvas如何實(shí)現(xiàn)文本對齊的方法

垂直對齊textBaseline

JavaScript Code復(fù)制內(nèi)容到剪貼板

context.textBaseline="alphabetic|top|hanging|middle|ideographic|bottom";

其中各值及意義如下表。

描述
alphabetic默認(rèn)。文本基線是普通的字母基線。
top文本基線是em方框的頂端。
hanging文本基線是懸掛基線。
middle文本基線是em方框的正中。
ideographic文本基線是表意基線。
bottom文本基線是em方框的底端。


首先咱們通過一個圖來看一下各個基線代表的位置。
HTML5 Canvas如何實(shí)現(xiàn)文本對齊的方法

我們通過一個例子來直觀的感受一下。

JavaScript Code復(fù)制內(nèi)容到剪貼板

<!DOCTYPE html>   
<html lang="zh">   
<head>   
    <meta charset="UTF-8">   
    <title>textBaseline</title>   
    <style>   
        body { background: url("./images/bg3.jpg") repeat; } 
        #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }   
    </style>   
</head>   
<body>   
<p id="canvas-warp">   
    <canvas id="canvas">   
        你的瀏覽器居然不支持Canvas?!趕快換一個吧!!   
    </canvas>   
</p>   
  
<script>   
    window.onload = function(){   
        var canvas = document.getElementById("canvas");   
        canvas.width = 800;   
        canvas.height = 600;   
        var context = canvas.getContext("2d");   
        context.fillStyle = "#FFF";   
        context.fillRect(0,0,800,600);   
  
        //在位置 y=300 繪制藍(lán)色線條   
        context.strokeStyle="blue";   
        context.moveTo(0,300);   
        context.lineTo(800,300);   
        context.stroke();   
  
        context.fillStyle = "#00AAAA";   
        context.font="20px Arial";   
  
        //在 y=300 以不同的 textBaseline 值放置每個單詞   
        context.textBaseline="top";   
        context.fillText("Top",150,300);   
        context.textBaseline="bottom";   
        context.fillText("Bottom",250,300);   
        context.textBaseline="middle";   
        context.fillText("Middle",350,300);   
        context.textBaseline="alphabetic";   
        context.fillText("Alphabetic",450,300);   
        context.textBaseline="hanging";   
        context.fillText("Hanging",550,300);   
    };   
</script>   
</body>   
</html>

運(yùn)行結(jié)果:
HTML5 Canvas如何實(shí)現(xiàn)文本對齊的方法

以上是HTML5 Canvas如何實(shí)現(xiàn)文本對齊的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章名稱:HTML5Canvas如何實(shí)現(xiàn)文本對齊的方法
本文鏈接:http://bm7419.com/article16/psspgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站營銷靜態(tài)網(wǎng)站、面包屑導(dǎo)航電子商務(wù)、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化