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

fabric-annotator

v1.0.2

Published

An annotation tool based on fabricjs.

Readme

fabric-annotator

An annotation tool based on fabric.js.

Installation

npm install fabric-annotator

Usage

初始化标注工具,需要提供一个 DOM 元素作为舞台。

const annotator = new Annotator({
  document.querySelector('.container'), // 容器元素
  {
    imageUrl: 'https://picsum.photos/1920/1080', // 图片地址
    showCrossLine: false, // 是否展示等分线
    vGridLineCount: 2, // 垂直等分线数量
    hGridLineCount: 4, // 水平等分线数量
    bgColor: '#B1B1B1', // 背景色
    lineWidth: 2, // 线条宽度
    annoInsideImage: true, // 在图像范围内标注
    imageRotation: 0, // 图片默认旋转角度
    events: {}, // 事件回调,见下方定义
  }
});
// events
{
  // 图形添加
  onObjectAdded?: (obj: fabric.Object, existing?: boolean) => void
  // 图形删除
  onObjectRemoved?: (obj: fabric.Object) => void
  // 图形被选中
  onObjectSelected?: (obj?: fabric.Object) => void
  // 图形被鼠标经过
  onObjectHover?: (obj?: fabric.Object) => void
  // 图形更新
  onObjectUpdated?: (obj?: fabric.Object) => void
  // 图形被双击
  onObjectDblClicked?: (obj?: fabric.Object) => void
  // 背景图添加
  onImageAdded?: (obj?: fabric.Image) => void
  // 创建选区
  onSelectionCreated?: (objs?: Array<fabric.Object>) => void
  // 清除选区
  onSelectionCleared?: (obj?: fabric.Object) => void
  // 撤销
  onWithdraw?: (objs?: Array<fabric.Object>) => void
  // 背景图旋转
  onImageRotated?: (deg: number) => void
}

API

setConfig(newConfig)

更新配置

getObjects()

获取全部标注图形,返回的是修改过的 fabricjs 对象

getObjectsByType(type)

获取指定类型的标注图形

dispose()

销毁实例

setAction(actionType, params)

设置当前动作

// 切换为绘制矩形
annotator.setAction('DRAW_RECT')

triggerEvent(eventType, params)

触发事件

// 删除指定对象
annotator.triggerEvent('DELETE', ['objId1', 'objId2'])

zoomIn(delta) / zoomOut(delta) / zoomReset()

缩放/重置缩放比例

rotate()

旋转图片

getThumbnailByObj(obj)

获取指定对象的缩略图

drawShape(shape)

命令式绘制图形,通常用于从数据还原舞台

annotator.drawShape('rect', {
  id: 'xxxxx',
  left: 0,
  top: 0,
  width: 100,
  height: 200
  data: {
    color: '#fff',
    label: 'Rect1',
  },
})

async toData()

获取导出的数据,位置信息以舞台的左上角为原点

导出的数据结构

// 以 rect 类型的数据为例,对常用字段做了解释
{
    "type": "rect", // 图形类型
    "version": "5.2.4",
    "originX": "left",  // 坐标 origin
    "originY": "top",
    "left": 1919.0595491802633, // 相对舞台左侧的距离
    "top": 2233.9368065006397,  // 相对舞台顶部的距离
    "width": 521.7659807486107, // 宽度
    "height": 750.0809753188391,  // 高度
    "fill": "transparent",
    "stroke": "#f00", // 描边颜色
    "strokeWidth": 6, // 描边宽度
    "strokeDashArray": null,
    "strokeLineCap": "butt",
    "strokeDashOffset": 0,
    "strokeLineJoin": "miter",
    "strokeUniform": true,
    "strokeMiterLimit": 4,
    "scaleX": 1,  // 缩放比例
    "scaleY": 1,
    "angle": 0, // 角度
    "opacity": 1,
    "visible": true
}

// 如果是 polygon 多边形等不规则图形,则要用 points 来确定每个顶点的位置
{
  "type": "polygon",
  "version": "5.2.4",
  "originX": "left",
  "originY": "top",
  "width": 1891.4016802137157,
  "height": 962.0603813872067,
  "points": [
      {
          "x": 3011.5070713726673,
          "y": 3978.6903795248973
      },
      {
          "x": 2277.773660944933,
          "y": 4940.750760912104
      },
      {
          "x": 4169.175341158649,
          "y": 4859.220220116579
      },
      {
          "x": 4071.3442197682834,
          "y": 4076.527028479529
      }
  ],
  // ......
}