请使用canvas画一个椭圆
<!DOCTYPE html>
<html>
<head>
<title>Canvas Ellipse</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// Draw an ellipse
ctx.beginPath();
ctx.ellipse(150, 75, 50, 30, 0, 0, 2 * Math.PI); // x, y, radiusX, radiusY, rotation, startAngle, endAngle
ctx.stroke();
// Example with fill and different color
ctx.beginPath();
ctx.ellipse(100, 75, 40, 20, Math.PI / 4, 0, 2 * Math.PI); // Rotated ellipse
ctx.fillStyle = "red";
ctx.fill();
// Example with a partial ellipse (arc)
ctx.beginPath();
ctx.ellipse(220, 75, 30, 40, 0, Math.PI/2 , 3*Math.PI/2); // Half ellipse, downwards
ctx.strokeStyle = "blue";
ctx.lineWidth = 3; // Set line thickness
ctx.stroke();
</script>
</body>
</html>
This code creates an HTML5 canvas and draws three ellipses:
- A simple black ellipse: Centered at (150, 75) with horizontal radius 50 and vertical radius 30.
- A filled red rotated ellipse: Centered at (100, 75), rotated by 45 degrees (Math.PI / 4 radians), with horizontal radius 40 and vertical radius 20.
- A blue half-ellipse arc: Centered at (220,75) with horizontal radius 30 and vertical radius 40. It draws the bottom half of the ellipse using
Math.PI/2
as the start angle and3*Math.PI/2
as the end angle. It also demonstrates setting the line thickness.
This provides a few examples of how to use the ellipse()
method with different parameters and styling options. Remember to include this code within the <body>
tags of your HTML file. You can adjust the parameters to create ellipses with different sizes, positions, rotations, and colors.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!