首页 技术 正文
技术 2022年11月17日
0 收藏 587 点赞 2,741 浏览 2127 个字

绘制曲线,经常会用到路径的知识,如果你对路径有疑问,可以参考我的这篇文章[js高手之路] html5 canvas系列教程 – 开始路径beginPath与关闭路径closePath详解.

arc:画弧度

cxt.arc( x, y, 半径, 开始角度,结束角度,是否逆时针 );

x, y: 为弧度的中心横坐标和纵坐标,如果这是画一个圆.那么x,y就是圆的圆心.

开始角度与结束角度都是以弧度单位,弧度与角度的换算关系为: 弧度=角度*(π/180°)。

以时钟为参考,3点钟方向为0度,6点钟方向为90度,9点钟方向为180度,12点钟方向为270度.

第五个参数:true为逆时针,false为顺时针,默认值为false

在canvas的中心,换一个从0度方向开始,逆时针到270度方向的一段圆弧:

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

如果是顺时针,就用这段:

oGc.arc( width / 2, height / 2, height / 2, 0, 270 * Math.PI / 180, false ); [js高手之路] html5 canvas系列教程 – arc绘制曲线图形(曲线,弧线,圆形)                                [js高手之路] html5 canvas系列教程 – arc绘制曲线图形(曲线,弧线,圆形)

如果采用闭合路径,弧度的起始点就会相连

 oGc.arc( width / 2, height / 2, height / 2, 0, 270 * Math.PI / 180, true );
oGc.closePath();
oGc.stroke();
 oGc.arc( width / 2, height / 2, height / 2, 0, 270 * Math.PI / 180, false );
oGc.closePath();
oGc.stroke();

[js高手之路] html5 canvas系列教程 – arc绘制曲线图形(曲线,弧线,圆形)                           [js高手之路] html5 canvas系列教程 – arc绘制曲线图形(曲线,弧线,圆形)

画两个不同颜色的圆形:

 <style>
body {
background: #000;
}
#canvas{
background:white;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ); oGc.beginPath();
oGc.strokeStyle = '#09f';
oGc.arc( 150, 150, 150, 0, 360 * Math.PI / 180 );
oGc.closePath();
oGc.stroke(); oGc.beginPath();
oGc.strokeStyle = 'orange';
oGc.arc( 450, 150, 150, 0, 360 * Math.PI / 180 );
oGc.closePath();
oGc.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
</body>

画两个填充圆形,把上面的例子,stoke方式改成fill方式即可.

     oGc.beginPath();
oGc.fillStyle = '#09f';
oGc.arc( 150, 150, 150, 0, 360 * Math.PI / 180 );
oGc.closePath();
oGc.fill(); oGc.beginPath();
oGc.fillStyle = 'orange';
oGc.arc( 450, 150, 150, 0, 360 * Math.PI / 180 );
oGc.closePath();
oGc.fill();

画一个圆角:

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

[js高手之路] html5 canvas系列教程 – arc绘制曲线图形(曲线,弧线,圆形)

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