UNPKG

@avolutions/canvas-painter

Version:

CanvasPainter.js is a simple yet powerful JavaScript library for drawing basic shapes (rectangles, circles, etc.) on HTML5 Canvas with ease. Perfect for creating 2D graphics in your web projects.

160 lines (159 loc) 4.91 kB
/** * Class representing an angle, which can be defined in degrees or radians. * Provides functionality for normalization and angle adjustments. */ export class Angle { /** The internal degrees value of the angle. */ _degrees; /** Boolean flag indicating whether the angle should be normalized. */ _normalized; /** * Constructs an Angle instance. * * @param degrees - The initial angle in degrees. * @param normalized - Whether the angle should be normalized to the range [0, 360). */ constructor(degrees, normalized = false) { this._normalized = normalized; this.degrees = degrees; } /** * Serializes the object’s properties into an array, consisting of * the degrees, radians, and the result of the `isNormalized()` method. * * @returns An array containing the degrees as a number, * the radians as a number, and the result of `isNormalized()` as a boolean. */ toArray() { return [ this.degrees, this.radians, this.isNormalized() ]; } /** * Serializes the object’s properties into a JSON string. The JSON object contains * the degrees, radians, and the result of the `isNormalized()` method. * * @returns A JSON string representation of the object, including degrees, * radians, and whether it is normalized. */ toJson() { const json = { "degrees": this.degrees, "radians": this.radians, "isNormalized": this.isNormalized() }; return JSON.stringify(json); } /** * Gets the current angle in degrees. * @returns The angle in degrees. */ get degrees() { return this._degrees; } /** * Gets the current angle in radians. * @returns The angle in radians. */ get radians() { return Angle.degreesToRadians(this.degrees); } /** * Sets the angle in degrees, optionally normalizing it if required. * @param degrees - The new angle in degrees. */ set degrees(degrees) { this._degrees = this.isNormalized() ? Angle.normalize(degrees) : degrees; } /** * Sets the angle in radians by converting it to degrees. * @param radians - The new angle in radians. */ set radians(radians) { this.degrees = Angle.radiansToDegrees(radians); } /** * Creates a new Angle instance from degrees. * @param degrees - The angle in degrees. * @param normalized - Whether the angle should be normalized. * @returns A new Angle instance. */ static fromDegrees(degrees, normalized = false) { return new Angle(degrees, normalized); } /** * Creates a new Angle instance from radians. * @param radians - The angle in radians. * @param normalized - Whether the angle should be normalized. * @returns A new Angle instance. */ static fromRadians(radians, normalized = false) { const degrees = Angle.radiansToDegrees(radians); return new Angle(degrees, normalized); } /** * Normalizes the angle to the range [0, 360). */ normalize() { this._normalized = true; this.degrees = this._degrees; // Reapply normalization logic } /** * Checks if the angle is normalized. * @returns True if the angle is normalized, false otherwise. */ isNormalized() { return this._normalized; } /** * Gets the normalized value of the angle in degrees. * @returns The normalized angle in degrees. */ getNormalized() { return Angle.normalize(this._degrees); } /** * Adjusts the angle by a given number of degrees. * @param degrees - The amount to adjust the angle by, in degrees. */ adjustBy(degrees) { this.degrees += degrees; } /** * Adjusts the angle by a given number of radians. * @param radians - The amount to adjust the angle by, in radians. */ adjustByRadians(radians) { this.radians += radians; } /** * Converts degrees to radians. * @param degrees - The angle in degrees. * @returns The angle in radians. */ static degreesToRadians(degrees) { return degrees * (Math.PI / 180); } /** * Converts radians to degrees. * @param radians - The angle in radians. * @returns The angle in degrees. */ static radiansToDegrees(radians) { return radians * (180 / Math.PI); } /** * Normalizes an angle to the range [0, 360). * @param degrees - The angle in degrees. * @returns The normalized angle in degrees. */ static normalize(degrees) { let normalized = degrees % 360; if (normalized < 0) { normalized += 360; } return normalized; } }