简体   繁体   中英

Why can't I access Konva Canvas properties in my React app?

I am using the FabricJS library with ReactJS. Here I want to create and use Konva.Canvas constructor methods. But, I have a problem while accessing the Konva Canvas properties.

if I use canvas.add method I get ERROR canvas.add is not a function

const Konva = window.Konva;
class Canvas extends Component {
    state ={
        canvas: null,
        width: null,
        height: null,
        layer: null,
    }
    componentDidMount() {
        const canvas = new Konva.Canvas({ 
            container: this.c,
            height:this.props.height,
            width:this.props.width
        });
        console.log(canvas);
        const layer = new Konva.Layer();
        this.setState({canvas,layer});
        // canvas.add(layer);
        layer.draw();
    }

Replaced

 const canvas = new Konva.Canvas({ 
        container: this.c,
        height:this.props.height,
        width:this.props.width
    });

with

 const canvas = new Konva.Stage({ 
        container: this.c,
        height:this.props.height,
        width:this.props.width
    });

Stage supports add method. More info on https://konvajs.org/api/Konva.Stage.html#main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM