<!DOCTYPE html> <html> <head> <title>Cylinder Figure on Canvas</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="myCanvas" width="400" height="300"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var centerX = canvas.width / 2; var centerY = canvas.height / 2; var radius = 80; var height = 150; var layerHeight = 30; var numLayers = 3; // Draw the base of the cylinder ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI); ctx.fillStyle = "#ff0000"; ctx.fill(); ctx.closePath(); // Draw the layers at the base for (var i = 1; i <= numLayers; i++) { var layerY = centerY + (height / 2) - (i * layerHeight); ctx.beginPath(); ctx.arc(centerX, layerY, radius, 0, 2 * Math.PI); ctx.fillStyle = "#00ff00"; ctx.fill(); ctx.closePath(); } // Draw the top of the cylinder var topY = centerY - (height / 2); ctx.beginPath(); ctx.arc(centerX, topY, radius, 0, 2 * Math.PI); ctx.fillStyle = "#ff0000"; ctx.fill(); ctx.closePath(); // Draw the layers at the top for (var i = 1; i <= numLayers; i++) { var layerY = topY + (i * layerHeight); ctx.beginPath(); ctx.arc(centerX, layerY, radius, 0, 2 * Math.PI); ctx.fillStyle = "#00ff00"; ctx.fill(); ctx.closePath(); } </script> </body> </html>