Home > Web Front-end > HTML Tutorial > How to draw Bezier curve using HTML5 Canvas?

How to draw Bezier curve using HTML5 Canvas?

WBOY
Release: 2023-09-02 20:21:05
forward
1428 people have browsed it

HTML5 tags are used to draw graphics, animations, etc. using scripts. It is a new tag introduced in HTML5. The canvas element has a DOM method called getContext, which obtains the rendering context and its drawing functions. This function takes one argument, the type of context 2d.

To draw a Bezier curve using the HTML5 canvas, use the bezierCurveTo() method. This method adds the given point to the current path, connecting it to the previous path via a cubic Bezier curve with the given control points.

如何使用HTML5 Canvas绘制贝塞尔曲线?

#You can try running the following code to learn how to draw Bezier curves on HTML5 Canvas. The x and y parameters in the bezierCurveTo() method are the coordinates of the endpoints. cp1x and cp1y are the coordinates of the first control point, and cp2x and cp2y are the coordinates of the second control point.

Example

<!DOCTYPE html>
<html>
<head>
<title>HTML5 Canvas Tag</title>
</head>

<body>
<canvas id = "newCanvas" width = "500" height = "300" style = "border:1px solid #000000;"></canvas>
<script>
var c = document.getElementById(&#39;newCanvas&#39;);
var ctx = c.getContext(&#39;2d&#39;);
ctx.beginPath();
ctx.moveTo(75,40);
ctx.bezierCurveTo(75,37,70,25,50,25);
ctx.bezierCurveTo(20,25,20,62.5,20,62.5);
ctx.bezierCurveTo(20,80,40,102,75,120);
ctx.bezierCurveTo(110,102,130,80,130,62.5);
ctx.bezierCurveTo(130,62.5,130,25,100,25);
ctx.bezierCurveTo(85,25,75,37,75,40);
ctx.fill();
</script>

</body>
</html>
Copy after login

Output

如何使用HTML5 Canvas绘制贝塞尔曲线?

The above is the detailed content of How to draw Bezier curve using HTML5 Canvas?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template