• 欢迎来到本站,本站主要做程序员相关技术、博客、笔记,如果喜欢本网站那么赶紧使用Ctrl+D 收藏吧!
  • 如果有任何疑问,可以在下方文章下方留下你的评论~

Example of using HTML canvas strokeStyle property

生活日常 8个月前 (10-14) 2次浏览 0个评论

Browser support

Internet Explorer 9, Firefox, opera, chrome, and safari support the strokeStyle property.

Note: the <canvas> element is not supported in Internet Explorer 8 and earlier.

Definition and Usage

The strokestyle property sets or returns the color, gradient, or mode used for stroke.

JavaScript syntax:context.strokeStyle=color|gradient|pattern;

Example 1

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>pen pal(chinapenpal.com)</title>
</head>
<body>

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support HTML5 canvas tags.</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.strokeStyle="#FF0000";
ctx.strokeRect(20,20,150,100);
</script>

</body>
</html>

Example of using HTML canvas strokeStyle property

Example 2

Draw a rectangle. Use gradient stroke:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>pen pal in China</title>
</head>
<body>

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support HTML5 canvas tags.</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var gradient=ctx.createLinearGradient(0,0,170,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
//gradient stroke
ctx.strokeStyle=gradient;
ctx.lineWidth=5;
ctx.strokeRect(20,20,150,100);

</script>

</body>
</html>

Example of using HTML canvas strokeStyle property

Example 3

Use a gradient stroke to write the text "big smile!":

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Your pen pal</title>
</head>
<body>

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support HTML5 canvas tags
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Verdana";
// Create gradient
var gradient=ctx.createLinearGradient(0,0,c.width,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
// Gradient Fill
ctx.strokeStyle=gradient;
ctx.strokeText("Big smile!",10,50);
</script>

</body>
</html>

Example of using HTML canvas strokeStyle property


中国笔友, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Example of using HTML canvas strokeStyle property
喜欢 (0)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址