HTML-5 Tutorial
HTML5 Tag Reference
HTML5 Useful References
HTML5 Tools
HTML5 Resources
Selected Reading
© 2011 TutorialsPoint.COM
|
HTML5 Canvas - Drawing Quadratic Curves
There are following methods required to draw quadratic curve on the canvas:
SN | Method and Description |
1 | beginPath() This method resets the current path. |
2 | moveTo(x, y) This method creates a new subpath with the given point. |
3 | closePath() This method marks the current subpath as closed, and starts a new subpath with a point the same as the start and end of the newly closed subpath. |
4 | fill() This method fills the subpaths with the current fill style. |
5 | stroke() This method strokes the subpaths with the current stroke style. |
6 | quadraticCurveTo(cpx, cpy, x, y) This method adds the given point to the current path, connected to the previous one by a quadratic Bezier curve with the given control point. |
The x and y parameters in quadraticCurveTo() method are the coordinates of the end point. cpx and cpy are the coordinates of the control point.
Example:
Following is a simple example which makes use of above mentioned methods to draw a Quadratic curves.
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function drawShape(){
// get the canvas element using the DOM
var canvas = document.getElementById('mycanvas');
// Make sure we don't execute when canvas isn't supported
if (canvas.getContext){
// use getContext to use the canvas for drawing
var ctx = canvas.getContext('2d');
// Draw shapes
ctx.beginPath();
ctx.moveTo(75,25);
ctx.quadraticCurveTo(25,25,25,62.5);
ctx.quadraticCurveTo(25,100,50,100);
ctx.quadraticCurveTo(50,120,30,125);
ctx.quadraticCurveTo(60,120,65,100);
ctx.quadraticCurveTo(125,100,125,62.5);
ctx.quadraticCurveTo(125,25,75,25);
ctx.stroke();
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
}
}
</script>
</head>
<body onload="drawShape();">
<canvas id="mycanvas"></canvas>
</body>
</html>
|
The above example would draw following shape:
To learn above concept - Do Online Practice using latest version of either Safari or Opera.
|
|
|