letradiusSlider;letradius=0;functionsetup(){createCanvas(400,400);// Create the radius sliderradiusSlider=createSlider(0,200,100);radiusSlider.position(10,10);}functiondraw(){background(220);// Fetch the current radius from the sliderradius=radiusSlider.value();// Draw the circlefill(0,0,255);noStroke();circle(width/2,height/2,radius*2);// Display the radius valuefill(0);text("Radius: "+radius,10,50);}
// Circle MicroSim with responsive design// Canvas dimensionsletcanvasWidth=400;letdrawHeight=400;letcontrolHeight=50;letcanvasHeight=drawHeight+controlHeight;letmargin=25;letsliderLeftMargin=100;letdefaultTextSize=16;// Global variables for width and heightletcontainerWidth;// calculated by container upon resizeletcontainerHeight=canvasHeight;// fixed height on page// Circle parametersletradius=60;letradiusSlider;functionsetup(){// Create a canvas to match the parent container's sizeupdateCanvasSize();constcanvas=createCanvas(containerWidth,containerHeight);canvas.parent(document.querySelector('main'));textSize(defaultTextSize);// Create slider for radius controlradiusSlider=createSlider(0,200,radius,1);radiusSlider.position(sliderLeftMargin,drawHeight+12);radiusSlider.size(canvasWidth-sliderLeftMargin-20);}functiondraw(){// Draw the display areafill('aliceblue');stroke('silver');strokeWeight(1);rect(0,0,canvasWidth,drawHeight);// Draw the controls areafill('white');rect(0,drawHeight,canvasWidth,controlHeight);// Get the current radius from the sliderradius=radiusSlider.value();// Draw the circlefill('blue');noStroke();circle(canvasWidth/2,drawHeight/2,radius*2);// Draw the label and valuefill('black');strokeWeight(0);textSize(defaultTextSize);textAlign(LEFT,CENTER);text('Radius: '+radius,10,drawHeight+20);}functionwindowResized(){// Update canvas size when the container resizesupdateCanvasSize();resizeCanvas(containerWidth,containerHeight);// Resize the slider to match the new canvasWidthradiusSlider.size(canvasWidth-sliderLeftMargin-20);}functionupdateCanvasSize(){// Get the exact dimensions of the containerconstcontainer=document.querySelector('main').getBoundingClientRect();containerWidth=Math.floor(container.width);// Avoid fractional pixelscanvasWidth=containerWidth;}