zoukankan      html  css  js  c++  java
  • canvas 画半圆的两种方式

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <canvas id="mycanvas" class="mycanvas" width="462" height="462"></canvas>
    </body>
    
    <script>
        const x0 = 231 / 2 * 2 // 圆心坐标
        const y0 = 231 / 2 * 2 // 圆心坐标
        const r1 = (231 / 2 - 16) * 2 // 外圆半径
        const r2 = (231 / 2 - 25) * 2 // 内圆半径
        const startAng = 135 // 起始角度
        const endAng = 45 // 终点角度
    
        function getPointX(r, ao) {
            return x0 + r * Math.cos(ao * Math.PI / 180)
    
        }
    
        function getPointY(r, ao) {
            return y0 + r * Math.sin(ao * Math.PI / 180)
        }
    
        var canvas = document.getElementById('mycanvas');
        var ctx = canvas.getContext("2d");
        ctx.beginPath();
        // ctx.moveTo(231/2, 231/2)
        // ---------1 画大圆------------
        ctx.arc(x0, y0, r1, (Math.PI / 180) * startAng, (Math.PI / 180) * endAng, false);
        // ctx.lineCap = "round";
        // ctx.lineWidth = '32';
        ctx.strokeStyle = 'blue';
        ctx.stroke();
    
    
    
    
    
    
        // 获取大圆上的点 与 边界圆交点
        let x1 = this.getPointX(r1, startAng)
        let y1 = this.getPointY(r1, startAng)
        // 获取小圆上的点  与 边界圆交点
        let x2 = this.getPointX(r2, startAng)
        let y2 = this.getPointY(r2, startAng)
        // 边界圆上的圆心
        let x3 = (x2 - x1) / 2 + x1
        let y3 = (y1 - y2) / 2 + y2
        // 边界圆上的半径
        let r3 = (r1 - r2) / 2
    
        // ---------2 画小圆与校园两个边界的的左侧圆------------
        ctx.moveTo(x2, y2)
        ctx.arc(x3, y3, r3, (Math.PI/180)*315, (Math.PI/180)*startAng, false);
        ctx.stroke();
    
        // ---------3 画小圆-----------
        ctx.moveTo(x2, y2)
        ctx.arc(x0, y0, r2, (Math.PI/180)*startAng, (Math.PI/180)*endAng, false);
        ctx.stroke();
    
    
        // 获取大圆上的点 与 边界圆交点
        let xx1 = this.getPointX(r1, endAng)
        let yy1 = this.getPointY(r1, endAng)
        // 获取小圆上的点  与 边界圆交点
        let xx2 = this.getPointX(r2, endAng)
        let yy2 = this.getPointY(r2, endAng)
        // 边界圆上的圆心
        let xx3 = (xx1 - xx2) / 2 + xx2
        let yy3 = (yy1 - yy2) / 2 + y2
        // 边界圆上的半径
        let rr3 = (r1 - r2) / 2
    
        // ---------2 画小圆与校园两个边界的的左侧圆------------
        ctx.moveTo(xx1, yy1)
        ctx.arc(xx3, yy3, rr3, (Math.PI/180)*45, (Math.PI/180)*235, false);
        ctx.stroke();
        ctx.closePath()
        ctx.fillStyle="green";
       //  ctx.fill()
    
     
    </script>
    
    <style>
    .mycanvas {
         231px;
    	height: 231px;
    }
    </style>
    
    </html>
    
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <canvas id="mycanvas" class="mycanvas" width="462" height="462"></canvas>
    </body>
    
    <script>
        const x0 = 231 / 2 * 2 // 圆心坐标
        const y0 = 231 / 2 * 2 // 圆心坐标
        const r1 = (231 / 2 - 16) * 2 // 外圆半径
        const r2 = (231 / 2 - 25) * 2 // 内圆半径
        const startAng = 135 // 起始角度
        const endAng = 45 // 终点角度
        var canvas = document.getElementById('mycanvas');
        var ctx = canvas.getContext("2d");
        ctx.beginPath();
        // ---------1 画大圆------------
        ctx.arc(x0, y0, r1, (Math.PI / 180) * startAng, (Math.PI / 180) * endAng, false);
        ctx.lineCap = "round";
        ctx.lineWidth = '32';
        ctx.strokeStyle = '#ebeff0';
        ctx.stroke();
    
        // 获取大圆上的点 与 边界圆交点
        let x1 = this.getPointX(r1, startAng)
        let y1 = this.getPointY(r1, startAng)
    
        ctx.moveTo(x1, y1)
        var temp = startAng
        window.requestAnimationFrame(() => {
            return this.animDraw(ctx, temp)
        });
    
        function drawColorArc(ctx, endAng) {
            ctx.beginPath();
            ctx.arc(x0, y0, r1, (Math.PI / 180) * startAng, (Math.PI / 180) * endAng, false);
            ctx.lineCap = "round";
            ctx.lineWidth = '32';
            ctx.strokeStyle = '#1dbdc4';
            ctx.stroke();
        }
    
        function animDraw(ctx, temp) {
            temp++
            if (temp < 270) {
                window.requestAnimationFrame(() => {
                    return this.animDraw(ctx, temp)
                });
            }
            this.drawColorArc(ctx, temp)
        }
    
        function getPointX(r, ao) {
            return x0   +   r   *   Math.cos(ao   *  Math.PI   /180   ) 
    
        }
        function getPointY(r,ao) {
            return y0   +   r   *   Math.sin(ao   *   Math.PI  /180   )  
        }
    
    
    </script>
    
    <style>
        .mycanvas {
             231px;
            height: 231px;
        }
    </style>
    
    </html>
    
  • 相关阅读:
    entity framework 缓存干扰的数据不一致问题
    async中await是干啥的,用不用有什么区别?
    await使用中的阻塞和并发
    Quartz.Net—MisFire
    Quartz.net misfire实践
    Quartz.NET 前一次任务未执行完成时不触发下次的解决方法
    Omni(USDT)钱包安装(ubuntu)
    USDT(omniCore)测试环境搭建
    WaitAll 和 WhenAll 的使用及区别
    C# 之 FileSystemWatcher事件多次触发的解决方法
  • 原文地址:https://www.cnblogs.com/WhiteHorseIsNotHorse/p/10757538.html
Copyright © 2011-2022 走看看