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

free-draw-svg

v1.0.8

Published

A mobile Drawing Tool based on SVG for React

Downloads

44

Readme

FreeDraw 一个基于svg的移动端react自定义画图工具

介绍

前端绘画工具,可以在屏幕上自由绘画,基于 Pointer Events 实现,同时支持触摸、鼠标和触控笔(含压感)。

使用

基础用法(命令式 API)

import { FreeDraw } from 'free-draw-svg';

const App = () => {
  const drawer = FreeDraw({
    strokeColor: 'red',
  });

  setTimeout(() => {
    drawer.destroy();
  }, 5000);
  return (
    <></>
  );
};

命令式 API 应在事件回调(如 onClick、useEffect)中调用,不要在渲染期间直接调用。

声明式用法(组件 + ref)

import { useRef } from 'react';
import { FreeDrawCanvas, FreeDrawRef } from 'free-draw-svg';

const App = () => {
  const ref = useRef<FreeDrawRef>(null);

  return (
    <>
      <FreeDrawCanvas ref={ref} strokeColor="red" strokeWidth={5} />
      <button onClick={() => ref.current?.undo()}>撤销</button>
    </>
  );
};

支持多笔绘制

默认只保留最新一笔,抬手重新落笔会清空上一笔。传入 multiple 可以叠加保留多笔:

FreeDraw({
  strokeColor: 'red',
  multiple: true,
});

自定义线条颜色、宽度和背景

const drawer = FreeDraw({
  strokeColor: 'red',
  strokeWidth: 5,
  backgroundColor: 'rgba(0, 0, 0, .5)',
});

在背景图片上绘制(批注场景)

const drawer = FreeDraw({
  backgroundImage: 'https://example.com/photo.png',
});

压感支持

在支持压感的设备(如触控笔)上,根据按压力度动态调整线宽:

const drawer = FreeDraw({
  pressureSensitive: true,
});

撤销 / 重做

drawer.undo();
drawer.redo();

导出

const svgString = drawer.toSVGString();
const pngDataUrl = await drawer.toDataURL();

清除画布内容 / 销毁实例

drawer.clear();   // 仅清空已绘制内容,画布保留
drawer.destroy(); // 卸载并移除画布

API

方法

| 参数 | 说明 | 类型 | 返回值 | | --- | --- | --- | --- | | FreeDraw | 绘图(命令式) | (opts: FreeDrawProps) => FreeDrawController | FreeDrawController | | FreeDrawCanvas | 绘图(声明式组件,配合 ref 使用) | React.ForwardRefExoticComponent | - |

FreeDrawController(FreeDraw(opts) 的返回值)

| 方法 | 说明 | | --- | --- | | clear | 清空当前已绘制内容,画布保留 | | undo | 撤销最后一笔 | | redo | 重做最后一次被撤销的笔迹 | | toSVGString | 导出当前内容为 SVG 字符串 | | toDataURL(type?, quality?) | 导出当前内容为图片 dataURL(Promise) | | destroy | 卸载并移除画布(替代旧版的 clear) |

Options

| 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | strokeColor | 线条颜色 | string | #000 | | strokeWidth | 线条宽度 | number | 3 | | backgroundColor | 背景颜色 | string | transparent | | backgroundImage | 背景图片地址 | string | - | | multiple | 是否支持绘制多笔(默认只保留最新一笔) | boolean | false | | pressureSensitive | 是否根据压感调整线宽 | boolean | false | | disabled | 是否禁用绘制 | boolean | false | | style | 自定义样式,与内置样式合并 | CSSProperties | - | | zIndex | 指定层级 | number | 9999 | | teleport | 指定挂载的节点(仅命令式 API) | HTMLElement|(() => HTMLElement) | body |

Events

| 事件名 | 说明 | 回调参数 | | ---------------- | -------------------------- | -------------- | | onDrawStart | 点击/落笔开始绘画触发 | event: PointerEvent | | onDrawMove | 滑动绘制时触发 | event: PointerEvent | | onDrawEnd | 结束绘制事件 | (event: PointerEvent, clientRect: DOMRect) |

类型定义

组件导出以下类型定义:

import type {
  FreeDrawProps,
  FreeDrawInstance,
  FreeDrawRef,
  FreeDrawController,
} from 'free-draw-svg';