美文网首页
canvas画饼图(一) 基础圆弧

canvas画饼图(一) 基础圆弧

作者: Viewwei | 来源:发表于2021-03-19 22:22 被阅读0次
image.png
  • 核心知识点
    扇形需要一个起始点,半径,起始弧度和结束弧度
  • 相关知识点
    beginPath : 开始一段新路径
    save: 这一时刻的设置会放到一个暂存栈中.保存之后可以继续修改上下文
    restore:这个方法主要是从暂存中取出并恢复之前保存的设置.保存多个,需要一步一步的释放
    moveTo:不绘制先,只移动光标
    arc:已x,y为圆心,已 r 为半径,其实位置和结束位置绘制一段圆弧
    fillStyle:填充 yanse
    fill:填充
最简单的圆弧如下
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>饼图</title>
        <style>
            body {
                margin: 0;
                overflow: hidden
            }

            #canvas {
                background: antiquewhite;
            }
        </style>
    </head>
    <body>
        <canvas id="canvas"></canvas>
    </body>
    <script>
    const [width,height]=[window.innerWidth,window.innerHeight];
        const canvas = document.getElementById("canvas")
        canvas.width = width
        canvas.height = height
        const ctx = canvas.getContext("2d")
        class Sector {
            constructor(radius = 200, startAngle = 0, endAngle = Math.PI / 2, color = 'chocolate') {
                this.radius = radius
                this.startAngle = startAngle
                this.endAngle = endAngle
                this.color = color
                this.x = 0
                this.y = 0
                this.text = "标签文字"
                this.data = 1000
                this.textAlign = 'left'
                this.p1 = {
                    x: 0,
                    y: 0,
                    d: 20
                };
                this.p2 = {
                    x: 0,
                    y: 0,
                    d: 70
                };
                this.p3 = {
                    x: 0,
                    y: 0,
                    d: 20
                };
                this.p4 = {
                    x: 0,
                    y: 0,
                    d: 10
                };
            }
            draw(ctx) {
                const {
                    x,
                    y,
                    radius,
                    startAngle,
                    endAngle,
                    color
                } = this
                ctx.save()
                ctx.beginPath()
                ctx.fillStyle = color
                ctx.moveTo(x,y) //这段非常重要,意思是把光标移动到圆心,这样绘制才是圆弧
                ctx.arc(x, y, radius, startAngle, endAngle)
                ctx.fill()
                ctx.restore()
            }
        }
        const item = new Sector()
        item.x = 200
        item.y = 200
        item.draw(ctx)
    </script>
</html>

相关文章

  • canvas画饼图(一) 基础圆弧

    核心知识点扇形需要一个起始点,半径,起始弧度和结束弧度 相关知识点beginPath : 开始一段新路径save:...

  • canvas入门基础2

    继canvas入门基础1 _ 圆弧context.arc(x, y, radius, start,end, boo...

  • canvas画饼状图

    1.绘制饼状态图1.1 根据数据绘制一个饼图1.2 绘制标题 从扇形的弧中心伸出一条线在画一条横线在横线的上面写上...

  • canvas画饼图(2)

    两个步骤: 第一步画一个扇形 第二步把扇形画满360° 来看第一个步骤,怎么画扇形? 看下面的示意图 要画扇形图,...

  • SVG基础/SVG画饼图

    SVG基础 SVG画饼图 扩展

  • 绘制基础(二)- 饼形图

    这回来画饼形图,进一步属性API调用 在此之前,先理清楚Canvas的坐标系,在Android里,Canvas就相...

  • Canvas和Path的基础用法

    Canvas和Path的基础用法 Canvas的基础用法 上代码: 效果图: 上代码: 效果图: 上代码: 效果图...

  • canvas画饼图(四) 饼图动画

    饼图的动画饼图的动画主要有两个:一个是刚开始的饼图由小到大,鼠标到饼图上面饼图,饼图变大或者变小 实现思路饼图变大...

  • canvas画饼图(二) 画射线

    第二节画上图红线标准的部分 思路解析上图标注的图形可以看出,需要 4 个点的位置分别为p1,p2,p3,p4.通过...

  • canvas-绘制圆弧

网友评论

      本文标题:canvas画饼图(一) 基础圆弧

      本文链接:https://www.haomeiwen.com/subject/fkgbcltx.html