embiem / react-canvas-draw

React Component for drawing in canvas
https://embiem.github.io/react-canvas-draw/
MIT License
904 stars 315 forks source link

We cannot export final canvas to png or base64 #36

Closed axelmarciano closed 4 years ago

jessicaanhdao commented 5 years ago

So what I did was have images : [] in state then called a function that gets binary from canvas tag. If you check from Inspect, there are 4 canvases, one of them is our drawing, one is the white canvas, one is the brush and one is something else. The second canvas is where our drawing is.

let canvas = document.getElementsByTagName('canvas');
    let localImages = []
    for(let c of canvas) {
      localImages.push(c.toDataURL())
    }
    this.setState({
      images : localImages
    })

then in render()

<img src={this.state.images[1]}/>
embiem commented 4 years ago

Thanks @jessicaanhdao. A while ago I also answered this here: https://github.com/embiem/react-canvas-draw/issues/11#issuecomment-453848162

I'll create a new issue to track work on adding a better way and documentation for this, as it's a common use case.