| import bindAll from 'lodash.bindall';
|
| import PropTypes from 'prop-types';
|
| import React from 'react';
|
| import {connect} from 'react-redux';
|
| import paper from '@turbowarp/paper';
|
| import Formats from '../lib/format';
|
| import log from '../log/log';
|
|
|
| import {performSnapshot} from '../helper/undo';
|
| import {undoSnapshot, clearUndoState} from '../reducers/undo';
|
| import {isGroup, ungroupItems} from '../helper/group';
|
| import {clearRaster, convertBackgroundGuideLayer, getRaster, setupLayers, updateTheme} from '../helper/layer';
|
| import {clearSelectedItems} from '../reducers/selected-items';
|
| import {
|
| ART_BOARD_WIDTH, ART_BOARD_HEIGHT, CENTER, MAX_WORKSPACE_BOUNDS,
|
| clampViewBounds, resetZoom, setWorkspaceBounds, zoomToFit, resizeCrosshair
|
| } from '../helper/view';
|
| import {ensureClockwise, scaleWithStrokes} from '../helper/math';
|
| import {clearHoveredItem} from '../reducers/hover';
|
| import {clearPasteOffset} from '../reducers/clipboard';
|
| import {changeFormat} from '../reducers/format';
|
| import {updateViewBounds} from '../reducers/view-bounds';
|
| import {saveZoomLevel, setZoomLevelId} from '../reducers/zoom-levels';
|
| import {setImportingImage} from '../lib/tw-is-importing-image';
|
|
|
| import styles from './paper-canvas.css';
|
|
|
| class PaperCanvas extends React.Component {
|
| constructor (props) {
|
| super(props);
|
| bindAll(this, [
|
| 'clearQueuedImport',
|
| 'setCanvas',
|
| 'importSvg',
|
| 'initializeSvg',
|
| 'maybeZoomToFit',
|
| 'switchCostume',
|
| 'onViewResize',
|
| 'recalibrateSize'
|
| ]);
|
| }
|
| componentDidMount () {
|
| paper.setup(this.canvas);
|
| paper.view.on('resize', this.onViewResize);
|
| resetZoom();
|
| if (this.props.zoomLevelId) {
|
| this.props.setZoomLevelId(this.props.zoomLevelId);
|
| if (this.props.zoomLevels[this.props.zoomLevelId]) {
|
|
|
| this.shouldZoomToFit = this.props.zoomLevels[this.props.zoomLevelId];
|
| } else {
|
|
|
| this.shouldZoomToFit = true;
|
| }
|
| } else {
|
| this.props.updateViewBounds(paper.view.matrix);
|
| }
|
|
|
| const context = this.canvas.getContext('2d');
|
| context.webkitImageSmoothingEnabled = false;
|
| context.imageSmoothingEnabled = false;
|
|
|
|
|
| paper.settings.handleSize = 0;
|
|
|
| setupLayers(this.props.format);
|
| updateTheme(this.props.theme);
|
| this.importImage(
|
| this.props.imageFormat, this.props.image, this.props.rotationCenterX, this.props.rotationCenterY);
|
| }
|
| componentWillReceiveProps (newProps) {
|
| if (this.props.imageId !== newProps.imageId) {
|
| this.switchCostume(newProps.imageFormat, newProps.image,
|
| newProps.rotationCenterX, newProps.rotationCenterY,
|
| this.props.zoomLevelId, newProps.zoomLevelId);
|
| }
|
| if (this.props.format !== newProps.format) {
|
| this.recalibrateSize();
|
| convertBackgroundGuideLayer(newProps.format);
|
| }
|
| if (this.props.theme !== newProps.theme) {
|
| updateTheme(newProps.theme);
|
| }
|
| }
|
| componentWillUnmount () {
|
| this.clearQueuedImport();
|
|
|
| if (!this.shouldZoomToFit) {
|
| this.props.saveZoomLevel();
|
| }
|
| paper.remove();
|
| }
|
| clearQueuedImport () {
|
| if (this.queuedImport) {
|
| window.clearTimeout(this.queuedImport);
|
| this.queuedImport = null;
|
| }
|
| if (this.queuedImageToLoad) {
|
| this.queuedImageToLoad.src = '';
|
| this.queuedImageToLoad.onload = null;
|
| this.queuedImageToLoad = null;
|
| }
|
| }
|
| switchCostume (format, image, rotationCenterX, rotationCenterY, oldZoomLevelId, newZoomLevelId) {
|
| if (oldZoomLevelId && oldZoomLevelId !== newZoomLevelId) {
|
| this.props.saveZoomLevel();
|
| }
|
| if (newZoomLevelId && oldZoomLevelId !== newZoomLevelId) {
|
| if (this.props.zoomLevels[newZoomLevelId]) {
|
| this.shouldZoomToFit = this.props.zoomLevels[newZoomLevelId];
|
| } else {
|
| this.shouldZoomToFit = true;
|
| }
|
| this.props.setZoomLevelId(newZoomLevelId);
|
| }
|
| this.props.clearUndo();
|
| this.props.clearSelectedItems();
|
| this.props.clearHoveredItem();
|
| this.props.clearPasteOffset();
|
| this.importImage(format, image, rotationCenterX, rotationCenterY);
|
| }
|
| clearPaperCanvas () {
|
| for (const layer of paper.project.layers) {
|
| if (layer.data.isRasterLayer) {
|
| clearRaster();
|
| } else if (!layer.data.isBackgroundGuideLayer &&
|
| !layer.data.isDragCrosshairLayer &&
|
| !layer.data.isOutlineLayer) {
|
| layer.removeChildren();
|
| }
|
| }
|
| }
|
| importImage (format, image, rotationCenterX, rotationCenterY) {
|
|
|
| this.clearQueuedImport();
|
|
|
| if (!image) {
|
| this.clearPaperCanvas();
|
| this.props.changeFormat(Formats.VECTOR_SKIP_CONVERT);
|
| performSnapshot(this.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT);
|
| this.recalibrateSize();
|
| return;
|
| }
|
|
|
| if (format === 'jpg' || format === 'png') {
|
|
|
| this.props.changeFormat(Formats.BITMAP_SKIP_CONVERT);
|
| setImportingImage(true);
|
|
|
| const mask = new paper.Shape.Rectangle(getRaster().getBounds());
|
| mask.guide = true;
|
| mask.locked = true;
|
| mask.setPosition(CENTER);
|
| mask.clipMask = true;
|
|
|
| const imgElement = new Image();
|
| this.queuedImageToLoad = imgElement;
|
| imgElement.onload = () => {
|
| if (!this.queuedImageToLoad) return;
|
| this.queuedImageToLoad = null;
|
|
|
| this.clearPaperCanvas();
|
| setImportingImage(false);
|
|
|
| if (typeof rotationCenterX === 'undefined') {
|
| rotationCenterX = imgElement.width / 2;
|
| }
|
| if (typeof rotationCenterY === 'undefined') {
|
| rotationCenterY = imgElement.height / 2;
|
| }
|
|
|
| getRaster().drawImage(
|
| imgElement,
|
| (ART_BOARD_WIDTH / 2) - rotationCenterX,
|
| (ART_BOARD_HEIGHT / 2) - rotationCenterY);
|
|
|
| this.maybeZoomToFit(true );
|
| performSnapshot(this.props.undoSnapshot, Formats.BITMAP_SKIP_CONVERT);
|
| this.recalibrateSize();
|
| };
|
| imgElement.src = image;
|
| } else if (format === 'svg') {
|
| this.props.changeFormat(Formats.VECTOR_SKIP_CONVERT);
|
| this.importSvg(image, rotationCenterX, rotationCenterY);
|
| } else {
|
| this.clearPaperCanvas();
|
| log.error(`Didn't recognize format: ${format}. Use 'jpg', 'png' or 'svg'.`);
|
| this.props.changeFormat(Formats.VECTOR_SKIP_CONVERT);
|
| performSnapshot(this.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT);
|
| this.recalibrateSize();
|
| }
|
| }
|
| maybeZoomToFit (isBitmapMode) {
|
| if (this.shouldZoomToFit instanceof paper.Matrix) {
|
| paper.view.matrix = this.shouldZoomToFit;
|
| this.props.updateViewBounds(paper.view.matrix);
|
| resizeCrosshair();
|
| } else if (this.shouldZoomToFit === true) {
|
| zoomToFit(isBitmapMode);
|
| }
|
| this.shouldZoomToFit = false;
|
| setWorkspaceBounds();
|
| this.props.updateViewBounds(paper.view.matrix);
|
| }
|
| importSvg (svg, rotationCenterX, rotationCenterY) {
|
| setImportingImage(true);
|
|
|
| const paperCanvas = this;
|
|
|
|
|
|
|
| svg = svg.split(/<\s*svg:/).join('<');
|
| svg = svg.split(/<\/\s*svg:/).join('</');
|
|
|
| const svgAttrs = svg.match(/<svg [^>]*>/);
|
| if (svgAttrs && svgAttrs[0].indexOf('xmlns=') === -1) {
|
| svg = svg.replace(
|
| '<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ');
|
| }
|
|
|
|
|
|
|
|
|
| const parser = new DOMParser();
|
| const svgDom = parser.parseFromString(svg, 'text/xml');
|
| const viewBox = svgDom.documentElement.attributes.viewBox ?
|
| svgDom.documentElement.attributes.viewBox.value.match(/\S+/g) : null;
|
| if (viewBox) {
|
| for (let i = 0; i < viewBox.length; i++) {
|
| viewBox[i] = parseFloat(viewBox[i]);
|
| }
|
| }
|
|
|
| paper.project.importSVG(svg, {
|
| expandShapes: true,
|
| insert: false,
|
| onLoad: function (item) {
|
| if (!item) {
|
| log.error('SVG import failed:');
|
| log.info(svg);
|
| setImportingImage(false);
|
| paperCanvas.props.changeFormat(Formats.VECTOR_SKIP_CONVERT);
|
| performSnapshot(paperCanvas.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT);
|
| return;
|
| }
|
|
|
|
|
|
|
| paperCanvas.queuedImport = paperCanvas.recalibrateSize(() => {
|
| paperCanvas.props.updateViewBounds(paper.view.matrix);
|
| paperCanvas.initializeSvg(item, rotationCenterX, rotationCenterY, viewBox);
|
| });
|
| }
|
| });
|
| }
|
| initializeSvg (item, rotationCenterX, rotationCenterY, viewBox) {
|
| setImportingImage(false);
|
| this.clearPaperCanvas();
|
|
|
| if (this.queuedImport) this.queuedImport = null;
|
| const itemWidth = item.bounds.width;
|
| const itemHeight = item.bounds.height;
|
|
|
|
|
| let mask;
|
| if (item.clipped) {
|
| for (const child of item.children) {
|
| if (child.isClipMask()) {
|
| mask = child;
|
| break;
|
| }
|
| }
|
| mask.clipMask = false;
|
| } else {
|
| mask = new paper.Shape.Rectangle(item.bounds);
|
| }
|
| mask.guide = true;
|
| mask.locked = true;
|
| mask.matrix = new paper.Matrix();
|
|
|
| mask.size.height = MAX_WORKSPACE_BOUNDS.height;
|
| mask.size.width = MAX_WORKSPACE_BOUNDS.width;
|
| mask.setPosition(CENTER);
|
| paper.project.activeLayer.addChild(mask);
|
| mask.clipMask = true;
|
|
|
|
|
| if (item instanceof paper.Group && item.children.length === 1) {
|
| item = item.reduce();
|
| }
|
|
|
| ensureClockwise(item);
|
| scaleWithStrokes(item, 2, new paper.Point());
|
|
|
|
|
| if (typeof rotationCenterX !== 'undefined' && typeof rotationCenterY !== 'undefined') {
|
| let rotationPoint = new paper.Point(rotationCenterX, rotationCenterY);
|
| if (viewBox && viewBox.length >= 2 && !isNaN(viewBox[0]) && !isNaN(viewBox[1])) {
|
| rotationPoint = rotationPoint.subtract(viewBox[0], viewBox[1]);
|
| }
|
| item.translate(CENTER.subtract(rotationPoint.multiply(2)));
|
| } else {
|
|
|
| item.translate(CENTER.subtract(itemWidth, itemHeight));
|
| }
|
|
|
| paper.project.activeLayer.insertChild(0, item);
|
| if (isGroup(item)) {
|
|
|
| for (const child of item.children) {
|
| if (isGroup(child) && child.children.length === 0) {
|
| child.remove();
|
| }
|
| }
|
| ungroupItems([item]);
|
| }
|
|
|
| performSnapshot(this.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT);
|
| this.maybeZoomToFit();
|
| }
|
| onViewResize () {
|
| setWorkspaceBounds(true );
|
| clampViewBounds();
|
|
|
| this.recalibrateSize();
|
| this.props.updateViewBounds(paper.view.matrix);
|
| }
|
| recalibrateSize (callback) {
|
|
|
|
|
| return window.setTimeout(() => {
|
|
|
|
|
|
|
| if (!paper.view) return;
|
|
|
|
|
| const elemSize = paper.DomElement.getSize(paper.view.element);
|
| elemSize.width = Math.round(elemSize.width);
|
| elemSize.height = Math.round(elemSize.height);
|
| paper.view.setViewSize(elemSize);
|
|
|
| if (callback) callback();
|
| }, 0);
|
| }
|
| setCanvas (canvas) {
|
| this.canvas = canvas;
|
| if (this.props.canvasRef) {
|
| this.props.canvasRef(canvas);
|
| }
|
| }
|
| render () {
|
| return (
|
| <canvas
|
| className={styles.paperCanvas}
|
| ref={this.setCanvas}
|
| style={{cursor: this.props.cursor}}
|
| resize="true"
|
| />
|
| );
|
| }
|
| }
|
|
|
| PaperCanvas.propTypes = {
|
| canvasRef: PropTypes.func,
|
| changeFormat: PropTypes.func.isRequired,
|
| clearHoveredItem: PropTypes.func.isRequired,
|
| clearPasteOffset: PropTypes.func.isRequired,
|
| clearSelectedItems: PropTypes.func.isRequired,
|
| clearUndo: PropTypes.func.isRequired,
|
| cursor: PropTypes.string,
|
| format: PropTypes.oneOf(Object.keys(Formats)),
|
| image: PropTypes.oneOfType([
|
| PropTypes.string,
|
| PropTypes.instanceOf(HTMLImageElement)
|
| ]),
|
| imageFormat: PropTypes.string,
|
| imageId: PropTypes.string,
|
| rotationCenterX: PropTypes.number,
|
| rotationCenterY: PropTypes.number,
|
| saveZoomLevel: PropTypes.func.isRequired,
|
| setZoomLevelId: PropTypes.func.isRequired,
|
| theme: PropTypes.string,
|
| undoSnapshot: PropTypes.func.isRequired,
|
| updateViewBounds: PropTypes.func.isRequired,
|
| zoomLevelId: PropTypes.string,
|
| zoomLevels: PropTypes.shape({
|
| currentZoomLevelId: PropTypes.string
|
| })
|
| };
|
| const mapStateToProps = state => ({
|
| mode: state.scratchPaint.mode,
|
| cursor: state.scratchPaint.cursor,
|
| format: state.scratchPaint.format,
|
| zoomLevels: state.scratchPaint.zoomLevels
|
| });
|
| const mapDispatchToProps = dispatch => ({
|
| undoSnapshot: snapshot => {
|
| dispatch(undoSnapshot(snapshot));
|
| },
|
| clearUndo: () => {
|
| dispatch(clearUndoState());
|
| },
|
| clearSelectedItems: () => {
|
| dispatch(clearSelectedItems());
|
| },
|
| clearHoveredItem: () => {
|
| dispatch(clearHoveredItem());
|
| },
|
| clearPasteOffset: () => {
|
| dispatch(clearPasteOffset());
|
| },
|
| changeFormat: format => {
|
| dispatch(changeFormat(format));
|
| },
|
| saveZoomLevel: () => {
|
| dispatch(saveZoomLevel(paper.view.matrix));
|
| },
|
| setZoomLevelId: zoomLevelId => {
|
| dispatch(setZoomLevelId(zoomLevelId));
|
| },
|
| updateViewBounds: matrix => {
|
| dispatch(updateViewBounds(matrix));
|
| }
|
| });
|
|
|
| export default connect(
|
| mapStateToProps,
|
| mapDispatchToProps
|
| )(PaperCanvas);
|
|
|