1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| document.addEventListener("DOMContentLoaded", function() {
var $canvas = document.getElementById("camembert");
var context = $canvas.getContext("2d");
var width = $canvas.width;
var height = $canvas.height;
// le rayon doit faire moins de la moitié de la plus petite dimension, j'ai choisi 1/3
var rayon = Math.min(width, height) / 3;
// on place le point d'origine au centre du canevas
context.translate(width / 2, height / 2);
// on commence le tracé
context.beginPath();
context.moveTo(0, 0);
context.lineTo(rayon, 0);
// la partie courbe
// 2/3 de pi = 120°
context.arc(0, 0, rayon, 0, Math.PI * 2/3);
// on revient au point de départ du tracé
context.closePath();
// on remplit
context.fillStyle = "orange";
context.fill();
// on trace le contour
context.strokeStyle = "gray";
context.lineWidth = 2;
context.stroke();
}); |
Partager