@axah/react-svg-editor

中文文档
2.2.1发布于 2026/09/18TypeScriptESM + CJS构建来源已验证近 30 天下载 3649
原始使用文档
文档目录5 节
  1. Installation
  2. Quick Example
  3. Key Concepts
  4. Development
  5. Release

@axah/react-svg-editor

A composable React SVG editor library. Supports drawing shapes, freehand paths, text, cropping, zooming, and dragging — all within a scalable SVG canvas backed by a PDF or image source.

Installation

pnpm add @axah/react-svg-editor
# or
npm install @axah/react-svg-editor

Peer dependencies (install separately):

pnpm add react react-dom pdfjs-dist

Quick Example

import { useState } from 'react';
import {
  BackgroundSource,
  UncontrolledEditor,
  Artboard,
  Drawables,
  Cropables,
  translateDrawable,
  resizeDrawable,
} from '@axah/react-svg-editor';
import type { Drawable, Crop } from '@axah/react-svg-editor';

const pdfjs = async () => {
  const lib = await import('pdfjs-dist');
  lib.GlobalWorkerOptions.workerSrc = (
    await import('pdfjs-dist/build/pdf.worker?worker&url')
  ).default;
  return lib;
};

export default function App() {
  const [drawMode, setDrawMode] = useState<'rect' | 'pen' | 'ellipse' | 'line' | 'text' | 'crop' | null>(null);
  const [drawables, setDrawables] = useState<ReadonlyArray<Drawable>>([]);
  const [crop, setCrop] = useState<Crop | undefined>();

  return (
    <BackgroundSource source="/document.pdf" pdfjs={pdfjs} hqPdf>
      {(source) => {
        if (source.state !== 'LOADED') return <div>{source.state}</div>;

        return (
          <UncontrolledEditor
            allowDrag={drawMode === null}
            drawMode={drawMode}
            backgroundUrl={source.url}
            width={source.width}
            height={source.height}
            rotate={0}
          >
            <Artboard
              drawMode={drawMode}
              width={source.width}
              height={source.height}
              drawingFill="transparent"
              drawingStroke="#027180"
              drawingStrokeWidth={4}
              text={['Hello', 'World']}
              fontSize={16}
              minWidth={0}
              minHeight={0}
              onDrawStart={() => {}}
              onDrawEnd={(d) => setDrawables((prev) => [...prev, d])}
              onCropStart={() => {}}
              onCropEnd={setCrop}
            >
              <Drawables
                drawables={drawables}
                diStrokeWidth={3}
                width={source.width}
                height={source.height}
                onSelectDrawable={() => {}}
                onDrawableTranslate={(id, x, y) =>
                  setDrawables((prev) =>
                    prev.map((d) => (d.id === id ? translateDrawable(d, x, y) : d)),
                  )
                }
                onResizeDrawable={(_e, id, hX, hY, nX, nY) =>
                  setDrawables((prev) =>
                    prev.map((d) => (d.id === id ? resizeDrawable(d, hX, hY, nX, nY) : d)),
                  )
                }
              />
              <Cropables
                width={source.width}
                height={source.height}
                diStrokeWidth={3}
                crop={crop}
                canTransformCrop={drawMode === 'crop'}
                onCropTranslate={(x, y) =>
                  setCrop((c) => c && { ...c, x: c.x + x, y: c.y + y })
                }
                onResizeCrop={(hX, hY, nX, nY) => {
                  /* resize crop logic */
                }}
                onRemoveCrop={() => setCrop(undefined)}
                onConfirmCrop={() => setDrawMode(null)}
              />
            </Artboard>
          </UncontrolledEditor>
        );
      }}
    </BackgroundSource>
  );
}

Key Concepts

ComponentPurpose
BackgroundSourceLoads a PDF or image URL and provides dimensions + blob URL
EditorControlled SVG canvas with zoom, pan, rotation
UncontrolledEditorWrapper around Editor that manages zoom/translate state internally
ArtboardRoutes draw events to the active draw mode (rect, ellipse, line, pen, text, crop)
DrawablesRenders all drawn shapes with selection and resize handles
CropablesRenders and manages the crop region overlay
translateDrawablePure helper to translate a drawable by (x, y)
resizeDrawablePure helper to resize a drawable via a handle corner

Development

# install dependencies
pnpm install

# start the dev preview app
pnpm start

# type-check
pnpm lint

# build the library
pnpm build

# check package exports compatibility
pnpm check:types

The preview app lives in src/App.tsx and loads a local PDF from public/pdf-test.pdf.

The library source is in lib/.


Release

Releases are managed via Changesets.

# create a changeset
pnpm changeset

# publish (runs lint + type checks + build first)
pnpm release

版本与发布记录

@axah/react-svg-editor 共发布 31 个版本,首次发布于 2021/02/01;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.1latest2026/09/181 个月前
0.0.0-20260918082854canary2026/09/181 个月前
2.2.02026/08/132 个月前
0.0.0-202608130753192026/08/132 个月前
2.1.02026/06/094 个月前
2.0.82026/05/195 个月前

关于 @axah/react-svg-editor 的常见问题

如何安装 @axah/react-svg-editor?

在项目目录运行 npm install @axah/react-svg-editor,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=19)、react-dom(>=19)、pdfjs-dist(>=6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@axah/react-svg-editor 支持 TypeScript 吗?

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

@axah/react-svg-editor 支持 ESM 还是 CommonJS?

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

@axah/react-svg-editor 可以商用吗?

@axah/react-svg-editor 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@axah/react-svg-editor 的下载量有多少?

根据 npm 官方下载统计,@axah/react-svg-editor 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,649 次,日均约 122 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@axah/react-svg-editor 还在维护吗?

最新版本 2.2.1 发布于 2026/09/18(1 个月前);至今共发布 31 个版本,其中过去 12 个月发布了 9 个;首次发布于 2021/02/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。