Skip to content

Instantly share code, notes, and snippets.

@benbotto
Last active March 25, 2020 16:21
Show Gist options
  • Save benbotto/bc6a79c31f5c1736bb75bc1435db9425 to your computer and use it in GitHub Desktop.
Save benbotto/bc6a79c31f5c1736bb75bc1435db9425 to your computer and use it in GitHub Desktop.
A rectangle orbiting about the origin.
class Rectangle {
constructor(gl, color, width, height) {
this.gl = gl;
this.color = color;
this.width = width;
this.height = height;
// Rotation, updated before each render.
this.rotation = gl.mat2d.create();
// Translation--the point through which the Rectangle should pass.
this.translation = gl.mat2d.fromTranslation(
gl.mat2d.create(),
gl.vec2.fromValues(40, 40));
}
/**
* Update the Rectangle's transformation matrix before a render.
*/
update(elapsedMs) {
// Rotate PI per second.
this.gl.mat2d.rotate(this.rotation, this.rotation,
Math.PI * elapsedMs / 1000);
}
/**
* Get the Rectangle's transformation matrix.
*/
getTransformation() {
// Rotate then translate.
return this.gl.mat2d.multiply(
this.gl.mat2d.create(),
this.rotation,
this.translation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment