Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML 画布 文本颜色


HTML 画布文本颜色

要设置画布上文本的颜色,我们使用两个属性

  • fillStyle - 定义文本的填充颜色
  • strokeStyle - 定义文本轮廓的颜色

fillStyle 属性

fillStyle 属性定义文本的填充颜色。

示例

将字体设置为 50px "Arial"。将填充颜色设置为紫色。在画布上写下填充的文本。从位置 (10,80) 开始

你的浏览器不支持 HTML5 canvas 标签。
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.font = "50px Arial";
ctx.fillStyle = "purple";
ctx.fillText("Hello World",10,80);
</script>
自己尝试 »


strokeStyle 属性

strokeStyle 属性定义文本轮廓的颜色。

示例

将字体设置为 50px "Arial"。将轮廓颜色设置为紫色。在画布上写下轮廓的文本。从位置 (10,80) 开始

你的浏览器不支持 HTML5 canvas 标签。
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.font = "50px Arial";
ctx.strokeStyle = "purple";
ctx.strokeText("Hello World",10,80);
</script>
自己尝试 »

使用渐变填充文本

示例

这里我们使用渐变填充文本

你的浏览器不支持 HTML5 canvas 标签。
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");

// 创建线性渐变
const grad=ctx.createLinearGradient(0,0,280,0);
grad.addColorStop(0, "lightblue");
grad.addColorStop(1, "darkblue");

// 使用渐变填充文本
ctx.font = "50px Arial";
ctx.fillStyle = grad;
ctx.fillText("Hello World",10,80);
</script>
自己尝试 »

使用渐变填充轮廓文本

示例

这里我们使用渐变填充轮廓文本

你的浏览器不支持 HTML5 canvas 标签。
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");

// 创建线性渐变
const grad=ctx.createLinearGradient(0,0,280,0);
grad.addColorStop(0, "lightblue");
grad.addColorStop(1, "darkblue");

// 使用渐变填充轮廓文本
ctx.font = "50px Arial";
ctx.strokeStyle = grad;
ctx.strokeText("Hello World",10,80);
</script>
自己尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.