- Canvas Cookbook
- Bhushan Purushottam Joshi
- 236字
- 2021-07-16 11:03:14
Drawing a quadratic curve
In this recipe, you will learn how to draw a quadratic curve. The quadratic curve provides much more flexibility. These curves can be used to create custom shapes in numerous drawings. You will find one implementation in the next recipe:

How to do it...
Here is a simple code to draw a quadratic curve:
<html> <head> <title>Arcs</title> <script> function init() { can = document.getElementById("MyCanvasArea"); ctx = can.getContext("2d"); //call to the function to draw curve drawQuadraticCurve(50,100,150,30,250,100,'#df34ef',7); //function to draw quadratic curve function drawQuadraticCurve(xStart,yStart,xControl, yControl, xEnd, yEnd,color,width) { ctx.beginPath(); ctx.strokeStyle=color; ctx.lineJoin="round"; ctx.lineWidth=width; ctx.moveTo(xStart,yStart); ctx.quadraticCurveTo(xControl, yControl, xEnd, yEnd); ctx.stroke(); ctx.closePath(); } } </script> </head> <body onload="init()"> <canvas ID="MyCanvasArea" width="300" height="200" style="border:2px solid black;"> your browser doesn't support canvas </canvas> </body> </html>
How it works...
The API function is quadraticCurveTo(cpX,cpY,epX,epY)
.
In this function, cpX
and cpY
are coordinates of the control point, and epX
and epY
are coordinates of the end point, the drawing has to start from some point. However, it is not part of this function. You have to move to a point that you want to draw from. This is done by using the moveTo()
function.
Refer to the diagram:

Observe the points in the diagram. The parameters passed to the quadraticCurveTo()
function are coordinates of the control point and end point. Before this function is called you need to call a function moveTo()
from where you specify the start/context point.
There's more...
Try the following:
- Change the control point to
(150,150)
- Change the other coordinates and observe the output
- 大學計算機應用基礎實踐教程
- 造個小程序:與微信一起干件正經事兒
- 小創客玩轉圖形化編程
- 無代碼編程:用云表搭建企業數字化管理平臺
- Dependency Injection in .NET Core 2.0
- Learning Linux Binary Analysis
- Windows Phone 7.5:Building Location-aware Applications
- C專家編程
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- 零基礎學Python編程(少兒趣味版)
- iOS開發項目化入門教程
- Docker on Windows
- Web前端開發技術實踐指導教程
- 威脅建模:設計和交付更安全的軟件
- SFML Essentials