@foblex/2d

中文文档
1.2.2发布于 2025/03/09MITTypeScriptESM + CJS近 30 天下载 35.5万

An Angular library for 2D geometric computations, providing classes and utilities for manipulating points, lines, vectors, rectangles, arcs, and transformations.

原始使用文档
文档目录10 节
  1. Overview
  2. Installation
  3. Usage
  4. Working with Points
  5. Working with Lines
  6. Vector Operations
  7. Working with Rectangles
  8. Handling Transformations
  9. Finding Intersections
  10. License

Foblex2D

An Angular library for 2D geometric computations, providing classes and utilities for manipulating points, lines, vectors, rectangles, arcs, and transformations. Used in Foblex Flow for 2D geometric operations.

Overview

The Foblex2D library is designed to simplify 2D geometric operations in TypeScript and Angular applications. It offers a rich set of classes and utility functions for:

  • Geometric Primitives: Points, Lines, Vectors, Rectangles, Arcs, and Rounded Rectangles.
  • Geometric Calculations: Distance, angle, intersection detection, and transformations.
  • Transformations: Handling position, scaling, and rotation transformations.
  • Intersection Detection: Finding intersections between line segments and various shapes, including rounded rectangles and arcs.

Whether you’re building graphical editors, games, data visualization tools, or any application that requires precise 2D geometric computations, Foblex2D provides the tools you need.

Installation

Install the library via npm:

  npm install @foblex/2D

Usage

Working with Points

Create and manipulate points:

import { Point, PointExtensions } from '@foblex/2D';

// Using the Point class
const pointA = new Point(10, 20);
const pointB = new Point(30, 40);

// Using PointExtensions for utility functions
const sumPoint = pointA.add(pointB);
const distance = PointExtensions.distance(pointA, pointB);

console.log(`Sum Point: (${sumPoint.x}, ${sumPoint.y})`);
console.log(`Distance between points: ${distance}`);

Working with Lines

Create and manipulate lines:

import { Line, LineExtensions } from '@foblex/2D';

const point1 = new Point(0, 0);
const point2 = new Point(10, 10);

const line = new Line(point1, point2);

// Calculate the length of the line
const length = LineExtensions.hypotenuse(line);

console.log(`Line length: ${length}`);

Vector Operations

Perform vector calculations:

import { VectorExtensions } from '@foblex/2D';

const vector1 = VectorExtensions.initialize(1, 0);
const vector2 = VectorExtensions.initialize(0, 1);

// Calculate the angle between two vectors
const angleRadians = VectorExtensions.angle(vector1, vector2);
const angleDegrees = (angleRadians * 180) / Math.PI;

console.log(`Angle between vectors: ${angleDegrees} degrees`);

Working with Rectangles

Create and manipulate rectangles:

import { RectExtensions, IRect, Point } from '@foblex/2D';

// Initialize a rectangle
const rect = RectExtensions.initialize(0, 0, 100, 50);

// Check if a point is inside the rectangle
const point = new Point(50, 25);
const isInside = RectExtensions.isIncludePoint(rect, point);

console.log(`Point is inside rectangle: ${isInside}`);

Handling Transformations

Work with transformations:

import { ITransformModel, TransformModelExtensions } from '@foblex/2D';

const transform = TransformModelExtensions.default();
transform.position = { x: 100, y: 50 };
transform.scale = 2;

// Get the transformation matrix string
const transformString = TransformModelExtensions.toString(transform);

console.log(`Transform Matrix: ${transformString}`);

Finding Intersections

Detect intersections between lines and shapes:

import { IntersectionFinder, RoundedRect, Point } from '@foblex/2D';

const fromPoint = new Point(0, 0);
const toPoint = new Point(150, 150);

const roundedRect = new RoundedRect(50, 50, 100, 100, 20, 20, 20, 20);

const intersections = IntersectionFinder.getIntersections(fromPoint, toPoint, roundedRect);

if (intersections.length > 0) {
  intersections.forEach((point, index) => {
    console.log(`Intersection ${index + 1}: (${point.x}, ${point.y})`);
  });
} else {
  console.log('No intersections found.');
}

License

This project is licensed under the MIT License.

By integrating Foblex2D into your Angular projects, you can simplify complex 2D geometric computations, enhance performance, and focus on building feature-rich applications.

版本与发布记录

@foblex/2d 共发布 13 个版本,首次发布于 2024/09/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.2latest2025/03/091.7 年前
1.2.12025/03/021.7 年前
1.2.02025/03/021.7 年前
1.1.92025/01/311.8 年前
1.1.82025/01/191.8 年前
1.1.72025/01/171.8 年前

关于 @foblex/2d 的常见问题

@foblex/2d 是什么?

An Angular library for 2D geometric computations, providing classes and utilities for manipulating points, lines, vectors, rectangles, arcs, and transformations.

如何安装 @foblex/2d?

在项目目录运行 npm install @foblex/2d,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=12.0.0)、@angular/core(>=12.0.0)、rxjs(>=6.6.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@foblex/2d 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@foblex/2d 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@foblex/2d 可以商用吗?

@foblex/2d 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@foblex/2d 的下载量有多少?

根据 npm 官方下载统计,@foblex/2d 在 2026/10/15 – 2026/11/13 这 30 天内被下载 355,308 次,日均约 11,844 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@foblex/2d 还在维护吗?

最新版本 1.2.2 发布于 2025/03/09(1.7 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/09/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。