图片批注插件
欢迎大家使用,有问题可以在这csdn下方留言,我会不定期来这看有什么bug和优化点,尽量快速更新优化
install
pnpm i picture-note
npm i picture-note
yarn add picture-note
pnpm picture-note-react
使用
html
<link rel="stylesheet" href="../lib/index.css">
<script src="../lib/index.js"></script>
<svg id="svg" width="800" height="600">
<image xlink:href="./2.jpg" width="100%" height="100%" />
</svg>
init
import pictureNote from 'picture-note'
pictureNote.init({
svg: document.getElementById('svg'),
noteData?: NoteData[] | (() => NoteData[]),
save?: (data: NoteData[], type: DrawingMode) => void,
plugins?: Plugin[],
})
切换模式
import pictureNote from 'picture-note'
pictureNote.setDrawingMode('pen' | 'text' | 'rect' | '')
设置颜色
import pictureNote from 'picture-note'
pictureNote.setColor('#f00'|'red')
设置尺寸
import pictureNote from 'picture-note'
pictureNote.setSize(20|'20')
重新加载
这种场景比如业务需求要上一步和下一步,这样就可以通过重载方法重新设置新数据了
import pictureNote from 'picture-note'
pictureNote.reload(NoteData[])
删除
import pictureNote from 'picture-note'
const data:DeleteData = pictureNote.removeChild()
获取图片
import pictureNote from 'picture-note'
const data:DeleteData = await pictureNote.getImg()
types
type CommonNoteData = {
id: string,
x: number,
y: number,
width?: number,
height?: number,
color?: string,
size?: number | string,
}
type CirclePosition = 't' | 'r' | 'b' | 'l' | 'tl' | 'tr' | 'bl' | 'br'
type Circles = {
pid: string,
type: CirclePosition,
cx: number,
cy: number,
r: number,
fill: string
}
type RectData = CommonNoteData & {
type: 'rect',
circles: Circles[]
}
type PathType = CommonNoteData & {
type: 'pen',
d: string,
}
type TextType = CommonNoteData & {
type: 'text',
text: string,
}
type NoteData = RectData | PathType | TextType
type DrawingMode = 'pen' | 'text' | 'rect' | string
type Props = {
svg: SVGElement,
noteData?: NoteData[] | (() => NoteData[]), // 回填数据
save?: (data: NoteData[], type: DrawingMode) => void,
}
type DeleteData = {
data: NoteData;
type: DrawingMode;
}
type SetDrawingMode = (data: DrawingMode) => void
type RemoveChild = () => DeleteData
type SetColor = (color: string) => void
type SetSize = (size: number | string) => void
type Reload = (data: NoteData[] | (() => NoteData[])) => void
type init = (props: Props) => void
type Destroy = () => void
type Plugin = {
name: string
init: (props: Props, isReload?: boolean) => void
mousedown: (x:number, y:number, e: MouseEvent, drawingMode: DrawingMode) => void
mousemove: (x:number, y:number, e: MouseEvent) => void
mouseup: (e: MouseEvent) => void
setColor: (color: string) => void
setSize: (size: number | string) => void
remove: () => ({ data: NoteData, type: DrawingMode})
destroy?: () => void
globalMousedown?: (e:MouseEvent) => void
}
原生示例
可以看一下demo中的使用示例