0.11.21 • Published 8 months ago

@yorab/react-paint v0.11.21

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

React Paint

An open-source canvas-based library for React used for image annotation or as a digital whiteboard

Features

Quick start

npm i @yorab/react-paint

Note : react-paint needs react and react-dom packages to work properly

Usage

Basic usage

import { Canvas, Editor, useReactPaint } from '@yorab/react-paint'

const BasicUsage = () => {
  const { editorProps, canvasProps } = useReactPaint()

  return (
    <Editor editorProps={editorProps}>
      <Canvas canvasProps={canvasProps} />
    </Editor>
  )
}

Advanced usage

import { useEffect, useState } from 'react'
import { Canvas, Editor, useReactPaint, type DrawableShape, type StateData } from '@yorab/react-paint'

const SHAPES_INIT = [
  {
    type: 'rect',
    x: 300,
    y: 300,
    width: 100,
    height: 200,
    rotation: 0,
    style: {
      fillColor: '#6a4e01',
      strokeColor: 'transparent',
      opacity: 100,
      lineWidth: 1,
      lineDash: 0,
      lineArrow: 0,
      pointsCount: 3,
      fontFamily: 'serif'
    }
  }
]

const AdvancedUsage = () => {
  const [shapes, setShapes] = useState<DrawableShape[] | undefined>(SHAPES_INIT) // keep shapes state for internal use

  const { editorProps, canvasProps, registerEvent, unregisterEvent } = useReactPaint({
    width: 1920,
    height: 1080,
    shapes,
    options: {
      canGrow: true,
      canShrink: true,
      brushAlgo: 'quadratic',
      clearCallback: 'defaultShapes'
    }
  })

  const editorOptions = {
    fontColor: '#2c1e69',
    fontHoverColor: '#0d0931'
  }

  const canvasOptions = {
    canvasBackgroundColor: '#ffffff'
  }

  useEffect(() => {
    const onDataChanged = (data: StateData, source: 'user' | 'remote') => {
      setShapes(data.shapes)
    }
    registerEvent('dataChanged', onDataChanged)
    
    return () => {
      unregisterEvent('dataChanged', onDataChanged)
    }
  }, [registerEvent, unregisterEvent])

  return (
    <Editor editorProps={editorProps} options={editorOptions}>
      <Canvas canvasProps={canvasProps} options={canvasOptions} />
    </Editor>
  )
}

Other usages

you will find other usages in stories

API

useReactPaint

Parameters
ParameterTypeDescriptionDefault value
widthnumberOptional. Canvas width in px1000
heightnumberOptional. Canvas height in px600
shapesDrawableShape[]Optional. Array of shapes used to init the canvasundefined
modeeditor\|viewerOptional. editor lets you interact with the canvas and editor. viewer hides the editor and makes the canvas read-onlyeditor
disabledbooleanOptional. Global parameter used to prevent interaction with every element in the editor or canvasfalse
optionsOptionalOptionsOptional. Set of options to customize available featuresSee OptionalOptions
Returns
ParameterTypeDescription
canvasPropsobjectSet of properties to forward to Canvas component
editorPropsobjectSet of properties to forward to Editor component
registerEvent(event: "dataChanged", listener: (data: StateData, source: 'user' \| 'remote') => void) => voidregisterEvent is used to register a listener for special events triggered by react-paint. The only currently available event is dataChanged. source indicates the origin of the state update
unregisterEvent(event: "dataChanged", listener?: ((data: StateData, source: 'user' \| 'remote') => void) \| undefined) => voidunregisterEvent is used to unregister a listener previously registered. Omitting listener will result in unregistering every listeners for the the given event
resetCanvas(shapes: DrawableShape[], clearHistory?: boolean) => Promise<void>reset canvas with the given shapes. use [] or undefined to clear the canvas. Set false to clearHistory to prevent history stack to be cleared
getCurrentImage() => string \| undefinedReturns a data URL containing the content of the current canvas as a PNG image, or undefined if an error occured
getCurrentData() => StateDataReturns the current state of the canvas

OptionalOptions

ParameterTypeDescriptionDefault value
layersManipulationbooleanOptional. Show panel to manipulate layerstrue
gridnumberOptional. Size in px for grid cells. Set to 0 to hide grid0
canGrowbooleanOptional. Allow canvas to upscale and grow to fit containerfalse
canShrinkbooleanOptional. Allow canvas to downscale and shrink to fit containertrue
withExportbooleanOptional. Show button to manually export to PNGtrue
withLoadAndSavebooleanOptional.Show button to manually save or load datatrue
withUploadPicturebooleanOptional. Show button to add picture shape stored in base64true
withUrlPicturebooleanOptional.Show button to add picture shape with only the url stored. Need connectivity to be displayedfalse
withFrameSelectionbooleanOptional.Enable frame selection. Currently, does not support multi shapes selectionfalse
withSkeletonbooleanOptional. Display skeleton when hovering shapefalse
clearCallback'empty' \| 'defaultShapes' \| (() => DrawableShape[])Optional. Set clear button behavior. empty clear all shapes, defaultShapes uses shapes given in props. It is also possible to set a function returning an array of shapesempty
brushAlgo'simple' \| 'quadratic'Optional. Choose which algorithm to display brush shape. simple displays path as is, quadratic uses quadratic bézier curvessimple
isBrushShapeDoneOnMouseUpbooleanOptional. Choose whether drawing brush shape after releasing mouse should create a new shape or nottrue
canvasSelectionPaddingbooleanOptional. Padding between shape and selection frame0
availableToolsCustomToolInput[]Optional. List of available tools. See CustomTool for more details

Canvas

Parameters
ParameterTypeDescriptionDefault value
canvasPropsobjectRequired. Set of properties coming from useReactPaint
classNamestringOptional. Classname for canvas parent nodeundefined
styleCSSPropertiesOptional. css properties to inject to canvas parent nodeundefined
options{canvasBackgroundColor?: string; canvasSelectionColor?: string; canvasSelectionWidth?: number}Optional. Set of properties to use to customize canvas style{canvasBackgroundColor: 'white'; canvasSelectionColor: 'blue'; canvasSelectionWidth: 2}

Editor

Parameters
ParameterTypeDescriptionDefault value
editorPropsobjectRequired. Set of properties coming from useReactPaint
childrenReactNodeRequired. Need Canvas component to work properly
classNamestringOptional. Classname for canvas parent nodeundefined
styleCSSPropertiesOptional. css properties to inject to canvas parent nodeundefined
options{ toolbarBackgroundColor?: string; dividerColor?: string; fontRadius?: number; fontDisabledColor?: string; fontDisabledBackgroundColor?: string; fontColor?: string; fontBackgroundColor?: string; fontSelectedColor?: string; fontSelectedBackgroundColor?: string; fontHoverColor?: string; fontHoverBackgroundColor?: string; }Optional. Set of properties to use to customize editor style{ toolbarBackgroundColor: 'white', dividerColor: '#36418129', fontRadius: 8, fontDisabledColor: '#3641812b', fontDisabledBackgroundColor: 'transparent', fontColor: '#364181', fontBackgroundColor: 'transparent', fontSelectedColor: 'white', fontSelectedBackgroundColor: '#364181', fontHoverColor: '#364181', fontHoverBackgroundColor: '#afd8d8'}

Types

DrawableShape

StateData

CustomTool

 type CustomTool = {
  id: string
  icon: string
  label: string
} & (
  | {
      type: 'brush'
      settings: ToolsSettingsType<'brush'>
    }
  | {
      type: 'circle'
      settings: ToolsSettingsType<'circle'>
    }
  | {
      type: 'ellipse'
      settings: ToolsSettingsType<'ellipse'>
    }
  | {
      type: 'rect'
      settings: ToolsSettingsType<'rect'>
    }
  | {
      type: 'square'
      settings: ToolsSettingsType<'square'>
    }
  | {
      type: 'line'
      settings: ToolsSettingsType<'line'>
    }
  | {
      type: 'polygon'
      settings: ToolsSettingsType<'polygon'>
    }
  | {
      type: 'curve'
      settings: ToolsSettingsType<'curve'>
    }
  | {
      type: 'text'
      settings: ToolsSettingsType<'text'>
    }
  | {
      type: 'picture'
      settings: ToolsSettingsType<'picture'>
    }
)

type SettingsOpacity = {
  opacity: {
    min: number
    max: number
    step: number
    default: number
    hidden?: boolean
  }
}

type SettingsStrokeColor = {
  strokeColor: {
    values: string[]
    default: string
    hidden?: boolean
  }
}

type SettingsFillColor = {
  fillColor: {
    values: string[]
    default: string
    hidden?: boolean
  }
}

type SettingsLineWidth = {
  lineWidth: {
    min: number
    max: number
    step: number
    default: number
    hidden?: boolean
  }
}

type SettingsLineDash = {
  lineDash: {
    values: number[]
    default: number
    hidden?: boolean
  }
}

type SettingsLineArrow = {
  lineArrow: {
    values: number[]
    default: number
    hidden?: boolean
  }
}

type SettingsFont = {
  fontFamily: {
    values: string[]
    default: string
    hidden?: boolean
  }
  fontBold: {
    values: boolean[]
    default: boolean
    hidden?: boolean
  }
  fontItalic: {
    values: boolean[]
    default: boolean
    hidden?: boolean
  }
}

type SettingsPointsCount = {
  pointsCount: {
    min: number
    max: number
    step: number
    default: number
    hidden?: boolean
  }
}

type ToolsRectSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash

type ToolsSquareSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash

type ToolsCircleSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash

type ToolsEllipseSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash

type ToolsTextSettings = SettingsOpacity & SettingsStrokeColor & SettingsFont

type ToolsLineSettings = SettingsOpacity & SettingsStrokeColor & SettingsLineWidth & SettingsLineDash & SettingsLineArrow

type ToolsBrushSettings = SettingsOpacity & SettingsStrokeColor & SettingsLineWidth & SettingsLineDash

type ToolsPolygonSettings = SettingsOpacity &
  SettingsStrokeColor &
  SettingsFillColor &
  SettingsLineWidth &
  SettingsLineDash &
  SettingsPointsCount

type ToolsCurveSettings = SettingsOpacity &
  SettingsStrokeColor &
  SettingsFillColor &
  SettingsLineWidth &
  SettingsLineDash &
  SettingsPointsCount

type ToolsTriangleSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash

type ToolsPictureSettings = SettingsOpacity

type ToolsSettingsType<T extends ShapeType> = T extends 'rect'
  ? ToolsRectSettings
  : T extends 'square'
    ? ToolsSquareSettings
    : T extends 'circle'
      ? ToolsCircleSettings
      : T extends 'ellipse'
        ? ToolsEllipseSettings
        : T extends 'text'
          ? ToolsTextSettings
          : T extends 'line'
            ? ToolsLineSettings
            : T extends 'brush'
              ? ToolsBrushSettings
              : T extends 'polygon'
                ? ToolsPolygonSettings
                : T extends 'curve'
                  ? ToolsCurveSettings
                  : T extends 'triangle'
                    ? ToolsTriangleSettings
                    : T extends 'picture'
                      ? ToolsPictureSettings
                      : never

Changelog

https://github.com/YoRab/react-paint-editor/blob/main/Changelog.md

License

MIT

Contributing

0.11.21

8 months ago

0.11.7

11 months ago

0.11.6

11 months ago

0.11.5

11 months ago

0.11.2

12 months ago

0.11.3

12 months ago

0.11.4

12 months ago

0.11.1

1 year ago

0.11.0

1 year ago

0.10.1

1 year ago

0.10.0

1 year ago

0.9.3

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.7.6

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.7.5

2 years ago

0.7.4

2 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.6.6

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago