Skip to content

Commit 457a698

Browse files
author
Thomas Bolis
committed
Adding touch events to canvas
1 parent 3d74482 commit 457a698

File tree

8 files changed

+39
-15
lines changed

8 files changed

+39
-15
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
# Linux trash folder which might appear on any partition or disk
88
.Trash-*
9+
10+
911
### JetBrains template
1012
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
1113

@@ -53,6 +55,8 @@ atlassian-ide-plugin.xml
5355
com_crashlytics_export_strings.xml
5456
crashlytics.properties
5557
crashlytics-build.properties
58+
59+
5660
### OSX template
5761
.DS_Store
5862
.AppleDouble
@@ -78,6 +82,8 @@ Icon
7882
Network Trash Folder
7983
Temporary Items
8084
.apdisk
85+
86+
8187
### Eclipse template
8288
*.pydevproject
8389
.metadata
@@ -118,6 +124,8 @@ local.properties
118124

119125
# TeXlipse plugin
120126
.texlipse
127+
128+
121129
### Node template
122130
# Logs
123131
logs
@@ -147,6 +155,9 @@ build/Release
147155
# Dependency directory
148156
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
149157
node_modules
158+
159+
lib/
160+
150161
### Windows template
151162
# Windows image file caches
152163
Thumbs.db

.npmignore

