scratch-paint/src/containers/tools/eraser-tool.jsx

89 lines
2.8 KiB
React
Raw Normal View History

2017-07-25 11:53:54 -04:00
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
import ToolTypes from '../../tools/tool-types.js';
import BlobTool from '../../tools/blob.js';
import EraserToolReducer from '../../reducers/eraser-tool';
import paper from 'paper';
class EraserTool extends React.Component {
static get TOOL_TYPE () {
return ToolTypes.ERASER;
}
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
'deactivateTool',
'onScroll'
]);
this.blob = new BlobTool();
}
componentDidMount () {
if (this.props.tool === EraserTool.TOOL_TYPE) {
this.activateTool();
}
}
componentWillReceiveProps (nextProps) {
if (nextProps.tool === EraserTool.TOOL_TYPE && this.props.tool !== EraserTool.TOOL_TYPE) {
this.activateTool();
} else if (nextProps.tool !== EraserTool.TOOL_TYPE && this.props.tool === EraserTool.TOOL_TYPE) {
this.deactivateTool();
} else if (nextProps.tool === EraserTool.TOOL_TYPE && this.props.tool === EraserTool.TOOL_TYPE) {
this.blob.setOptions(nextProps.eraserToolState);
}
}
shouldComponentUpdate () {
return false; // Logic only component
}
activateTool () {
2017-07-27 11:45:41 -04:00
this.props.canvas.addEventListener('mousewheel', this.onScroll);
2017-07-25 11:53:54 -04:00
this.tool = new paper.Tool();
this.blob.activateTool(true /* isEraser */, this.tool, this.props.eraserToolState);
this.tool.activate();
}
deactivateTool () {
2017-07-27 11:45:41 -04:00
this.props.canvas.removeEventListener('mousewheel', this.onScroll);
2017-07-25 11:53:54 -04:00
this.blob.deactivateTool();
}
onScroll (event) {
2017-07-27 16:41:41 -04:00
event.preventDefault();
2017-07-25 11:53:54 -04:00
if (event.deltaY < 0) {
this.props.changeBrushSize(this.props.eraserToolState.brushSize + 1);
} else if (event.deltaY > 0 && this.props.eraserToolState.brushSize > 1) {
this.props.changeBrushSize(this.props.eraserToolState.brushSize - 1);
}
}
render () {
return (
2017-07-27 16:41:41 -04:00
<div>Eraser Tool</div>
2017-07-25 11:53:54 -04:00
);
}
}
EraserTool.propTypes = {
2017-07-27 16:41:41 -04:00
canvas: PropTypes.instanceOf(Element).isRequired,
2017-07-25 11:53:54 -04:00
changeBrushSize: PropTypes.func.isRequired,
eraserToolState: PropTypes.shape({
brushSize: PropTypes.number.isRequired
}),
2017-07-27 16:41:41 -04:00
tool: PropTypes.oneOf(Object.keys(ToolTypes)).isRequired
2017-07-25 11:53:54 -04:00
};
const mapStateToProps = state => ({
2017-07-25 15:00:35 -04:00
eraserToolState: state.eraserTool,
2017-07-25 11:53:54 -04:00
tool: state.tool
});
const mapDispatchToProps = dispatch => ({
changeBrushSize: brushSize => {
dispatch(EraserToolReducer.changeBrushSize(brushSize));
}
});
2017-07-27 11:45:41 -04:00
export default connect(
2017-07-25 11:53:54 -04:00
mapStateToProps,
mapDispatchToProps
)(EraserTool);