0.1.3 • Published 8 months ago

react-konva-editor-copy v0.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

react-konva-editor

A fundamental image editor component based on react and konva.You can use it with react-konva-editor-kit which supplies some tools about transforming and styling of text and image on canvas.

中文

Installation

$ npm install react-konva-editor

or

$ yarn add react-konva-editor

Usage

<KonvaCanvas
  backgroundStyle={{
    backgroundColor: '#F1F3F7',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
  }}
  onChangeSelected={store.setSelected}
  width={store.width}
  height={store.height}
  backgroundColor={store.backgroundColor}
  addItem={addedItem}
  selectedItemChange={changedItem}
  maxStep={10}
  setWithdraw={setWithdraw}
  setRedo={setRedo}
  bindRef={store.setCurrentRef}
  stepInfo={stepInfo}
  onChangeStep={onChangeStep}
/>

Props

propNametyperequireddescription
backgroundStyleObjectcanvas component background style
widthnumbercanvas width
heightnumbercanvas height
backgroundColorstring-canvas background color
addItemItemProp-when you need to add something on canvas,you should change addItem
onChangeSelected(ItemProp)=>voidreturn selected item data when you click a layer on canvas
selectedItemChangeObject-when you need to change the selected item,you should put a object with properties in selectedItemChange
maxStepnumber-max length of withdraw/redo queue (default is 10)
stepInfoArray\A series of ItemProp which had been shown on your canvas
bindRef(ReactRef)=>voidA function bind React Ref
setWithdraw()=>void-A callback when you withdraw operation
setRedo()=>void-A callback when you redo operation
onChangeStep(Array\ )=>voidA callback when you change anything on canvas,it will return ItemProps which means infomations on canvas

StepInfo

example

[
  {
    "type": "shape",
    "value": "star",
    "fill": "red",
    "id": 1001,
    "scaleX": 1,
    "scaleY": 1,
    "rotation": 0,
    "skewX": 0,
    "skewY": 0,
    "x": 195,
    "y": 345
  },
  {
    "type": "text",
    "value": "可定义内外径、角数量",
    "fontFamily": "默认",
    "id": 1002,
    "scaleX": 1.1925926495341033,
    "scaleY": 1.192592649534104,
    "rotation": 0,
    "skewX": 0,
    "skewY": 0,
    "x": 63,
    "y": 168,
    "fontSize": 25
  },
  {
    "type": "image",
    "value": "https://image.yonghuivip.com/jimu/1/1638943345035942610",
    "elementName": "上传人",
    "id": 1003,
    "x": 19,
    "y": 436,
    "width": 400,
    "height": 400,
    "scaleX": 1,
    "scaleY": 1,
    "rotation": 0,
    "skewX": 0,
    "skewY": 0
  }
]

demo

ItemProp

API

API nametypedescription
exportToImage(fileName:string,options?:ImageExportProps)=>voidexport image can be customized
exportToBASE64()=>voidexport BASE64 of the canvas
withdraw()=>voidwithdraw action
redo()=>voidredo action
canvasScale(ratio:number)=>voidzoom ratio of the canvas (ratio is (0,2.75])
deleteItem()=>voiddelete selected item on canvas
moveLayerLevel(i: number)=>voidWhen i is less than 0, the selected layer will move to the next layer
moveLayer(direction: 'right' \| 'left' \| 'top'\| 'bottom', delta: number)=>voidmove delta unit on canvas on specific direction
setSelectedIndex(id:number)=>voidSelect the selected layers in order
clearSelected()=>voidUnselected layer
toggleMultiSelected(state:boolean)=>voidswitch multi-selected mode
toogleLock(index:number)=>voidtoggle the lock state of specific layer
madeGroup(layers:Array\<ItemProp>)=>voidGroup the selected layers
divideGroup(groupId:string)=>voidUngroup by a specific groupId
propNametyperequireddescription
scalenumber0.1-3-Scale ratio of output image (default is 1)
qualitynumber 0.1-1-quality of export image(default is 1)
fileType'image/png' | 'image/jpeg'-filetype of export image(default is 'image/png' )

Tips

  1. How to switch on multi-select Mode?
const handler = useCallback((e: KeyboardEvent) => {
  if (e.keyCode === 16) {
    // 开启多选模式
    KonvaCanvas.toggleMultiSelected(true);
    store.setMultiSelected(true);
  }
}, []);

useEffect(() => {
  window.addEventListener('keydown', handler);
  return () => {
    window.removeEventListener('keydown', handler);
  };
}, []);

Timeline

23-2-20 支持多选图层,拆组和解组-可配合 toolkit 使用

reference

  1. build reference
  2. react-konva

Todos

序号内容完成情况
1js 环境调通
2ts type 规范化delay
3ts 环境调通
4依赖库配置delay
5使用文档ing
6线上示例-