javascript - 如何在 HTML Canvas 中使用不同颜色的文本

标签 javascript html canvas fonts

所以我试图让这个 Canvas 上有不同类型的文本,包括颜色。到目前为止,这是我所拥有的。

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="400" height="200" style=" border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
window.onload = function(){
     var canvas = document.getElementById("myCanvas");
     var context = canvas.getContext("2d");
     var imageObj = new Image();
     imageObj.onload = function(){
         context.drawImage(imageObj, 0, 0);
         context.font = "40pt Calibri";
         context.fillText("My TEXT!", 50, 100);
     };
     imageObj.src = "Assets/Background2.png"; 
};
</script>
</body>
</html>

最佳答案

每次要更改字体颜色时,只需使用fillStyle 方法即可:

context.fillStyle = 'red';

关于javascript - 如何在 HTML Canvas 中使用不同颜色的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357859/

相关文章:

javascript - 使用 Brad Birdsall 的 Swipe 2.0 在纯 JavaScript 中的 iOS 导航栏

javascript - JQuery - 单击添加和删除类

html - CSS 显示无 - 隐藏

html - 页脚和页面底部之间的空间

javascript - 将 HTML(用户定义的指令)插入 View - AngularJS

javascript - 对 highcharts 中的类别中的后代数据进行排序

javascript - 为什么 html 选项卡在设置为 active 时不显示其内容?

javascript - 使用 HTML5/Canvas/JavaScript 在浏览器中截屏

css - 如何从网站中提取 Canvas 动画创建器/触发器功能?

javascript - 在 Canvas 中创建复合剪辑