html5 canvas ( 文字的書寫和樣式控制 ) font, fillText, strokeText


<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css"> *{ margin: 0; padding: 0; outline: none; border: none; } #canvas{ width: 7rem; margin: .25rem 0 0 1.5rem; border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="1000" height="600"></canvas> </body> </html> <script type="text/javascript"> /** * rem 布局初始化 */ $('html').css('font-size', $(window).width()/10); /** * 獲取 canvas 畫布 * 獲取 canvas 繪圖上下文環境 */ var canvas = $('#canvas')[0]; var cxt = canvas.getContext('2d'); /** * 文字的書寫 * fillText(要寫的文字, 文字開始的橫坐標, 文字開始的縱坐標, 文字占用的最長寬度) * strokeText(要寫的文字, 文字開始的橫坐標, 文字開始的縱坐標, 文字占用的最長寬度) * font 字體大小和樣式 */ /* * font參數的值分為 * font-style: normal(正常), italic(斜體字), oblique(傾斜字體) 后兩種在網頁端一般沒什么區別 * font-variant: normal(正常), small-caps(英文小寫字母變成小的大寫) * font-weight: normal(正常), bold(加粗) 100-900(一般不用) * font-size: 文字大小 * font-family: 字體樣式 */ cxt.font = "oblique small-caps bold 50px Arial"; cxt.fillStyle = "blue"; cxt.fillText("Hello Canvas!" ,100, 100, 200); cxt.strokeStyle = 'blue'; cxt.strokeText("Hello Canvas!", 100, 300, 200); </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM