scratch-paint/src/containers/eraser-mode.jsx

89 lines
2.7 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 Modes from '../modes/modes';
import Blobbiness from '../modes/blob';
import EraserModeReducer from '../reducers/eraser-mode';
2017-07-25 11:53:54 -04:00
import paper from 'paper';
class EraserMode extends React.Component {
static get MODE () {
return Modes.ERASER;
2017-07-25 11:53:54 -04:00
}
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
'deactivateTool',
'onScroll'
]);
this.blob = new Blobbiness();
2017-07-25 11:53:54 -04:00
}
componentDidMount () {
if (this.props.isEraserModeActive) {
2017-07-25 11:53:54 -04:00
this.activateTool();
}
}
componentWillReceiveProps (nextProps) {
if (nextProps.isEraserModeActive && !this.props.isEraserModeActive) {
2017-07-25 11:53:54 -04:00
this.activateTool();
} else if (!nextProps.isEraserModeActive && this.props.isEraserModeActive) {
2017-07-25 11:53:54 -04:00
this.deactivateTool();
} else if (nextProps.isEraserModeActive && this.props.isEraserModeActive) {
this.blob.setOptions(nextProps.eraserModeState);
2017-07-25 11:53:54 -04:00
}
}
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.eraserModeState);
2017-07-25 11:53:54 -04:00
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.eraserModeState.brushSize + 1);
} else if (event.deltaY > 0 && this.props.eraserModeState.brushSize > 1) {
this.props.changeBrushSize(this.props.eraserModeState.brushSize - 1);
2017-07-25 11:53:54 -04:00
}
}
render () {
return (
<div>Eraser Mode</div>
2017-07-25 11:53:54 -04:00
);
}
}
EraserMode.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,
eraserModeState: PropTypes.shape({
2017-07-25 11:53:54 -04:00
brushSize: PropTypes.number.isRequired
}),
isEraserModeActive: PropTypes.bool.isRequired
2017-07-25 11:53:54 -04:00
};
const mapStateToProps = state => ({
eraserModeState: state.eraserMode,
isEraserModeActive: state.mode === EraserMode.MODE
2017-07-25 11:53:54 -04:00
});
const mapDispatchToProps = dispatch => ({
changeBrushSize: brushSize => {
dispatch(EraserModeReducer.changeBrushSize(brushSize));
2017-07-25 11:53:54 -04:00
}
});
2017-07-27 11:45:41 -04:00
export default connect(
2017-07-25 11:53:54 -04:00
mapStateToProps,
mapDispatchToProps
)(EraserMode);