search
HomeWeb Front-endH5 Tutorial在HTML5的CANVAS上绘制椭圆的几种方法

  HTML5中的Canvas并没有直接提供绘制椭圆的方法,下面是对几种绘制方法的总结。各种方法各有优缺,视情况选用。各方法的参数相同:


  •   context为Canvas的2D绘图环境对象,
  •   x为椭圆中心横坐标,
  •   y为椭圆中心纵坐标,
  •   a为椭圆横半轴长,
  •   b为椭圆纵半轴长。



  参数方程法


  该方法利用椭圆的参数方程来绘制椭圆
  1. //-----------用参数方程绘制椭圆---------------------
  2. //函数的参数x,y为椭圆中心;a,b分别为椭圆横半轴、
  3. //纵半轴长度,不可同时为0
  4. //该方法的缺点是,当linWidth较宽,椭圆较扁时
  5. //椭圆内部长轴端较为尖锐,不平滑,效率较低
  6. function ParamEllipse(context, x, y, a, b)
  7. {
  8.    //max是等于1除以长轴值a和b中的较大者
  9.    //i每次循环增加1/max,表示度数的增加
  10.    //这样可以使得每次循环所绘制的路径(弧线)接近1像素
  11.    var step = (a > b) ? 1 / a : 1 / b;
  12.    context.beginPath();
  13.    context.moveTo(x + a, y); //从椭圆的左端点开始绘制
  14.    for (var i = 0; i
  15.    {
  16.       //参数方程为x = a * cos(i), y = b * sin(i),
  17.       //参数为i,表示度数(弧度)
  18.       context.lineTo(x + a * Math.cos(i), y + b * Math.sin(i));
  19.    }
  20.    context.closePath();
  21.    context.stroke();
  22. };
复制代码

  均匀压缩法


  这种方法利用了数学中的均匀压缩原理将圆进行均匀压缩为椭圆,理论上为能够得到标准的椭圆.
  1. //------------均匀压缩法绘制椭圆--------------------
  2. //其方法是用arc方法绘制圆,结合scale进行
  3. //横轴或纵轴方向缩放(均匀压缩)
  4. //这种方法绘制的椭圆的边离长轴端越近越粗,长轴端点的线宽是正常值
  5. //边离短轴越近、椭圆越扁越细,甚至产生间断,这是scale导致的结果
  6. //这种缺点某些时候是优点,比如在表现环的立体效果(行星光环)时
  7. //对于参数a或b为0的情况,这种方法不适用
  8. function EvenCompEllipse(context, x, y, a, b)
  9. {
  10.    context.save();
  11.    //选择a、b中的较大者作为arc方法的半径参数
  12.    var r = (a > b) ? a : b;
  13.    var ratioX = a / r; //横轴缩放比率
  14.    var ratioY = b / r; //纵轴缩放比率
  15.    context.scale(ratioX, ratioY); //进行缩放(均匀压缩)
  16.    context.beginPath();
  17.    //从椭圆的左端点开始逆时针绘制
  18.    context.moveTo((x + a) / ratioX, y / ratioY);
  19.    context.arc(x / ratioX, y / ratioY, r, 0, 2 * Math.PI);
  20.    context.closePath();
  21.    context.stroke();
  22.    context.restore();
  23. };
复制代码


      下面的代码会出现线宽不一致的问题,解决办法:


     均匀压缩法中把
  context.stroke();  context.restore();
  改為
  context.restore();  context.stroke();
  就可以

        三次贝塞尔曲线法一


  三次贝塞尔曲线绘制椭圆在实际绘制时是一种近似,在理论上也是一种近似。 但因为其效率较高,在计算机矢量图形学中,常用于绘制椭圆,但是具体的理论我不是很清楚。 近似程度在于两个控制点位置的选取。这种方法的控制点位置是我自己试验得出,精度还可以.
  1. //---------使用三次贝塞尔曲线模拟椭圆1---------------------
  2. //此方法也会产生当lineWidth较宽,椭圆较扁时,
  3. //长轴端较尖锐,不平滑的现象
  4. function BezierEllipse1(context, x, y, a, b)
  5. {
  6.    //关键是bezierCurveTo中两个控制点的设置
  7.    //0.5和0.6是两个关键系数(在本函数中为试验而得)
  8.    var ox = 0.5 * a,
  9.        oy = 0.6 * b;

  10.    context.save();
  11.    context.translate(x, y);
  12.    context.beginPath();
  13.    //从椭圆纵轴下端开始逆时针方向绘制
  14.    context.moveTo(0, b);
  15.    context.bezierCurveTo(ox, b, a, oy, a, 0);
  16.    context.bezierCurveTo(a, -oy, ox, -b, 0, -b);
  17.    context.bezierCurveTo(-ox, -b, -a, -oy, -a, 0);
  18.    context.bezierCurveTo(-a, oy, -ox, b, 0, b);
  19.    context.closePath();
  20.    context.stroke();
  21.    context.restore();

  22. };
复制代码

  三次贝塞尔曲线法二


  这种方法是从StackOverFlow中一个帖子的回复中改变而来,精度较高,也是通常用来绘制椭圆的方法.
  1. //---------使用三次贝塞尔曲线模拟椭圆2---------------------
  2. //此方法也会产生当lineWidth较宽,椭圆较扁时
  3. //,长轴端较尖锐,不平滑的现象
  4. //这种方法比前一个贝塞尔方法精确度高,但效率稍差
  5. function BezierEllipse2(ctx, x, y, a, b)
  6. {
  7.    var k = .5522848,
  8.    ox = a * k, // 水平控制点偏移量
  9.    oy = b * k; // 垂直控制点偏移量

  10.    ctx.beginPath();
  11.    //从椭圆的左端点开始顺时针绘制四条三次贝塞尔曲线
  12.    ctx.moveTo(x - a, y);
  13.    ctx.bezierCurveTo(x - a, y - oy, x - ox, y - b, x, y - b);
  14.    ctx.bezierCurveTo(x + ox, y - b, x + a, y - oy, x + a, y);
  15.    ctx.bezierCurveTo(x + a, y + oy, x + ox, y + b, x, y + b);
  16.    ctx.bezierCurveTo(x - ox, y + b, x - a, y + oy, x - a, y);
  17.    ctx.closePath();
  18.    ctx.stroke();
  19. };
复制代码


  光栅法


  这种方法可以根据Canvas能够操作像素的特点,利用图形学中的基本算法来绘制椭圆。 例如中点画椭圆算法等。


  其中一个例子是园友“豆豆狗”的一篇博文“HTML5 Canvas 提高班(一) —— 光栅图形学(1)中点画圆算法”。这种方法由于比较“原始”,灵活性大,效率高,精度高,但要想实现一个有使用价值的绘制椭圆的函数,比较复杂。比如,要当线宽改变时,算法就复杂一些。

      原文出自:Cloudy Waterman

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5: How It Enhances User Experience on the WebH5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AM

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

Deconstructing H5 Code: Tags, Elements, and AttributesDeconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool