日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

canvas笔记-文字渲染

發布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 canvas笔记-文字渲染 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字體以及填充文字

程序運行截圖如下:

源碼如下:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">當前瀏覽器不支持canvas </canvas><script>window.onload = function(){let canvas = document.getElementById("canvas");canvas.width = 800;canvas.height = 800;let context = canvas.getContext("2d");context.font = "bold 40px Arial";context.fillStyle = "#058";context.fillText("中文ABC1234!@#¥%", 40, 100);}</script></body> </html>

其中:

context.font = "bold 40px Arial"; context.fillStyle = "#058"; context.fillText("中文ABC1234!@#¥%", 40, 100);

其中:font指定了加粗bold與字體大小為40px及Arial。

使用fillStyle指定了顏色為#058,使用fillText設置了文字內容,以及再40,100處進行畫文字。

?

如果使用描邊進行繪畫

源碼如下:

<script>window.onload = function(){let canvas = document.getElementById("canvas");canvas.width = 800;canvas.height = 800;let context = canvas.getContext("2d");context.font = "bold 40px Arial";context.fillStyle = "#058";context.fillText("中文ABC1234!@#¥%", 40, 100);context.strokeText("中文ABC1234!@#¥%", 40, 200);}</script>

程序運行截圖如下:

再fillText與strokeText包含第四個參數,這個參數的意義是限制其寬度:

如下代碼:

<script>window.onload = function(){let canvas = document.getElementById("canvas");canvas.width = 800;canvas.height = 800;let context = canvas.getContext("2d");context.font = "bold 40px Arial";context.fillStyle = "#058";context.fillText("中文ABC1234!@#¥%", 40, 100);context.strokeText("中文ABC1234!@#¥%", 40, 200);context.fillText("中文ABC1234!@#¥%", 40, 300, 100);context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);}</script>

程序運行截圖如下:

下面是使用線性梯度進行渲染下:

程序運行截圖如下:

源碼如下:

window.onload = function(){let canvas = document.getElementById("canvas");canvas.width = 800;canvas.height = 800;let context = canvas.getContext("2d");context.font = "bold 40px Arial";context.fillStyle = "#058";context.fillText("中文ABC1234!@#¥%", 40, 100);context.strokeText("中文ABC1234!@#¥%", 40, 200);context.fillText("中文ABC1234!@#¥%", 40, 300, 100);context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);let linearGrad = context.createLinearGradient(0, 0, 800, 0);linearGrad.addColorStop(0.0, "red");linearGrad.addColorStop(0.1, "black");linearGrad.addColorStop(0.2, "green");linearGrad.addColorStop(0.3, "yellow");linearGrad.addColorStop(0.4, "orange");linearGrad.addColorStop(0.5, "purple");linearGrad.addColorStop(0.6, "teal");linearGrad.addColorStop(0.7, "pink");linearGrad.addColorStop(1.0, "brown");context.fillStyle = linearGrad;context.fillText("中文ABC1234!@#¥%", 40, 500); }

?

下面是使用圖片紋理的例子:

程序運行截圖如下:

源碼如下:

<script>window.onload = function(){let canvas = document.getElementById("canvas");canvas.width = 800;canvas.height = 800;let context = canvas.getContext("2d");context.font = "bold 40px Arial";context.fillStyle = "#058";context.fillText("中文ABC1234!@#¥%", 40, 100);context.strokeText("中文ABC1234!@#¥%", 40, 200);context.fillText("中文ABC1234!@#¥%", 40, 300, 100);context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);let linearGrad = context.createLinearGradient(0, 0, 800, 0);linearGrad.addColorStop(0.0, "red");linearGrad.addColorStop(0.1, "black");linearGrad.addColorStop(0.2, "green");linearGrad.addColorStop(0.3, "yellow");linearGrad.addColorStop(0.4, "orange");linearGrad.addColorStop(0.5, "purple");linearGrad.addColorStop(0.6, "teal");linearGrad.addColorStop(0.7, "pink");linearGrad.addColorStop(1.0, "brown");context.fillStyle = linearGrad;context.fillText("中文ABC1234!@#¥%", 40, 500);let bakgroundImage = new Image();bakgroundImage.src = "img/bg.jpg";bakgroundImage.onload = function(){let pattern = context.createPattern(bakgroundImage, "repeat");context.fillStyle = pattern;context.font = "bold 60px Arial";context.fillText("中文ABC1234!@#¥%", 40, 600);}}</script>

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的canvas笔记-文字渲染的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。