首页 技术 正文
技术 2022年11月20日
0 收藏 879 点赞 3,931 浏览 3535 个字

上文,写完弧度与贝塞尔曲线[js高手之路] html5 canvas系列教程 – arcTo(弧度与二次,三次贝塞尔曲线以及在线工具),本文主要是关于线条的样式设置

lineWidth: 设置线条的宽度,值是一个数值,如lineWidth = 5.

画3条不同宽度的线条:

 <style>
body {
background: #000;
}
#canvas {
background: white;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); oGc.beginPath();
oGc.lineWidth = 5;
oGc.strokeStyle = 'red';
oGc.moveTo( 200, 100 );
oGc.lineTo( 600, 100 );
oGc.stroke(); oGc.beginPath();
oGc.lineWidth = 10;
oGc.strokeStyle = 'orange';
oGc.moveTo( 200, 300 );
oGc.lineTo( 600, 300 );
oGc.stroke(); oGc.beginPath();
oGc.lineWidth = 20;
oGc.strokeStyle = '#09f';
oGc.moveTo( 200, 500 );
oGc.lineTo( 600, 500 );
oGc.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

lineWidth设置弧形的宽度:

 <style>
body {
background: #000;
}
#canvas {
background: white;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); oGc.lineWidth = 10;
oGc.arc( 300, 300, 200, 0, 270 * Math.PI / 180, false );
oGc.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

这段圆弧设置了lineWidth, 那么他的半径就等于lineWidth + 原来的半径

lineCap设置线条开始与结尾的线帽样式,有3个值

1,butt: 这是默认值,不加任何样式

2,round: 圆形

3,square: 正方形

 <style>
body {
background: #000;
}
#canvas {
background: white;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); oGc.lineWidth = 100;
oGc.beginPath();
oGc.strokeStyle = 'red';
oGc.lineCap = 'butt';
oGc.moveTo( 200, 100 );
oGc.lineTo( 600, 100 );
oGc.stroke(); oGc.beginPath();
oGc.strokeStyle = 'orange';
oGc.lineCap = 'round';
oGc.moveTo( 200, 300 );
oGc.lineTo( 600, 300 );
oGc.stroke(); oGc.beginPath();
oGc.strokeStyle = '#09f';
oGc.lineCap = 'square';
oGc.moveTo( 200, 500 );
oGc.lineTo( 600, 500 );
oGc.stroke(); oGc.beginPath();
oGc.lineWidth = 1;
oGc.strokeStyle = '#ccc';
oGc.moveTo( 200, 0 );
oGc.lineTo( 200, oCanvas.height );
oGc.stroke(); oGc.beginPath();
oGc.moveTo( 150, 0 );
oGc.lineTo( 150, oCanvas.height );
oGc.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

如果设置了线帽的样式( square, round ),线条就会加长,长出了多少?我在图中作了两条参考线,线条两边多出来的长度是线宽的一半。着这里就是 50,就是lineWidth=100的一半.

利用 lineWidth和lineCap属性写一个字母Z

 <style>
body {
background: #000;
}
#canvas {
background: white;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); oGc.lineWidth = 30;
oGc.lineCap = 'round';
oGc.moveTo( 100, 100 );
oGc.lineTo( 300, 100 );
oGc.lineTo( 100, 300 );
oGc.lineTo( 300, 300 );
oGc.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

你会发现,只有两个地方有lineCap样式,线的开始点和结束点。线条的连接处并没有加上lineCap样式

canvas为我们提供了lineJoin方法,可以设置线的连接处的样式,有3个值:

miter: 默认值,尖角

round: 圆角

bevel: 斜角

 oGc.lineWidth = 30;
oGc.lineCap = 'round';
oGc.lineJoin = 'round';
oGc.moveTo( 100, 100 );
oGc.lineTo( 300, 100 );
oGc.lineTo( 100, 300 );
oGc.lineTo( 300, 300 );
oGc.stroke();

加上lineJoin = ’round’ 就会变成圆角效果:

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

miter效果:

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

bevel效果

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

画虚线:

我们之前用moveTo, lineTo画的都是实线,canvas为我们提供了setLineDash()方法,它可以用来画虚线:

用法:

cxt.setLineDash( 数组 )

参数中这个数组,是由实线和空白组合合成,如:

[ 20, 5 ]: 20px 实线,5px空白

[ 20, 5, 10, 5]: 20px实线,5px空白, 10px实线, 5px空白

重复拼凑组合而成的线型.

 <style>
body {
background: #000;
}
#canvas {
background: white;
}
</style>
<script>
window.onload = function () {
var oCanvas = document.querySelector("#canvas"),
oGc = oCanvas.getContext('2d'); oGc.lineWidth = 5;
oGc.strokeStyle = '#09f';
oGc.beginPath();
oGc.setLineDash( [ 10, 5 ] );
oGc.moveTo( 100, 100 );
oGc.lineTo( 600, 100 );
oGc.stroke(); oGc.beginPath();
oGc.setLineDash( [ 20, 5, 10, 5 ] );
oGc.moveTo( 100, 150 );
oGc.lineTo( 600, 150 );
oGc.stroke(); oGc.beginPath();
oGc.setLineDash( [ 40, 5, 20, 5, 10, 1 ] );
oGc.moveTo( 100, 200 );
oGc.lineTo( 600, 200 );
oGc.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>

[js高手之路] html5 canvas系列教程 – 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,096
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765