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';
|
2020-04-07 04:34:08 +08:00
|
|
|
import { getFormattedColor, getStrokeWidth, denormalizeCoord } from '../helpers';
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
export default class TriangleDrawComponent extends Component {
|
|
|
|
shouldComponentUpdate(nextProps) {
|
2020-04-07 04:34:08 +08:00
|
|
|
const { version } = this.props;
|
|
|
|
return version !== nextProps.version;
|
2017-05-10 04:34:46 +08:00
|
|
|
}
|
|
|
|
|
2016-06-09 01:13:25 +08:00
|
|
|
getCoordinates() {
|
2020-04-07 04:34:08 +08:00
|
|
|
const { slideWidth, slideHeight, annotation } = this.props;
|
|
|
|
const { points } = annotation;
|
2017-08-20 14:32:01 +08:00
|
|
|
|
|
|
|
// points[0] and points[1] are x and y coordinates of the top left corner of the annotation
|
|
|
|
// points[2] and points[3] are x and y coordinates of the bottom right corner of the annotation
|
|
|
|
const xBottomLeft = points[0];
|
|
|
|
const yBottomLeft = points[3];
|
|
|
|
const xBottomRight = points[2];
|
|
|
|
const yBottomRight = points[3];
|
2017-06-03 03:25:02 +08:00
|
|
|
const xTop = ((xBottomRight - xBottomLeft) / 2) + xBottomLeft;
|
2017-08-20 14:32:01 +08:00
|
|
|
const yTop = points[1];
|
|
|
|
|
2020-04-07 04:34:08 +08:00
|
|
|
const path = `M${denormalizeCoord(xTop, slideWidth)
|
|
|
|
},${denormalizeCoord(yTop, slideHeight)
|
|
|
|
},${denormalizeCoord(xBottomLeft, slideWidth)
|
|
|
|
},${denormalizeCoord(yBottomLeft, slideHeight)
|
|
|
|
},${denormalizeCoord(xBottomRight, slideWidth)
|
|
|
|
},${denormalizeCoord(yBottomRight, slideHeight)
|
|
|
|
}Z`;
|
2016-06-09 01:13:25 +08:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-05-31 06:07:02 +08:00
|
|
|
render() {
|
2017-06-03 03:25:02 +08:00
|
|
|
const path = this.getCoordinates();
|
2017-08-20 14:32:01 +08:00
|
|
|
const { annotation, slideWidth } = this.props;
|
2016-05-31 06:07:02 +08:00
|
|
|
return (
|
|
|
|
<path
|
2017-08-20 14:32:01 +08:00
|
|
|
style={{ WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)' }}
|
2016-06-09 01:13:25 +08:00
|
|
|
fill="none"
|
2020-04-07 04:34:08 +08:00
|
|
|
stroke={getFormattedColor(annotation.color)}
|
2016-06-09 01:13:25 +08:00
|
|
|
d={path}
|
2020-04-07 04:34:08 +08:00
|
|
|
strokeWidth={getStrokeWidth(annotation.thickness, slideWidth)}
|
2017-08-26 06:45:06 +08:00
|
|
|
strokeLinejoin="miter"
|
2017-06-03 03:25:02 +08:00
|
|
|
/>
|
2016-05-31 06:07:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 01:13:25 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
TriangleDrawComponent.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 triangle
|
|
|
|
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-09 01:13:25 +08:00
|
|
|
};
|