Skip to content

Commit

Permalink
Merge pull request fabricjs#11 from wmaillard/master
Browse files Browse the repository at this point in the history
Added a Pan Tool (by wmaillard)
  • Loading branch information
tbolis authored Mar 4, 2017
2 parents c440bb3 + 9e0a76f commit 83b0350
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class SketchFieldDemo extends React.Component {
<MenuItem value={Tools.Line} primaryText="Line"/>
<MenuItem value={Tools.Rectangle} primaryText="Rectangle"/>
<MenuItem value={Tools.Circle} primaryText="Circle"/>
<MenuItem value={Tools.Pan} primaryText="Pan"/>
</SelectField>
<br/>
<br/>
Expand Down
7 changes: 7 additions & 0 deletions src/SketchField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Pencil from './pencil';
import Line from './line';
import Rectangle from './rectangle';
import Circle from './circle';
import Pan from './pan';
import Tool from './tools';

const fabric = require('fabric').fabric;
Expand Down Expand Up @@ -149,6 +150,7 @@ class SketchField extends Component {
this._tools[Tool.Line] = new Line(fabricCanvas);
this._tools[Tool.Rectangle] = new Rectangle(fabricCanvas);
this._tools[Tool.Circle] = new Circle(fabricCanvas);
this._tools[Tool.Pan] = new Pan(fabricCanvas);
}

componentWillUnmount() {
Expand All @@ -168,10 +170,15 @@ class SketchField extends Component {
if (this.props.tool !== nextProps.tool) {
this._selectedTool = this._tools[nextProps.tool] || this._tools[Tool.Pencil];
}

//Bring the cursor back to default if it is changed by a tool
this._fc.defaultCursor = 'default';

this._selectedTool.configureCanvas(nextProps);
if (this.props.backgroundColor !== nextProps.backgroundColor) {
this._backgroundColor(nextProps.backgroundColor)
}

}

/**
Expand Down
41 changes: 41 additions & 0 deletions src/pan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*eslint no-unused-vars: 0*/
'use strict';

import FabricCanvasTool from './fabrictool'
const fabric = require('fabric').fabric;

class Pan extends FabricCanvasTool {

configureCanvas(props) {
let canvas = this._canvas;
canvas.isDrawingMode = canvas.selection = false;
canvas.forEachObject((o) => o.selectable = o.evented = false);
//Change the cursor to the move grabber
canvas.defaultCursor = 'move';
}

doMouseDown(o) {
let canvas = this._canvas;
this.isDown = true;
let pointer = canvas.getPointer(o.e);
this.startX = pointer.x;
this.startY = pointer.y;
}

doMouseMove(o) {
if (!this.isDown) return;
let canvas = this._canvas;
let pointer = canvas.getPointer(o.e);

canvas.relativePan({ x : pointer.x - this.startX,
y : pointer.y - this.startY});
canvas.renderAll();
}

doMouseUp(o) {
this.isDown = false;
}

}

export default Pan;
3 changes: 2 additions & 1 deletion src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export default {
Line: 'line',
Pencil: 'pencil',
Rectangle: 'rectangle',
Select: 'select'
Select: 'select',
Pan: 'pan'
}

0 comments on commit 83b0350

Please sign in to comment.