- context为Canvas的2D绘图环境对象,
- x为椭圆中心横坐标,
- y为椭圆中心纵坐标,
- a为椭圆横半轴长,
-
b为椭圆纵半轴长。
参数方程法
该方法利用椭圆的参数方程来绘制椭圆
- //-----------用参数方程绘制椭圆---------------------
- //函数的参数x,y为椭圆中心;a,b分别为椭圆横半轴、
- //纵半轴长度,不可同时为0
- //该方法的缺点是,当linWidth较宽,椭圆较扁时
- //椭圆内部长轴端较为尖锐,不平滑,效率较低
- function ParamEllipse(context, x, y, a, b)
- {
- //max是等于1除以长轴值a和b中的较大者
- //i每次循环增加1/max,表示度数的增加
- //这样可以使得每次循环所绘制的路径(弧线)接近1像素
- var step = (a > b) ? 1 / a : 1 / b;
- context.beginPath();
- context.moveTo(x + a, y); //从椭圆的左端点开始绘制
- for (var i = 0; i
- {
- //参数方程为x = a * cos(i), y = b * sin(i),
- //参数为i,表示度数(弧度)
- context.lineTo(x + a * Math.cos(i), y + b * Math.sin(i));
- }
- context.closePath();
- context.stroke();
- };
均匀压缩法
这种方法利用了数学中的均匀压缩原理将圆进行均匀压缩为椭圆,理论上为能够得到标准的椭圆.
- //------------均匀压缩法绘制椭圆--------------------
- //其方法是用arc方法绘制圆,结合scale进行
- //横轴或纵轴方向缩放(均匀压缩)
- //这种方法绘制的椭圆的边离长轴端越近越粗,长轴端点的线宽是正常值
- //边离短轴越近、椭圆越扁越细,甚至产生间断,这是scale导致的结果
- //这种缺点某些时候是优点,比如在表现环的立体效果(行星光环)时
- //对于参数a或b为0的情况,这种方法不适用
- function EvenCompEllipse(context, x, y, a, b)
- {
- context.save();
- //选择a、b中的较大者作为arc方法的半径参数
- var r = (a > b) ? a : b;
- var ratioX = a / r; //横轴缩放比率
- var ratioY = b / r; //纵轴缩放比率
- context.scale(ratioX, ratioY); //进行缩放(均匀压缩)
- context.beginPath();
- //从椭圆的左端点开始逆时针绘制
- context.moveTo((x + a) / ratioX, y / ratioY);
- context.arc(x / ratioX, y / ratioY, r, 0, 2 * Math.PI);
- context.closePath();
- context.stroke();
- context.restore();
- };
下面的代码会出现线宽不一致的问题,解决办法:
均匀压缩法中把
context.stroke(); context.restore();
改為
context.restore(); context.stroke();
就可以
三次贝塞尔曲线法一
三次贝塞尔曲线绘制椭圆在实际绘制时是一种近似,在理论上也是一种近似。 但因为其效率较高,在计算机矢量图形学中,常用于绘制椭圆,但是具体的理论我不是很清楚。 近似程度在于两个控制点位置的选取。这种方法的控制点位置是我自己试验得出,精度还可以.
- //---------使用三次贝塞尔曲线模拟椭圆1---------------------
- //此方法也会产生当lineWidth较宽,椭圆较扁时,
- //长轴端较尖锐,不平滑的现象
- function BezierEllipse1(context, x, y, a, b)
- {
- //关键是bezierCurveTo中两个控制点的设置
- //0.5和0.6是两个关键系数(在本函数中为试验而得)
- var ox = 0.5 * a,
- oy = 0.6 * b;
-
- context.save();
- context.translate(x, y);
- context.beginPath();
- //从椭圆纵轴下端开始逆时针方向绘制
- context.moveTo(0, b);
- context.bezierCurveTo(ox, b, a, oy, a, 0);
- context.bezierCurveTo(a, -oy, ox, -b, 0, -b);
- context.bezierCurveTo(-ox, -b, -a, -oy, -a, 0);
- context.bezierCurveTo(-a, oy, -ox, b, 0, b);
- context.closePath();
- context.stroke();
- context.restore();
-
- };
三次贝塞尔曲线法二
这种方法是从StackOverFlow中一个帖子的回复中改变而来,精度较高,也是通常用来绘制椭圆的方法.
- //---------使用三次贝塞尔曲线模拟椭圆2---------------------
- //此方法也会产生当lineWidth较宽,椭圆较扁时
- //,长轴端较尖锐,不平滑的现象
- //这种方法比前一个贝塞尔方法精确度高,但效率稍差
- function BezierEllipse2(ctx, x, y, a, b)
- {
- var k = .5522848,
- ox = a * k, // 水平控制点偏移量
- oy = b * k; // 垂直控制点偏移量
-
- ctx.beginPath();
- //从椭圆的左端点开始顺时针绘制四条三次贝塞尔曲线
- ctx.moveTo(x - a, y);
- ctx.bezierCurveTo(x - a, y - oy, x - ox, y - b, x, y - b);
- ctx.bezierCurveTo(x + ox, y - b, x + a, y - oy, x + a, y);
- ctx.bezierCurveTo(x + a, y + oy, x + ox, y + b, x, y + b);
- ctx.bezierCurveTo(x - ox, y + b, x - a, y + oy, x - a, y);
- ctx.closePath();
- ctx.stroke();
- };
光栅法
这种方法可以根据Canvas能够操作像素的特点,利用图形学中的基本算法来绘制椭圆。 例如中点画椭圆算法等。
其中一个例子是园友“豆豆狗”的一篇博文“HTML5 Canvas 提高班(一) —— 光栅图形学(1)中点画圆算法”。这种方法由于比较“原始”,灵活性大,效率高,精度高,但要想实现一个有使用价值的绘制椭圆的函数,比较复杂。比如,要当线宽改变时,算法就复杂一些。
原文出自:Cloudy Waterman

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.

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.

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.

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.

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.

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 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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool