npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gantfdt/2d-viewer-core

v0.1.1

Published

gant-2d 核心:GDF 契约、acad-ts 本地解析、WebGL2 渲染、交互与内置 UI(框架无关)

Readme

@gantfdt/2d-viewer-core

浏览器端的 2D 图纸预览内核:DXF(ASCII / 二进制)与 DWG 全部在本地解析,WebGL2 渲染并以 Canvas2D 降级,内置一套可直接使用的查看器 UI(框架无关)。

当前版本 0.1.0。许可证为 UNLICENSED(源码可见,使用需单独授权),见包内 LICENSE。

能力

  • 格式:ASCII DXF、二进制 DXF、DWG。解析在 Web Worker 内进行,环境不支持 Worker 时自动退回主线程。
  • 渲染:WebGL2 为主,能力不足时自动降到 Canvas2D,并通过 rendererchange 事件告知。
  • 交互:缩放、平移、旋转、适配视图、图层可见性与隔离、点选实体、全文搜索、多布局切换、导出 PNG、全屏、左右面板折叠。
  • UI:工具条、图层面板、信息面板、状态栏、画布覆盖层,以及四个宿主插槽;内置中文与英文文案。
  • 默认离线:offline 默认为 true,不发任何后端请求。

安装

pnpm add @gantfdt/2d-viewer-core

引入样式

样式不会自动注入,必须由宿主引入一次:

import '@gantfdt/2d-viewer-core/style.css';

style.css 声明全部 --g2d-* 设计 token;内置字体资产尚未随包提供,文字使用浏览器可用字体。忘记引入时查看器仍可运行,但控制台会输出一次提示,UI 也没有样式。

最小示例

import { createViewer } from '@gantfdt/2d-viewer-core';
import '@gantfdt/2d-viewer-core/style.css';

const viewer = createViewer({
  container: document.querySelector('#viewer') as HTMLElement,
  height: '100%', // 数字按像素;字符串原样(如 calc(100vh - 64px))
  theme: 'light',
  locale: 'zh',
});

viewer.on({ event: 'load', handler: (result) => console.log(result.stats.entityCount) });
viewer.on({ event: 'error', handler: (error) => console.error(error.code, error.messageKey) });

// URL 由前端下载;也可以传 file / base64 / 已归一化的 gdf
await viewer.load({ source: { type: 'url', url: '/samples/plan.dxf' } });

// 卸载(移除 DOM、释放 Worker 与监听)
viewer.destroy();

图纸来源

| source.type | 字段 | 说明 | | ------------- | ------------------------------- | ---------------------------------------- | | url | url、headers?、format? | 由前端 fetch 下载;失败为 FETCH_FAIL | | file | file: File \| Blob、format? | 本地文件,不经过网络 | | base64 | data、format? | 允许带 data: 前缀 | | gdf | gdf: GDFDocument | 已归一化的文档,短路、不读字节 |

format 只是提示,实际以嗅探结果为准。

选项

| 选项 | 默认 | 说明 | | --------------------- | ----------------- | ----------------------------------------------- | | renderer | auto | auto 优先 WebGL2,失败退回 Canvas2D | | height | 样式默认 | 根元素高度:数字为 px,字符串原样 | | offline | true | 离线模式;不发后端请求 | | theme / tokens | light / — | 初始主题与 token 覆盖(键名不含 --g2d- 前缀) | | locale / messages | zh / — | 初始语言与外部文案(局部覆盖,同名以外部为准) | | precision | 2 | 状态栏坐标显示的小数位 | | worker / padding | true / 内置常量 | 是否用 Worker 解析、适配视图的留白比例 | | ui | 全开 | 内置 UI 开关(UIConfig) | | slots | — | 宿主插槽(见下) |

方法

| 分组 | 方法 | | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 加载 | load({ source })、retryLoad()、cancelLoad()、getDocument()、getLoadResult()、getLastError()、clearError()、isLoading()、getProgress() | | 视图 | fit()、zoomBy()、zoomTo()、zoomToBounds()、zoomToSelection()、panBy()、rotate()、resetView()、getView()、setView()、getZoomPercent()、screenToWorld() | | 图层 | getLayers()、getLayerIds()、setLayerVisible()、setLayersVisible()、setLayerState()、isolateLayer()、showAllLayers()、invertLayerVisibility() | | 选择 | pick({ point })、select({ ids })、clearSelection()、getSelection()、getEntity({ id })、selectionBounds() | | 搜索与布局 | search({ query })、focusResult()、getLayouts()、setActiveLayout()、getActiveLayout() | | 显示 | setHeight()、setPrecision()(均有对应 getter) | | 导出 | exportImage({ format?, scale?, quality?, area? }) | | UI | setPanelCollapsed()、setFullscreen()、getState() | | 事件 | on()、once()、off()、emitEvent() | | 其他 | t(key, vars?)(按当前语言取文案)、destroy() |

公共 API 一律对象入参(load({ source })),不提供位置参数。

事件

| 事件 | 载荷 | 触发时机 | | ----------------- | ----------------------- | ------------------------------------------------ | | ready | — | 查看器初始化完成 | | loadprogress | { p } | 下载 / 解析进度(0–1) | | load | LoadResult | 加载成功 | | degraded | { degradations } | 加载成功但存在降级 | | error | G2DError | 加载失败(code + messageKey) | | viewchange | ViewState | 相机变化(每帧最多一次) | | layerchange | { ids } | 图层状态变化 | | layoutchange | { id } | 切换布局 | | selectionchange | EntityInfo[] | 选择变化 | | search | SearchResult[] | 搜索完成 | | themechange | { theme } | 主题变化 | | localechange | { locale } | 语言变化 | | rendererchange | { renderer, reason? } | 实际渲染器变化;降级时 reason 为初始化失败原因 |

内置 UI 与插槽

createViewer({
  container,
  ui: {
    toolbar: true,
    layerPanel: true,
    infoPanel: true, // 内置「图纸」页签
    statusBar: true,
    overlays: true,
    collapsed: { left: false, right: false },
    shortcuts: true,
  },
});

四个插槽:toolbar-start、toolbar-end、layer-panel-footer、info-tab。每个插槽可挂多条,按数组顺序渲染;info-tab 的 title 用作页签名,首次切到该页签时才调用 render,返回的函数在销毁时执行。

const slots = {
  'toolbar-end': [
    {
      render: (ctx) => {
        const button = document.createElement('button');
        button.textContent = ctx.t('info.drawing');
        ctx.container.append(button);
        return () => button.remove();
      },
    },
  ],
};

已知限制

  • 文字由独立的 2D 叠加层绘制(WebGL2 与 Canvas2D 的字形与缩放行为一致),未使用字形图集;可见文字很多的图纸下,文字层开销随文字数线性增长。
  • 文字用浏览器可用字体绘制(内置字体资产尚未随包提供),不加载图纸自带的 TTF / SHX,跨平台字形可能有差异。
  • 当前没有后端解析:所有格式都在浏览器本地解析。
  • 二进制 DXF 与 DWG 的兼容范围以实际解析结果为准。