Question: * Computer Graphics Define a slider that changes the radius of the shaded circle. Be sure that the radius value is greater than zero. circle.js

*Computer Graphics
Define a slider that changes the radius of the shaded circle. Be sure that the radius value is greater than zero.
circle.js
class Circle2D {
static vertexPositions =[];
static vertexColors =[];
static shaderProgram =-1;
static positionBuffer =-1;
static colorBuffer =-1;
static aPositionShader =-1;
static aColorShader =-1;
static initialize(){
Circle2D.shaderProgram = initShaders(gl,"/vshader.glsl","/fshader.glsl");
const radius =0.2;
const center = vec2(0.8- radius, 0.95- radius);
const numSegments =30;
// First vertex (3 o'clock) with black color
Circle2D.vertexPositions.push(vec2(center[0]+ radius, center[1]));
Circle2D.vertexColors.push(vec3(0.0,0.0,0.0));
for (let i =0; i = numSegments; i++){
const angle =(i / numSegments)*2* Math.PI;
const x = radius * Math.cos(angle)+ center[0];
const y = radius * Math.sin(angle)+ center[1];
// Vary red color as a function of angle
const fadeFactor =0.45;
const red =0.5+0.5* Math.cos(15)* fadeFactor;
Circle2D.vertexPositions.push(vec2(x, y));
Circle2D.vertexColors.push(vec3(red,0.0,0.0));
}
// Load the data into the GPU
Circle2D.positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, Circle2D.positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(Circle2D.vertexPositions), gl.STATIC_DRAW);
Circle2D.colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, Circle2D.colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(Circle2D.vertexColors), gl.STATIC_DRAW);
Circle2D.aPositionShader = gl.getAttribLocation(Circle2D.shaderProgram, "aPosition");
Circle2D.aColorShader = gl.getAttribLocation(Circle2D.shaderProgram, "aColor");
}
constructor(){
if (Circle2D.shaderProgram ===-1) Circle2D.initialize();
}
draw(){
gl.useProgram(Circle2D.shaderProgram);
gl.bindBuffer(gl.ARRAY_BUFFER, Circle2D.positionBuffer);
gl.vertexAttribPointer(Circle2D.aPositionShader, 2, gl.FLOAT, false, 0,0);
gl.bindBuffer(gl.ARRAY_BUFFER, Circle2D.colorBuffer);
gl.vertexAttribPointer(Circle2D.aColorShader, 3, gl.FLOAT, false, 0,0);
gl.enableVertexAttribArray(Circle2D.aPositionShader);
gl.enableVertexAttribArray(Circle2D.aColorShader);
gl.drawArrays(gl.TRIANGLE_FAN, 0, Circle2D.vertexPositions.length);
gl.disableVertexAttribArray(Circle2D.aPositionShader);
gl.disableVertexAttribArray(Circle2D.aColorShader);
}
}
app.js
var canvas;
var gl;
var sq;
var sq2;
var sq3;
var sq4;
var sq5;
var sq6;
var triangle;
var circle;
var ellipse;
// Function to handle button click event
function rotateEllipse(){
ellipse.rotate(90); // Call a function to rotate the ellipse by 90 degrees
render(); // Render the updated scene
}
window.onload = function init(){
canvas = document.getElementById("gl-canvas" );
gl = canvas.getContext('webgl2');
if (!gl ){ alert( "WebGL 2.0 isn't available" ); }
gl.viewport(0,0, canvas.width, canvas.height );
gl.clearColor(0.0,0.0,0.0,1.0);
// Create an event listener for the rotate button
const rotateButton = document.getElementById('rotateButton');
rotateButton.addEventListener('click', rotateEllipse);
sq = new Square();
sq2= new Square2();
sq3= new Square3();
sq4= new Square4();
sq5= new Square5();
sq6= new Square6();
triangle = new Triangle2D();
circle = new Circle2D();
ellipse = new Ellipse2D();
render();
};
function render(){
gl.clear( gl.COLOR_BUFFER_BIT );
sq.draw();
sq2.draw();
sq3.draw();
sq4.draw();
sq5.draw();
sq6.draw();
triangle.draw();
circle.draw();
ellipse.draw();
}
index.html
Not supported
Rotate Ellipse
 *Computer Graphics Define a slider that changes the radius of the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!