Web888.vn
  • Shop
  • Blog
Đăng nhập
  • Đăng nhập / Đăng ký

Please enter key search to display results.

Home
  • HTML programming
  • Technology News
HTML5 Canvas – Draw Curves

HTML5 Canvas – Draw Curves

  • 24-07-2022
  • Trung Minh
  • 0 Comments

In addition to drawing straight lines, Canvas can also draw curves such as arcs, quadratic curves, and Benzier curves. For how to draw a straight line, we only need to determine the beginning and end points, but with curves, there are quite a few problems to calculate before drawing it.

1. Draw an arc in Canvas

To draw an arc, we use arc(x, y, radius, startAngle, endAngle, counterClockwise) , where:

  • x and y are the centers of the arc
  • radius is the radius
  • startAngle is the starting angle
  • endAngle is the end angle
  • counterClockwise is drawing direction ( clockwise or counterclockwise )
 var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); // Xác đinh tâm là trọng tâm của Canvas var x = canvas.width / 2; var y = canvas.height / 2; // bán kính 75px var radius = 75; // Góc bắt đầu là 1.1PI var startAngle = 1.1 * Math.PI; // Góc kết thúc là 1.9PI var endAngle = 1.9 * Math.PI; // Cùng chiều kim đồng hồ var counterClockwise = false; // Vẽ đường cung context.beginPath(); context.arc(x, y, radius, startAngle, endAngle, counterClockwise); context.lineWidth = 15; // line color context.strokeStyle = 'black'; context.stroke();

2. Draw a quadratic curve in Canvas

To draw a quadratic curve in Canvas, we use the quadraticCurveTo() method, the quadratic curve is determined by three main points including:

  • The starting point ( context point ).
  • Control point ( control point ).
  • End point ( end point ).
 var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); context.beginPath(); context.moveTo(20, 20); // Điểm bắt đầu vẽ context.quadraticCurveTo(20, 100, 200, 20); //vẽ qua các điểm context.lineWidth = 1; // line color context.strokeStyle = 'black'; context.stroke();
  • The starting point is ( 20, 20 ) in the context.moveTo( 20, 20 ) function.
  • The control point is ( 20, 100 ) in the function context.quadraticCurveTo( 20, 100 , 200, 20) .
  • The end point is ( 200, 20 ) in the context.quadraticCurveTo(20, 100, 200, 20 ) function.

The vertex position of the curve is determined by the line connecting the midpoints between the two lines ( context point, control point ) and ( end point, control point ).

 var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); context.beginPath(); context.moveTo(188, 150); context.quadraticCurveTo(288, 0, 388, 150); context.lineWidth = 10; // line color context.strokeStyle = 'black'; context.stroke();

3. Draw a Benzier Curve in Canvas

The Benzier curve is defined by four points:

  • The starting point ( context point )
  • Control point one ( control point 1 )
  • Control point two ( control point 2 )
  • End point
 var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.beginPath(); ctx.moveTo(20, 20); ctx.bezierCurveTo(20, 100, 200, 100, 200, 20); ctx.stroke();

The explanation in the code is as follows:

  • The starting point is ( 20, 20 ) in the context.moveTo( 20, 20 ) function.
  • Control point 1 is ( 20, 100 ) in the function context.quadraticCurveTo( 20, 100 , 200, 100, 200, 20) .
  • Control point 2 is ( 200, 100 ) in the function context.quadraticCurveTo(20, 100, 200, 100 , 200, 20) .
  • The end point is ( 200, 20 ) in the context.quadraticCurveTo(20, 100, 200, 100, 200, 20 ) function.

The question is how to determine the vertex and curvature? See the example below.

 var canvas = document.getElementById('myCanvas');<br /> var context = canvas.getContext('2d');<br /> <br /> context.beginPath();<br /> context.moveTo(188, 130);<br /> context.bezierCurveTo(140, 10, 388, 10, 388, 170);<br /> context.lineWidth = 10;<br /> <br /> // line color<br /> context.strokeStyle = 'black';<br /> context.stroke();

4. Conclusion

Starting to find it difficult to work with Canvas already. Really, to draw good images, you have to calculate each standard Pixel, not to mention having to use math knowledge to determine coordinates.

In the next lesson we will learn how to draw a line using the functions learned in this and previous lessons.

Bài viết liên quan:

Installing PHP and introducing PHP basics
Instructions for using the plugin wp bakery builder (visual composer) to make a landing page
Instructions for installing and activating Office 2013
15 pocket tips in daily life you should know
What is WordPress and why should learn about wordpress?
Install composer use for Xampp on Windows
Guide to debug (debug) when programming C/C++ with Dev C++
Binary integer representation and binary operations
Create Gmail STMP and install for WordPress, Laravel, php…
Install and enable fileinfo extension in PHP 7 linux (centos,ubuntu)
The thing that limits your growth is the relationship you have
10 groups of customers that business people often encounter and how to behave professionally

THÊM BÌNH LUẬN Cancel reply

Dịch vụ thiết kế Wesbite

NỘI DUNG MỚI CẬP NHẬT

Step-by-step guide to installing Microsoft SQL Server and connecting to the database for beginners

Reasons Why People Are Increasingly Inexperienced in Life

PHP Data Types

Table (table) in SQL Server

Basic data types in C

Giới thiệu

web888.vn là chuyên trang chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !

Liên hệ quảng cáo: [email protected]

Kết nối với web888

© web888.vn - Tech888 Co .Ltd since 2019

Đăng nhập

Trở thành một phần của cộng đồng của chúng tôi!
Registration complete. Please check your email.
Đăng nhập bằng google
Đăng kýBạn quên mật khẩu?

Create an account

Welcome! Register for an account
The user name or email address is not correct.

Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our privacy policy.

Registration confirmation will be emailed to you.
Log in Lost your password?

Reset password

Recover your password
Password reset email has been sent.
The email could not be sent. Possible reason: your host may have disabled the mail function.
A password will be e-mailed to you.
Log in Register
×