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.

144 lines (143 loc) 5.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Circle = void 0; const InvalidConstructorArgumentsError_js_1 = require("../errors/InvalidConstructorArgumentsError.js"); const CircleDefinition_js_1 = require("../definitions/CircleDefinition.js"); const CircleOptions_js_1 = require("../options/CircleOptions.js"); const CircleStyle_js_1 = require("../styles/CircleStyle.js"); const Point_js_1 = require("../types/Point.js"); const Shape_js_1 = require("./Shape.js"); /** * Class representing a circle, extending the Shape class with a CircleDefinition. * Provides functionality for rendering, resizing and moving the circle. */ class Circle extends Shape_js_1.Shape { /** * Creates an instance of the `Circle` class. * * The `Circle` can be created either by passing a `Point` object representing the center, * or by providing the individual coordinates for the center. * * @throws {@link InvalidConstructorArgumentsError} if invalid arguments are passed. */ constructor(arg1, arg2, arg3, arg4, arg5) { let definition; let style; let options; if (typeof arg1 === 'number' && typeof arg3 === 'number') { // Constructor with coordinates const center = new Point_js_1.Point(arg1, arg2); definition = new CircleDefinition_js_1.CircleDefinition(center, arg3); style = arg4; options = arg5; } else if (arg1 instanceof Point_js_1.Point && typeof arg3 !== 'number' && arg5 === undefined) { // Constructor with Point object definition = new CircleDefinition_js_1.CircleDefinition(arg1, arg2); style = arg3; options = arg4; } else { throw new InvalidConstructorArgumentsError_js_1.InvalidConstructorArgumentsError(); } super(definition, new CircleStyle_js_1.CircleStyle(style), new CircleOptions_js_1.CircleOptions(options)); } // Getters /** * Gets the center point of the circle. * * @returns The center point of the circle. */ get center() { return this._definition.center; } /** * Gets the radius of the circle. * * @returns The radius of the circle. */ get radius() { return this._definition.radius; } // Setters /** * Sets the center point of the circle. * * @param center - The new center point of the circle. */ set center(center) { this._definition.center = center; } /** * Sets the radius of the circle. * * @param radius - The new radius of the circle. */ set radius(radius) { this._definition.radius = radius; } /** * Moves the circle by adjusting the current center by delta values. * * @param deltaX - The change in the x-coordinate. * @param deltaY - The change in the y-coordinate. */ move(deltaX = 0, deltaY = 0) { this.center.move(deltaX, deltaY); } /** * Resizes the circle by adjusting the current radius by delta values. * * @param delta - The change of the radius. */ resize(delta) { this.radius += delta; } /** * Renders the circle on the canvas using the provided 2D rendering context. * * The circle will be rendered with its current position and radius size. * * @param context - The 2D rendering context of the canvas where the circle will be drawn. */ render(context) { context.save(); // Save the current canvas state // Set circle specific styles context.fillStyle = this.stateStyle.color; context.beginPath(); context.arc(this.center.x, this.center.y, this.radius, 0, Math.PI * 2); context.fill(); // Draw border for circle if (this.hasBorder()) { context.strokeStyle = this.stateStyle.borderColor; context.lineWidth = this.stateStyle.borderWidth; context.stroke(); } context.restore(); // Restore the canvas state to before the transformations } /** * Determines if the mouse is currently over the shape. * * @param mousePosition - The current mouse position. * @returns True if the mouse is over the shape, false otherwise. */ isMouseOver(mousePosition) { const dx = mousePosition.x - this.center.x; const dy = mousePosition.y - this.center.y; const distance = Math.sqrt(dx * dx + dy * dy); // Adjust radius to include the border width const borderRadius = this.hasBorder() ? this.stateStyle.borderWidth / 2 : 0; const effectiveRadius = this.radius + borderRadius; // Check if the distance is within the circle's radius return distance <= effectiveRadius; } /** * Handles the drag operation by applying the given delta to the current position. * * @param delta - The change in position represented as a `Point`. */ onDrag(delta) { this.move(delta.x, delta.y); } } exports.Circle = Circle;