如何使用javascript生成圆

发布于 2025-01-04 13:50:15 字数 85 浏览 1 评论 0原文

我如何使用 javascript 在 Html 中创建一个圆圈。是否可以使用 Javascript 来完成?我已经创建了矩形和正方形,但不知道如何完成圆形。

How can i create a Circle in Html using javascript. Does it can be done using Javascript or not? I have done creating rectangles and squares but don't know how to accomplish a circle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘羡 2025-01-11 13:50:15

以下是针对现代浏览器进行循环的简单方法:

<div style='border: 3px solid red; border-radius: 50px; width: 100px; height: 100px;'>
</div>

演示

编辑 - 将“box-sizing”(对于 Firefox 为“-moz-box-sizing”)设置为“border-box”效果更好。

<style>
  div.circle {
    border: 3px solid red;
    border-radius: 50%;
    width: 100px; height: 100px;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }
</style>

<div class=circle>
</div>

Here's a simple way to do a circle for modern browsers:

<div style='border: 3px solid red; border-radius: 50px; width: 100px; height: 100px;'>
</div>

Demo.

edit — It works better with the "box-sizing" ("-moz-box-sizing" for Firefox) set to "border-box".

<style>
  div.circle {
    border: 3px solid red;
    border-radius: 50%;
    width: 100px; height: 100px;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }
</style>

<div class=circle>
</div>
贵在坚持 2025-01-11 13:50:15

使用 HTML5 Canvas 和 AJAX,您可以执行以下操作

window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var centerX = canvas.width / 2;
    var centerY = canvas.height / 2;
    var radius = 10;

    context.beginPath();
    context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    context.fillStyle = "black";
    context.fill();
    context.lineWidth = 1;
    context.strokeStyle = "black";
    context.stroke();

主要内容是:

context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

有关更多信息,请参阅:HTML5 Canvas 教程

Using HTML5 Canvas and AJAX, you can do the following:

window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var centerX = canvas.width / 2;
    var centerY = canvas.height / 2;
    var radius = 10;

    context.beginPath();
    context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    context.fillStyle = "black";
    context.fill();
    context.lineWidth = 1;
    context.strokeStyle = "black";
    context.stroke();

};

the big line being the:

context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

For more go see: HTML5 Canvas Tutorials

渔村楼浪 2025-01-11 13:50:15

您需要使用以下库:-

将代码放入您的 head 标签中。

<script type="text/JavaScript" src="jsDraw2D.js"></script>

可以从此处下载

,将以下代码复制并粘贴到您希望圈子出现的位置...

<script type="text/JavaScript">

//Create jsGraphics object
var gr = new jsGraphics(document.getElementById("canvas"));

//Create jsColor object
var col = new jsColor("red");

//Create jsPen object
var pen = new jsPen(col,1);

//Draw a Line between 2 points
var pt1 = new jsPoint(20,30);
var pt2 = new jsPoint(120,230);
gr.drawLine(pen,pt1,pt2);

//Draw filled circle with pt1 as center point and radius 30. 
gr.fillCircle(col,pt1,30);

//You can also code with inline object instantiation like below
gr.drawLine(pen,new jsPoint(40,10),new jsPoint(70,150)); 

</script>

您可以检查此处的文档

You need to use the following liabrary:-

Put the code in your head tag.

<script type="text/JavaScript" src="jsDraw2D.js"></script>

It is available to download from here

Copy and paste the following code where you want your circle to appear...

<script type="text/JavaScript">

//Create jsGraphics object
var gr = new jsGraphics(document.getElementById("canvas"));

//Create jsColor object
var col = new jsColor("red");

//Create jsPen object
var pen = new jsPen(col,1);

//Draw a Line between 2 points
var pt1 = new jsPoint(20,30);
var pt2 = new jsPoint(120,230);
gr.drawLine(pen,pt1,pt2);

//Draw filled circle with pt1 as center point and radius 30. 
gr.fillCircle(col,pt1,30);

//You can also code with inline object instantiation like below
gr.drawLine(pen,new jsPoint(40,10),new jsPoint(70,150)); 

</script>

You can check the documentation for this from here

旧伤慢歌 2025-01-11 13:50:15

您可以使用一些 javascript 库来实现同样的目的。就像这里是一个第三方 js 库,它可以满足您的目的

http://processingjs.org/

You can use some javascript library for the same. Like here is a third party js library which may serve your purpose

http://processingjs.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文