Paint a cloud by using bezier curve in JavaScript
Description
The following code shows how to paint a cloud by using bezier curve.
Example
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function(){<!--from ww w . j a v a2 s . c o m-->
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var startX = 200;
var startY = 100;
// draw cloud shape
context.beginPath();
context.moveTo(startX, startY);
context.bezierCurveTo(startX - 40, startY + 10, startX - 20, startY + 180, startX + 60, startY + 70);
context.bezierCurveTo(startX + 100, startY + 100, startX + 150, startY + 100, startX + 150, startY + 70);
context.bezierCurveTo(startX + 280, startY + 70, startX + 230, startY + 40, startX + 210, startY + 20);
context.bezierCurveTo(startX + 360, startY - 40, startX + 210, startY - 50, startX + 160, startY - 30);
context.bezierCurveTo(startX + 150, startY - 75, startX + 80, startY - 60, startX + 70, startY - 30);
context.bezierCurveTo(startX + 30, startY - 75, startX - 10, startY - 60, startX, startY);
context.closePath();
// add a radial gradient
var grdCenterX = 250;
var grdCenterY = 80;
var grd = context.createRadialGradient(grdCenterX, grdCenterY, 10, grdCenterX, grdCenterY, 200);
grd.addColorStop(0, "#eee");
grd.addColorStop(1, "#aaa");
context.fillStyle = grd;
context.fill();
// set the line width and stroke color
context.lineWidth = 5;
context.strokeStyle = "#333";
context.stroke();
};
</script>
</head>
<body>
<canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
</canvas>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Canvas Shape
Clear a rectangle region on HTML5 canvas in...
Do shape composite in JavaScript
Draw Control points for quadratic Bezier cu...
Draw Fractals of lines in JavaScript
Draw Quadratic Bezier Curves in JavaScript
Draw a Bezier curve in JavaScript
Draw a arc with border and fill on HTML5 Ca...
Draw a circle in JavaScript
Draw a line on HTML5 canvas in JavaScript
Draw a line with shadow on HTML5 Canvas in ...
Draw a path with arc in JavaScript
Draw a path with lines in JavaScript
Draw a rectangle with border and fill on HT...
Draw a triangle on HTML5 Canvas in JavaScri...
Draw a zigzag path with lineTo method in Ja...
Draw an arc in JavaScript
Draw an arc with control points in JavaScri...
Draw spiral with lineTo method on HTML5 can...
Fill a rectangle in JavaScript
Set linecap on HTML5 canvas in JavaScript
Show Control points for Cubic Bezier Curves...
Use solid color to fill a triangle in JavaS...
Clear a rectangle region on HTML5 canvas in...
Do shape composite in JavaScript
Draw Control points for quadratic Bezier cu...
Draw Fractals of lines in JavaScript
Draw Quadratic Bezier Curves in JavaScript
Draw a Bezier curve in JavaScript
Draw a arc with border and fill on HTML5 Ca...
Draw a circle in JavaScript
Draw a line on HTML5 canvas in JavaScript
Draw a line with shadow on HTML5 Canvas in ...
Draw a path with arc in JavaScript
Draw a path with lines in JavaScript
Draw a rectangle with border and fill on HT...
Draw a triangle on HTML5 Canvas in JavaScri...
Draw a zigzag path with lineTo method in Ja...
Draw an arc in JavaScript
Draw an arc with control points in JavaScri...
Draw spiral with lineTo method on HTML5 can...
Fill a rectangle in JavaScript
Paint a cloud by using bezier curve in Java...
Set line join style in JavaScriptSet linecap on HTML5 canvas in JavaScript
Show Control points for Cubic Bezier Curves...
Use solid color to fill a triangle in JavaS...