关键词搜索

源码搜索 ×
×

canvas笔记-文字渲染

发布2020-06-02浏览955次

详情内容

字体以及填充文字

程序运行截图如下:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.font = "bold 40px Arial";
  18. context.fillStyle = "#058";
  19. context.fillText("中文ABC1234!@#¥%", 40, 100);
  20. }
  21. </script>
  22. </body>
  23. </html>

其中:

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

其中:font指定了加粗bold与字体大小为40px及Arial。

使用fillStyle指定了颜色为#058,使用fillText设置了文字内容,以及再40,100处进行画文字。

 

如果使用描边进行绘画

源码如下:

  1. <script>
  2. window.onload = function(){
  3. let canvas = document.getElementById("canvas");
  4. canvas.width = 800;
  5. canvas.height = 800;
  6. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  7. context.font = "bold 40px Arial";
  8. context.fillStyle = "#058";
  9. context.fillText("中文ABC1234!@#¥%", 40, 100);
  10. context.strokeText("中文ABC1234!@#¥%", 40, 200);
  11. }
  12. </script>

程序运行截图如下:

再fillText与strokeText包含第四个参数,这个参数的意义是限制其宽度:

如下代码:

  1. <script>
  2. window.onload = function(){
  3. let canvas = document.getElementById("canvas");
  4. canvas.width = 800;
  5. canvas.height = 800;
  6. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  7. context.font = "bold 40px Arial";
  8. context.fillStyle = "#058";
  9. context.fillText("中文ABC1234!@#¥%", 40, 100);
  10. context.strokeText("中文ABC1234!@#¥%", 40, 200);
  11. context.fillText("中文ABC1234!@#¥%", 40, 300, 100);
  12. context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);
  13. }
  14. </script>

程序运行截图如下:

下面是使用线性梯度进行渲染下:

程序运行截图如下:

源码如下:

  1. window.onload = function(){
  2. let canvas = document.getElementById("canvas");
  3. canvas.width = 800;
  4. canvas.height = 800;
  5. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  6. context.font = "bold 40px Arial";
  7. context.fillStyle = "#058";
  8. context.fillText("中文ABC1234!@#¥%", 40, 100);
  9. context.strokeText("中文ABC1234!@#¥%", 40, 200);
  10. context.fillText("中文ABC1234!@#¥%", 40, 300, 100);
  11. context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);
  12. let linearGrad = context.createLinearGradient(0, 0, 800, 0);
  13. linearGrad.addColorStop(0.0, "red");
  14. linearGrad.addColorStop(0.1, "black");
  15. linearGrad.addColorStop(0.2, "green");
  16. linearGrad.addColorStop(0.3, "yellow");
  17. linearGrad.addColorStop(0.4, "orange");
  18. linearGrad.addColorStop(0.5, "purple");
  19. linearGrad.addColorStop(0.6, "teal");
  20. linearGrad.addColorStop(0.7, "pink");
  21. linearGrad.addColorStop(1.0, "brown");
  22. context.fillStyle = linearGrad;
  23. context.fillText("中文ABC1234!@#¥%", 40, 500);
  24. }

 

下面是使用图片纹理的例子:

程序运行截图如下:

源码如下:

  1. <script>
  2. window.onload = function(){
  3. let canvas = document.getElementById("canvas");
  4. canvas.width = 800;
  5. canvas.height = 800;
  6. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  7. context.font = "bold 40px Arial";
  8. context.fillStyle = "#058";
  9. context.fillText("中文ABC1234!@#¥%", 40, 100);
  10. context.strokeText("中文ABC1234!@#¥%", 40, 200);
  11. context.fillText("中文ABC1234!@#¥%", 40, 300, 100);
  12. context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);
  13. let linearGrad = context.createLinearGradient(0, 0, 800, 0);
  14. linearGrad.addColorStop(0.0, "red");
  15. linearGrad.addColorStop(0.1, "black");
  16. linearGrad.addColorStop(0.2, "green");
  17. linearGrad.addColorStop(0.3, "yellow");
  18. linearGrad.addColorStop(0.4, "orange");
  19. linearGrad.addColorStop(0.5, "purple");
  20. linearGrad.addColorStop(0.6, "teal");
  21. linearGrad.addColorStop(0.7, "pink");
  22. linearGrad.addColorStop(1.0, "brown");
  23. context.fillStyle = linearGrad;
  24. context.fillText("中文ABC1234!@#¥%", 40, 500);
  25. let bakgroundImage = new Image();
  26. bakgroundImage.src = "img/bg.jpg";
  27. bakgroundImage.onload = function(){
  28. let pattern = context.createPattern(bakgroundImage, "repeat");
  29. context.fillStyle = pattern;
  30. context.font = "bold 60px Arial";
  31. context.fillText("中文ABC1234!@#¥%", 40, 600);
  32. }
  33. }
  34. </script>

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载