JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="mycanvas" width="400" height="250"> </canvas>
CSS
#mycanvas{
border: 1px solid black;
}
JavaScript
function dix180(){
let nb=0;
while (nb<10){
nb=Math.round(Math.random()*180);
}
return nb
}
function mesureangle(x,y) {
let x1 = x;
let y1 = y;
let length1 = 100;
let angle1 = dix180();
let angle2 = angle1 + dix180();
// Convertir les angles en radians
let angleRad1 = (angle1 * Math.PI) / 180;
let angleRad2 = (angle2 * Math.PI) / 180;
console.log(angle2-angle1);
// Calculer les coordonnées de la première ligne
let x2 = x1 + length1 * Math.cos(angleRad1);
let y2 = y1 + length1 * Math.sin(angleRad1);
let x3 = x1 + length1 * Math.cos(angleRad2);
let y3 = y1 + length1 * Math.sin(angleRad2);
let canvas = document.getElementById('mycanvas');
let ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x3, y3);
ctx.stroke();
ctx.font = "30px Arial";
ctx.fillText((angle2-angle1).toString() +' °',20,50);
}
mesureangle(200,125);