Whitespace-only changes.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-sketch",
3-
"version": "0.1.0",
4-
"description": "Sketch Element for React",
3+
"version": "0.1.1",
4+
"description": "Simple Sketch Element for React based applications",
55
"keywords": [
66
"React",
77
"canvas",

src/Line.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import CanvasTool from './CanvasTool'
2-
import {mousePosition} from './utils';
2+
import {pointerPosition} from './utils';
33

44
class Line extends CanvasTool {
55

66
doMouseDown(event) {
7-
let [_x, _y] = mousePosition(event);
7+
let [_x, _y] = pointerPosition(event);
88
// store initial position of mouse
99
[this._startX, this._startY] = [_x, _y];
1010
[this._x, this._y] = [_x, _y];
@@ -15,7 +15,7 @@ class Line extends CanvasTool {
1515

1616
doMouseMove(event) {
1717
let [_startX,_startY] = [this._startX, this._startY];
18-
let [_currX, _currY] = mousePosition(event);
18+
let [_currX, _currY] = pointerPosition(event);
1919
if (this._isMouseDown) {
2020
this._ctx.beginPath();
2121
this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);

src/Pencil.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import CanvasTool from './CanvasTool'
2-
import {mousePosition} from './utils';
2+
import {pointerPosition} from './utils';
33

44
class Pencil extends CanvasTool {
55

@@ -8,7 +8,7 @@ class Pencil extends CanvasTool {
88
}
99

1010
doMouseDown(event) {
11-
let [_x, _y] = mousePosition(event);
11+
let [_x, _y] = pointerPosition(event);
1212
// store initial position of mouse
1313
[this._startX, this._startY] = [_x, _y];
1414
[this._x, this._y] = [_x, _y];
@@ -18,7 +18,7 @@ class Pencil extends CanvasTool {
1818

1919
doMouseMove(event) {
2020
let [_prevX,_prevY] = [this._x, this._y];
21-
let [_currX, _currY] = mousePosition(event);
21+
let [_currX, _currY] = pointerPosition(event);
2222
if (this._isMouseDown) {
2323
this._ctx.beginPath();
2424
this._ctx.moveTo(_prevX, _prevY);

src/Rectangle.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import CanvasTool from './CanvasTool'
2-
import {mousePosition} from './utils';
2+
import {pointerPosition} from './utils';
33

44
class Rectangle extends CanvasTool {
55

@@ -9,7 +9,7 @@ class Rectangle extends CanvasTool {
99
}
1010

1111
doMouseDown(event) {
12-
let [_x, _y] = mousePosition(event);
12+
let [_x, _y] = pointerPosition(event);
1313
// store initial position of mouse
1414
[this._startX, this._startY] = [_x, _y];
1515
[this._x, this._y] = [_x, _y];
@@ -20,7 +20,7 @@ class Rectangle extends CanvasTool {
2020

2121
doMouseMove(event) {
2222
let [_startX,_startY] = [this._startX, this._startY];
23-
let [_currX, _currY] = mousePosition(event);
23+
let [_currX, _currY] = pointerPosition(event);
2424
if (this._isMouseDown) {
2525
this._ctx.beginPath();
2626
this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);

src/SketchField.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ class SketchField extends React.Component {
5454
componentDidMount() {
5555
// Assign the events to canvas element
5656
this._canvas.addEventListener('mouseup', this._mouseUp, false);
57+
this._canvas.addEventListener('touchend', this._mouseUp, false);
58+
5759
this._canvas.addEventListener('mousedown', this._mouseDown, false);
60+
this._canvas.addEventListener('touchstart', this._mouseDown, false);
61+
5862
this._canvas.addEventListener('mousemove', this._mouseMove, false);
59-
this._canvas.addEventListener('mouseout', this._mouseOut, false);
63+
this._canvas.addEventListener('touchmove', this._mouseMove, false);
64+
65+
this._canvas.addEventListener('mouseout', this._mouseOut);
66+
6067
// Handle the resize of Canvas
6168
this._canvas.width = ReactDOM.findDOMNode(this).offsetWidth;
6269
window.addEventListener('resize', this._resize, false);
@@ -131,6 +138,7 @@ class SketchField extends React.Component {
131138
* @private
132139
*/
133140
_mouseUp(event) {
141+
if (event) event.preventDefault();
134142
// Call corresponding tool action
135143
let tool = this._tools[this.props.tool || Tool.Pencil];
136144
tool.doMouseUp(event);
@@ -152,6 +160,7 @@ class SketchField extends React.Component {
152160
* @private
153161
*/
154162
_mouseMove(event) {
163+
if (event) event.preventDefault();
155164
let tool = this._tools[this.props.tool || Tool.Pencil];
156165
tool.doMouseMove(event);
157166
}
@@ -163,6 +172,7 @@ class SketchField extends React.Component {
163172
* @private
164173
*/
165174
_mouseDown(event) {
175+
if (event) event.preventDefault();
166176
let tool = this._tools[this.props.tool || Tool.Pencil];
167177
tool.doMouseDown(event);
168178
}
@@ -174,6 +184,7 @@ class SketchField extends React.Component {
174184
* @private
175185
*/
176186
_mouseOut(event) {
187+
if (event) event.preventDefault();
177188
let tool = this._tools[this.props.tool || Tool.Pencil];
178189
tool.doMouseOut(event);
179190
// there is no direct on change event

src/utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
* @returns *[] tuple of position x,y
66
* @private
77
*/
8-
export function mousePosition(event) {
8+
export function pointerPosition(event) {
99
event = event || window.event;
1010
var target = event.target || event.srcElement,
1111
style = target.currentStyle || window.getComputedStyle(target, null),
1212
borderLeftWidth = parseInt(style['borderLeftWidth'], 10),
1313
borderTopWidth = parseInt(style['borderTopWidth'], 10),
1414
rect = target.getBoundingClientRect(),
1515
_x = event.clientX - borderLeftWidth - rect.left,
16-
_y = event.clientY - borderTopWidth - rect.top;
17-
return [_x, _y];
16+
_y = event.clientY - borderTopWidth - rect.top,
17+
_touchX = event.changedTouches ? event.changedTouches[0].clientX - borderLeftWidth - rect.left : null,
18+
_touchY = event.changedTouches ? event.changedTouches[0].clientY - borderTopWidth - rect.top : null;
19+
return [(_x || _touchX), (_y || _touchY)];
1820
}

0 commit comments

Comments
 (0)