You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
903 B

2 years ago
function draw_roulette(ctx, w, h, datalist){
var radius = h / 2 - 5;
var center = new Array(w / 2, h / 2);
var total = 0;
var sum = datalist.reduce((acc, value) => acc + value, total);
var lastend = 0;
var offset = Math.PI / 2;
for (i=0; i < datalist.length; i++){
var part = datalist[i];
2 years ago
2 years ago
ctx.beginPath();
ctx.fillStyle = colist[i];
ctx.moveTo(center[0], center[1]);
var arcsector = Math.PI * (2 * part / total);
ctx.arc(center[0], center[1], radius, lastend - offset, lastend + arcsector - offset, false);
ctx.lineTo(center[0], center[1]);
ctx.fill();
ctx.closePath();
lastend += arcsector;
}
2 years ago
}
var datalist= new Array(35, 25, 20, 12, 7, 1);
var colist = new Array('blue', 'red', 'green', 'orange', 'gray', 'yellow');
var canvas = document.getElementById("roulette");
var rendu = canvas.getContext('2d');
draw_roulette(rendu, canvas.width, canvas.height, datalist);