Canvas strokeStyle 属性
示例
用 strokeStyle = "red" 绘制一个矩形
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.strokeStyle = "red";
ctx.strokeRect(20, 20, 150, 100);
自己动手试一试 »
更多示例见下文。
描述
strokeStyle
属性用于设置或返回用于描边的颜色、渐变或图案。
默认值为 #000000(纯黑色)。
另请参阅
fillStyle 属性 (设置填充颜色/样式)
lineWidth 属性(设置线宽)
beginPath() 方法 (开始一个新路径)
moveTo() 方法 (将路径移动到指定点)
lineTo() 方法 (向路径添加一条线)
stroke() 方法 (绘制当前路径)
strokeRect() 方法 (绘制一个矩形)
语法
context.strokeStyle = color|gradient|pattern |
属性值
值 | 描述 | 试一试 |
---|---|---|
color | 一个 CSS 颜色值,表示绘图的描边颜色。默认值为 #000000 | 试一试 » |
渐变 | 用于创建渐变描边的渐变对象(线性或 径向) | |
pattern | 用于创建图案描边的图案对象 |
更多示例
示例
用渐变 strokeStyle 绘制一个矩形
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
// 创建渐变
const gradient = ctx.createLinearGradient(0, 0, 170, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
// 用渐变填充
ctx.strokeStyle = gradient;
ctx.lineWidth = 5;
ctx.strokeRect(20, 20, 150, 100);
自己动手试一试 »
示例
用渐变 strokeStyle 写出文本 "Big smile!"
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.font = "30px Verdana";
// 创建渐变
const gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
// 用渐变填充
ctx.strokeStyle = gradient;
ctx.strokeText("Big smile!", 10, 50);
自己动手试一试 »
浏览器支持
<canvas>
元素是 HTML5 标准(2014)。
stokestyle
在所有现代浏览器中都受支持
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 9-11 |
❮ Canvas 参考