Sizing Konva image to full width of parent element

2k views Asked by At

I have a react component that overlays a circle on to an image when the image is clicked. I am unable to find a good way to size the image, as it seems to only takes pxl as an value. I would like to make it the full width of the parent <Col> element. I am using react-konva and react-bootstrap.

import { PageHeader, Grid, Row, Col, 
ListGroup, ListGroupItem, Table, Panel, 
ButtonToolbar, Button, Modal, FormGroup, FormControl, HelpBlock, ControlLabel} 
from 'react-bootstrap';

import {BootstrapTable, TableHeaderColumn} from 'react-bootstrap-table';
import {Stage, Layer, Rect, Line, Image, Circle} from 'react-konva';

        <Grid>
            <Row>
                <PageHeader>System Boards<small> {this.state.name}</small></PageHeader>
                <Col md={6}>
                    <Stage height="100" width="100">
                        <Layer>
                            <Image 
                              image={this.state.image} 
                              height="100" 
                              width="100" 
                              onClick={ (event) => {
                                this.handleClickImage(event);
                              }}
                              onTap={ (event) => {
                                this.handleClickImage(event);
                              }}
                            />
                        </Layer>
                        {this.state.moves}
                   </Stage>
                </Col>
                <Col md={6}>
2

There are 2 answers

2
bennygenel On

You can use react-bootsrap Media Content for image sizing. You shoud have a non displaying version of the image out of canvas so when you click on thumbnail you can change the visibility.

0
lavrton On

You can read Col size in componentDidMount and then update state of you component with required size for the image. Something like this:

componenentDidMount() {
    // you can add class or id to Col to find it in the DOM
    const colEl = document.querySelector('.col-selector');
    this.setState({
        imageWidth: colEl.offsetWidth,
        imageHeight: colEl.offsetHeight,
    });
}

Probably you will need to do the same on window resize.