- 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
- Web程序設(shè)計(jì)及應(yīng)用
- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門(mén)寶典
- 小程序?qū)崙?zhàn)視頻課:微信小程序開(kāi)發(fā)全案精講
- SOA實(shí)踐
- PHP 7底層設(shè)計(jì)與源碼實(shí)現(xiàn)
- Visual C++實(shí)例精通
- AIRAndroid應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)
- MATLAB實(shí)用教程
- 游戲程序設(shè)計(jì)教程
- STM32F0實(shí)戰(zhàn):基于HAL庫(kù)開(kāi)發(fā)
- Java軟件開(kāi)發(fā)基礎(chǔ)
- Java SE實(shí)踐教程
- Java EE企業(yè)級(jí)應(yīng)用開(kāi)發(fā)教程(Spring+Spring MVC+MyBatis)
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- 安卓工程師教你玩轉(zhuǎn)Android