2017-08-20 14:32:01 +08:00
|
|
|
import React, { Component } from 'react';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2017-08-20 14:32:01 +08:00
|
|
|
import AnnotationHelpers from '../helpers';
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
export default class LineDrawComponent extends Component {
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
return this.props.version !== nextProps.version;
|
2017-05-10 04:34:46 +08:00
|
|
|
}
|
|
|
|
|
2016-06-07 01:12:14 +08:00
|
|
|
getCoordinates() {
|
2017-08-20 14:32:01 +08:00
|
|
|
const { slideWidth, slideHeight } = this.props;
|
|
|
|
const { points } = this.props.annotation;
|
|
|
|
|
|
|
|
const x1 = (points[0] / 100) * slideWidth;
|
|
|
|
const y1 = (points[1] / 100) * slideHeight;
|
|
|
|
const x2 = (points[2] / 100) * slideWidth;
|
|
|
|
const y2 = (points[3] / 100) * slideHeight;
|
2016-06-07 01:12:14 +08:00
|
|
|
|
|
|
|
return {
|
2017-06-03 03:25:02 +08:00
|
|
|
x1,
|
|
|
|
y1,
|
|
|
|
x2,
|
|
|
|
y2,
|
2016-06-08 02:33:31 +08:00
|
|
|
};
|
2016-06-07 01:12:14 +08:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:07:02 +08:00
|
|
|
render() {
|
2017-06-03 03:25:02 +08:00
|
|
|
const results = this.getCoordinates();
|
2017-08-20 14:32:01 +08:00
|
|
|
const { annotation, slideWidth } = this.props;
|
2017-09-06 09:36:15 +08:00
|
|
|
const { x1, y1, x2, y2 } = results;
|
2017-08-20 14:32:01 +08:00
|
|
|
|
2016-05-31 06:07:02 +08:00
|
|
|
return (
|
|
|
|
<line
|
2017-09-06 09:36:15 +08:00
|
|
|
x1={x1}
|
|
|
|
y1={y1}
|
|
|
|
x2={x2}
|
|
|
|
y2={y2}
|
|
|
|
stroke={AnnotationHelpers.getFormattedColor(annotation.color)}
|
2016-06-08 02:33:31 +08:00
|
|
|
strokeLinejoin="round"
|
2017-08-20 14:32:01 +08:00
|
|
|
strokeWidth={AnnotationHelpers.getStrokeWidth(annotation.thickness, slideWidth)}
|
|
|
|
style={{ WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)' }}
|
2016-05-31 06:07:02 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 01:12:14 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
LineDrawComponent.propTypes = {
|
|
|
|
// Defines a version of the shape, so that we know if we need to update the component or not
|
|
|
|
version: PropTypes.number.isRequired,
|
|
|
|
// Defines an annotation object, which contains all the basic info we need to draw a line
|
|
|
|
annotation: PropTypes.shape({
|
|
|
|
points: PropTypes.arrayOf(PropTypes.number).isRequired,
|
|
|
|
color: PropTypes.number.isRequired,
|
|
|
|
thickness: PropTypes.number.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
// Defines the width of the slide (svg coordinate system), which needed in calculations
|
|
|
|
slideWidth: PropTypes.number.isRequired,
|
|
|
|
// Defines the height of the slide (svg coordinate system), which needed in calculations
|
|
|
|
slideHeight: PropTypes.number.isRequired,
|
2016-06-07 01:12:14 +08:00
|
|
|
};
|