nhn / toast-ui.react-image-editor

TOAST UI ImageEditor wrapper for React.js
MIT License
117 stars 51 forks source link

How to get edited image content/ Data url? #23

Closed fxnoob closed 4 years ago

russian-proger commented 4 years ago

answer

russian-proger commented 4 years ago

To make data url, you should to get an instance of the reference to the image editor, then use the "toDataURL" method

russian-proger commented 4 years ago

Also you can find canvas of this editor via classes. Image editor has two canvases: .lower-canvas and .upper-canvas. The second of these stores last changes

fxnoob commented 4 years ago

Hey @russian-proger, thanks. btw i've edited the component like this


import React from 'react';
import TuiImageEditor from 'tui-image-editor';

export default class ImageEditor extends React.Component {
  rootEl = React.createRef();

  imageEditorInst = null;

  componentDidMount() {
    this.imageEditorInst = new TuiImageEditor(this.rootEl.current, {
      ...this.props,
    });
    this.props.getInstance(this.imageEditorInst);
    this.bindEventHandlers(this.props);
  }

  componentWillUnmount() {
    this.unbindEventHandlers();
    this.imageEditorInst.destroy();
    this.imageEditorInst = null;
    this.props.getInstance(this.imageEditorInst);
  }

  shouldComponentUpdate(nextProps) {
    this.bindEventHandlers(this.props, nextProps);

    return false;
  }

  getInstance() {
    return this.imageEditorInst;
  }

  getRootElement() {
    return this.rootEl.current;
  }

  bindEventHandlers(props, prevProps) {
    Object.keys(props)
      .filter(this.isEventHandlerKeys)
      .forEach((key) => {
        const eventName = key[2].toLowerCase() + key.slice(3);
        // For <ImageEditor onFocus={condition ? onFocus1 : onFocus2} />
        if (prevProps && prevProps[key] !== props[key]) {
          this.imageEditorInst.off(eventName);
        }
        this.imageEditorInst.on(eventName, props[key]);
      });
  }

  unbindEventHandlers() {
    Object.keys(this.props)
      .filter(this.isEventHandlerKeys)
      .forEach((key) => {
        const eventName = key[2].toLowerCase() + key.slice(3);
        this.imageEditorInst.off(eventName);
      });
  }

  isEventHandlerKeys(key) {
    return /on[A-Z][a-zA-Z]+/.test(key);
  }

  render() {
    return <div ref={this.rootEl} />;
  }
}

` ```