HTML5高阶实例之canvas绘制圆弧

最新更新时间:2019年07月14日14:18:12
《猛戳-查看我的博客地图-总有你意想不到的惊喜》

本文内容:canvas 绘制圆弧

绘制任意角度的纯色圆弧

模板骨架

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>canvas demo</title>
    <script type="text/javascript">
      function draw(){
        var canvas = document.getElementById('myCanvas');//返回DOM对象
        if (canvas.getContext){
          var ctx = canvas.getContext('2d');//返回canvas 的上下文
        }
      }
    </script>
</head>
<body onload="draw();">
	<canvas id="myCanvas" width="300" height="300"></canvas>
</body>
<scritp></script>
</html>

初始化并绘制30°圆弧

function initCanvas(){
	let canvas = document.getElementById("myCanvas")
    this.ctx = canvas.getContext("2d");
    let width = 140,height = 140;
    canvas.width = width;//等同于写在组件 width={canvaswidth}
    canvas.height = height;//等同于写在组件 height={canvasheight}
    //移动端毛边优化和模糊优化-借鉴于echarts
	if (window.devicePixelRatio) {
		canvas.style.width = width + "px";
		canvas.style.height = height + "px";
		canvas.height = height * window.devicePixelRatio;
		canvas.width = width * window.devicePixelRatio;
		this.ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
	}
	//创建背景圆
	this.ctx.lineWidth = 12;//线宽
	this.ctx.lineCap = 'round';//圆弧顶端弧面
	this.ctx.beginPath();//开始一个新的路径 x*(36/180)*Math.PI
    this.ctx.arc(70, 70, 60, -Math.PI/2, Math.PI/6 - Math.PI/2, false);//绘制30°圆弧context.arc(圆心的x坐标,圆心的y坐标,半径,起始角度,终止角度,false顺时针/true逆时针)
    this.ctx.strokeStyle = 'rgba(252,255,152, 1)';
    this.ctx.stroke();//对当前路径进行描边
}

几点说明:

  • lineCap 线段末端的属性,butt-线段末端以方形结束,round-线段末端以圆形结束,square-线段末端以方形结束,但是增加了一个宽度和线段相同,高度是线段厚度一半的矩形区域。
  • arc 绘制圆弧路径的方法,起始角度-Math.PI/2为十二点钟(零点)方向

在这里插入图片描述

绘制任意角度的渐变色圆弧

初始化并绘制270°圆弧

function initCanvas(){
	let canvas = document.getElementById("myCanvas")
    this.ctx = canvas.getContext("2d");
    let width = 140,height = 140;
    canvas.width = width;//等同于写在组件 width={canvaswidth}
    canvas.height = height;//等同于写在组件 height={canvasheight}
    //移动端毛边优化和模糊优化-借鉴于echarts
	if (window.devicePixelRatio) {
		canvas.style.width = width + "px";
		canvas.style.height = height + "px";
		canvas.height = height * window.devicePixelRatio;
		canvas.width = width * window.devicePixelRatio;
		this.ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
	}
	//创建背景圆
	this.ctx.lineWidth = 12;//线宽
	this.ctx.lineCap = 'butt';
	let g = this.ctx.createLinearGradient(30*Math.sqrt(2), 0, 0, 30*Math.sqrt(2));
    g.addColorStop(0, 'rgba(252,255,152, 1)');
    g.addColorStop(1, 'rgba(252,0,0, 1)');
    this.ctx.strokeStyle = g;
	this.ctx.beginPath();//开始一个新的路径 x*(36/180)*Math.PI
    this.ctx.arc(70, 70, 60, -Math.PI/2, Math.PI*3/2 - Math.PI/2, false);
    this.ctx.stroke();//对当前路径进行描边
}

半径为60°的圆弧,渐变色x轴和y轴的起点和终点的值如下表:

坐标点名称30°90°180°270°360°
渐变色x起点--30*Math.sqrt(2)30*Math.sqrt(2)-
渐变色y起点--00-
渐变色x终点--00-
渐变色y终点--30*Math.sqrt(2)*230*Math.sqrt(2)-
圆弧路径起始坐标--–Math.PI/2-Math.PI/2-
圆弧路径终点坐标--Math.PI - Math.PI/2Math.PI*3/2 - Math.PI/2-

在这里插入图片描述

绘制360°双色圆弧
function initCanvas(){
	let canvas = document.getElementById("myCanvas")
    this.ctx = canvas.getContext("2d");
    let width = 140,height = 140;
    canvas.width = width;//等同于写在组件 width={canvaswidth}
    canvas.height = height;//等同于写在组件 height={canvasheight}
    //移动端毛边优化和模糊优化-借鉴于echarts
	if (window.devicePixelRatio) {
		canvas.style.width = width + "px";
		canvas.style.height = height + "px";
		canvas.height = height * window.devicePixelRatio;
		canvas.width = width * window.devicePixelRatio;
		this.ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
	}
	//创建背景圆
	this.ctx.lineWidth = 12;//线宽
	this.ctx.lineCap = 'butt';
	
	this.ctx.strokeStyle = 'rgba(252,255,152, 1)';
    this.ctx.beginPath();
    this.ctx.arc(70, 70, 60, -Math.PI/2, Math.PI - Math.PI/2, false);
    this.ctx.stroke();

    this.ctx.strokeStyle = 'rgba(255,105,180, 1)';
    this.ctx.beginPath();
    this.ctx.arc(70, 70, 60, Math.PI/2, Math.PI*2 - Math.PI/2, false);
    this.ctx.stroke();
}

在这里插入图片描述

