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.

65 lines (64 loc) 1.82 kB
import { Serializable } from "../common/Serializable.js"; /** * Class representing a 2D point with x and y coordinates. */ export class Point extends Serializable { /** * The x-coordinate of the point. */ x; /** * The y-coordinate of the point. */ y; /** * Creates an instance of Point. * * @param x - The x-coordinate of the point. * @param y - The y-coordinate of the point. */ constructor(x, y) { super(); this.x = x; this.y = y; } /** * Moves the point along the x-axis by a specified delta. * * @param delta - The amount to move the point along the x-axis. */ moveX(delta) { this.move(delta, 0); } /** * Moves the point along the y-axis by a specified delta. * * @param delta - The amount to move the point along the y-axis. */ moveY(delta) { this.move(0, delta); } /** * Moves the point by a specified delta along the x and y axes. * * @param deltaX - The amount to move the point along the x-axis (default is 0). * @param deltaY - The amount to move the point along the y-axis (default is 0). */ move(deltaX = 0, deltaY = 0) { this.x += deltaX; this.y += deltaY; } /** * Adjusts the point's coordinates to remove the effects of a specified pan offset and zoom level. * * @param offset - The pan offset to remove from the current point's coordinates. * @param zoom - The zoom level to reverse from the current point's coordinates. * * @returns The current point with out transformation. */ asUntransformed(offset, zoom) { this.x = (this.x - offset.x) / zoom; this.y = (this.y - offset.y) / zoom; return this; } }