how can i draw a semi circle on a canvas in js? the line should have rounded borders
- User
how can i draw a semi circle on a canvas in js? the line should have rounded borders
Assistant
Here is how you can draw a semi-circle with rounded borders on a canvas in JavaScript:
// Get the canvas and context const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); // Set the line properties ctx.lineWidth = 10; ctx.lineJoin = 'round'; ctx.lineCap = 'round'; // Set the arc properties const x = 100; const y = 100; const radius = 50; const startAngle = 0; const endAngle = Math.PI; // 180 degrees // Draw the semi-circle ctx.beginPath(); ctx.arc(x, y, radius, startAngle, endAngle); ctx.stroke();The key things here are:
- Set
lineJoinandlineCapto'round'to get rounded endpoints - Use
arc()to draw a segment of a circle by defining start and end angles - Convert 180 degrees to radians by using
Math.PIas the end angle
- Set
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.