HTML5canvas画圆的实现

本文介绍了如何使用HTML5的Canvas API来实现画圆的功能。通过提供的代码示例,展示了如何设置画布、确定圆心坐标和半径,以及填充和描边颜色,帮助读者理解Canvas绘制圆形的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下:

代码如下:

<!DOCTYPE html>
<htmls>
<head>
    <meta charset="UTF-8">
    <title>Text2画圆</title>
</head>
<style>
</style>
<body>
<div id="canvas-warp">
    <canvas id="canvas" style="border: 1px solid #aaaaaa; display: block; margin: 50px auto;">
        1
    </canvas>
</div>

<script>
    window.onload = function () {
        var canvas = document.getElementById("canvas");
        canvas.width = 500;
        canvas.height = 500;
        var context = canvas.getContext("2d");
        context.beginPath();
        context.arc(100,70,50,0.5*Math.PI,1*Math.PI);
        context.strokeStyle = "green";
        context.fillStyle = "white";
        context.fill(); //确认填充
        context.lineWidth = 5
        context.stroke();

        context.beginPath();
        context.arc(100,70,50,1*Math.PI,1.5*Math.PI);
        context.strokeStyle = "#999";
        context.fillStyle = "white";
        context.fill(); //确认填充
        context.stroke();
        
        context.beginPath();
        context.arc(100,70,50,1.5*Math.PI,2*Math.PI);
        context.strokeStyle = "red";
        context.fillStyle = "white";
        context.fill(); //确认填充
        context.stroke();
        
        context.beginPath();
        context.arc(100,70,50,2*Math.PI,0.5*Math.PI);
        context.strokeStyle = "blue";
        context.fillStyle = "white";
        context.fill(); //确认填充
        context.stroke();

    }
</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值