EN
JavaScript - how to fill ellipse with certain color on canvas element?
7 points
Simple solution:
xxxxxxxxxx
1
context.fillStyle = color;
2
3
context.beginPath();
4
context.ellipse(x, y, radiusX, radiusY, rotation, 0, 2 * Math.PI);
5
context.fill();
Using JavaScript it is possible to draw filled ellipse with certain color in following way.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style> #my-canvas { border: 1px solid gray; } </style>
5
</head>
6
<body>
7
<canvas id="my-canvas" width="200" height="200"></canvas>
8
<script>
9
10
var canvas = document.querySelector('#my-canvas');
11
var context = canvas.getContext('2d');
12
13
function fillEllipse(x, y, radiusX, radiusY, rotation, color) {
14
context.fillStyle = color;
15
context.beginPath();
16
context.ellipse(x, y, radiusX, radiusY, rotation, 0, 2 * Math.PI);
17
context.fill();
18
}
19
20
var deg = 20;
21
var rad = deg * (Math.PI / 180.0);
22
23
// Eclipse paramters:
24
// x=100; y=100;
25
// width=80; height=40;
26
// rotation=20; // degrees - clockwise rotation according to point (x, y)
27
// color='blue'
28
fillEllipse(100, 100, 80, 40, rad, 'blue');
29
30
</script>
31
</body>
32
</html>