绘制360°三色圆弧
function initCanvas(){
	let canvas = document.getElementById("myCanvas")
    this.ctx = canvas.getContext("2d");
    let width = 140,height = 140;
    canvas.width = width;//等同于写在组件 width={canvaswidth}
    canvas.height = height;//等同于写在组件 height={canvasheight}
    //移动端毛边优化和模糊优化-借鉴于echarts
	if (window.devicePixelRatio) {
		canvas.style.width = width + "px";
		canvas.style.height = height + "px";
		canvas.height = height * window.devicePixelRatio;
		canvas.width = width * window.devicePixelRatio;
		this.ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
	}
	//创建背景圆
	this.ctx.lineWidth = 12;//线宽
	this.ctx.lineCap = 'butt';
	
	this.ctx.strokeStyle = 'rgba(252,255,152, 1)';
    this.ctx.beginPath();
    this.ctx.arc(70, 70, 60, -Math.PI/2, Math.PI*2/3 - Math.PI/2, false);
    this.ctx.stroke();

    this.ctx.strokeStyle = 'rgba(255,105,180, 1)';
    this.ctx.beginPath();
    this.ctx.arc(70, 70, 60, -Math.PI/2 + Math.PI*2/3, Math.PI*4/3 - Math.PI/2, false);
    this.ctx.stroke();

    this.ctx.strokeStyle = 'rgba(0,255,0, 1)';
    this.ctx.beginPath();
    this.ctx.arc(70, 70, 60, Math.PI*4/3 - Math.PI/2, Math.PI*2 - Math.PI/2, false);
    this.ctx.stroke();
}

在这里插入图片描述

以动画形式绘制圆弧
function initCanvas(){
	let canvas = document.getElementById("myCanvas")
    this.ctx = canvas.getContext("2d");
    let width = 140,height = 140;
    canvas.width = width;//等同于写在组件 width={canvaswidth}
    canvas.height = height;//等同于写在组件 height={canvasheight}
    //移动端毛边优化和模糊优化-借鉴于echarts
	if (window.devicePixelRatio) {
		canvas.style.width = width + "px";
		canvas.style.height = height + "px";
		canvas.height = height * window.devicePixelRatio;
		canvas.width = width * window.devicePixelRatio;
		this.ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
	}
	//创建背景圆
	this.ctx.lineWidth = 12;//线宽
	this.ctx.lineCap = 'round';//圆弧顶端弧面
	startAnimate();
}
function startAnimate(){
    let _this = this;
    let totalScore = 90;//以满分为100分表示圆弧为360°
    let count = 0;
    this.scoreCountTimer = setInterval(function(){
      count += 1;
      if(count >= totalScore){
        count = totalScore
      }
      //清空canvas内上一次的绘制图案
      _this.ctx.clearRect(0, 0, 140, 140);//左上角为 00坐标 x y w h
      //绘制新圆弧
      _this.ctx.beginPath();//开始一个新的路径 x*(36/180)*Math.PI
      _this.ctx.arc(70, 70, 60, -Math.PI/2, (count*Math.PI*2/100) - Math.PI/2, false);///用于绘制圆弧context.arc(x坐标,y坐标,半径,起始角度,终止角度,false顺时针/true逆时针)
      _this.ctx.strokeStyle = 'rgba(252,255,152, 1)';
      _this.ctx.stroke();//对当前路径进行描边
      //分数动态渲染,canvas重绘不需要执行setState方法
      _this.setState({score: count})
      if(count >= totalScore){
        clearInterval(_this.scoreCountTimer)
      }
    },15)
  }

在这里插入图片描述

canvas api详解

HTMLCanvasElement.getContext() 方法返回canvas 的上下文,CanvasRenderingContext2D 具有的属性和方法如下:

  • 属性:canvas、currentTransform、direction、fillStyle、filter、font、globalAlpha、globalCompositeOperation、imageSmoothingEnabled、imageSmoothingQuality、lineCap、lineDashOffset、lineJoin、lineWidth、miterLimitshadowBlur、shadowColor、shadowOffsetX、shadowOffsetY、strokeStyle、textAlign、textBaseline
  • 方法:addHitRegion()、arc()、arcTo()、beginPath()、bezierCurveTo()、clearHitRegions()、clearRect()、clip()、closePath()、createImageData()、createLinearGradient()、createPattern()、createRadialGradient()、drawFocusIfNeeded()、drawImage()、drawWidgetAsOnScreen()、drawWindow()、ellipse()、fill()、fillRect()、fillText()、getImageData()、getLineDash()、isPointInPath()、isPointInStroke()、lineTo()、measureText()、moveTo()、putImageData()、quadraticCurveTo()、rect()、removeHitRegion()、resetTransform()、restore()、rotate()、save()、scale()、scrollPathIntoView()、setLineDash()、setTransform()、stroke()、strokeRect()、strokeText()、transform()、translate()
var canvas = document.getElementById('tutorial');
var ctx = canvas.getContext('2d');

常用的:

  • ctx.clearRect();//设置指定矩形区域内所有像素变成透明,并擦除之前绘制的所有内容
  • ctx.fillRect();//绘制填充矩形
  • ctx.strokeRect();//使用当前的绘画样式
  • ctx.lineCap;//线末端的类型
  • ctx.fillStyle;//图形内部的颜色和样式。 默认 #000 (黑色).
  • ctx.strokeStyle;//图形边线的颜色和样式。 默认 #000 (黑色).
  • ctx.beginPath();//清空子路径列表开始一个新的路径
  • ctx.arc();//绘制一段圆弧路径
  • ctx.stroke();//使用当前的样式描边子路径
  • ctx.drawImage();//绘制指定的图片
  • ctx.getImageData();//返回一个 ImageData 对象
参考资料

感谢阅读,欢迎评论^-^

打赏我吧^-^

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值