Skip to content
This repository was archived by the owner on Aug 1, 2022. It is now read-only.

Commit 4a8c227

Browse files
committed
Unify touch & mouse events
Fixes #29 #42
1 parent 5ca2635 commit 4a8c227

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/index.js

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -275,42 +275,39 @@ export default class extends PureComponent {
275275
});
276276
};
277277

278-
handleTouchStart = e => {
279-
const { x, y } = this.getPointerPos(e);
280-
this.lazy.update({ x, y }, { both: true });
281-
this.handleMouseDown(e);
278+
handleDrawStart = e => {
279+
e.preventDefault();
282280

283-
this.mouseHasMoved = true;
284-
};
281+
// Start drawing
282+
this.isPressing = true;
285283

286-
handleTouchMove = e => {
287-
e.preventDefault();
288284
const { x, y } = this.getPointerPos(e);
289-
this.handlePointerMove(x, y);
290-
};
291285

292-
handleTouchEnd = e => {
293-
this.handleMouseUp(e);
294-
const brush = this.lazy.getBrushCoordinates();
295-
this.lazy.update({ x: brush.x, y: brush.y }, { both: true });
296-
this.mouseHasMoved = true;
286+
if (e.touches && e.touches.length > 0) {
287+
// on touch, set catenary position to touch pos
288+
this.lazy.update({ x, y }, { both: true });
289+
}
290+
291+
// Ensure the initial down position gets added to our line
292+
this.handlePointerMove(x, y);
297293
};
298294

299-
handleMouseDown = e => {
295+
handleDrawMove = e => {
300296
e.preventDefault();
301-
this.isPressing = true;
302-
};
303297

304-
handleMouseMove = e => {
305298
const { x, y } = this.getPointerPos(e);
306299
this.handlePointerMove(x, y);
307300
};
308301

309-
handleMouseUp = e => {
302+
handleDrawEnd = e => {
310303
e.preventDefault();
304+
305+
// Draw to this end pos
306+
this.handleDrawMove(e);
307+
308+
// Stop drawing & save the drawn line
311309
this.isDrawing = false;
312310
this.isPressing = false;
313-
314311
this.saveLine();
315312
};
316313

@@ -359,19 +356,19 @@ export default class extends PureComponent {
359356
handlePointerMove = (x, y) => {
360357
if (this.props.disabled) return;
361358

362-
const hasChanged = this.lazy.update({ x, y });
359+
this.lazy.update({ x, y });
363360
const isDisabled = !this.lazy.isEnabled();
364361

365362
if (
366-
(this.isPressing && hasChanged && !this.isDrawing) ||
363+
(this.isPressing && !this.isDrawing) ||
367364
(isDisabled && this.isPressing)
368365
) {
369366
// Start drawing and add point
370367
this.isDrawing = true;
371368
this.points.push(this.lazy.brush.toObject());
372369
}
373370

374-
if (this.isDrawing && (this.lazy.brushHasMoved() || isDisabled)) {
371+
if (this.isDrawing) {
375372
// Add new point
376373
this.points.push(this.lazy.brush.toObject());
377374

@@ -583,14 +580,14 @@ export default class extends PureComponent {
583580
}
584581
}}
585582
style={{ ...canvasStyle, zIndex }}
586-
onMouseDown={isInterface ? this.handleMouseDown : undefined}
587-
onMouseMove={isInterface ? this.handleMouseMove : undefined}
588-
onMouseUp={isInterface ? this.handleMouseUp : undefined}
589-
onMouseOut={isInterface ? this.handleMouseUp : undefined}
590-
onTouchStart={isInterface ? this.handleTouchStart : undefined}
591-
onTouchMove={isInterface ? this.handleTouchMove : undefined}
592-
onTouchEnd={isInterface ? this.handleTouchEnd : undefined}
593-
onTouchCancel={isInterface ? this.handleTouchEnd : undefined}
583+
onMouseDown={isInterface ? this.handleDrawStart : undefined}
584+
onMouseMove={isInterface ? this.handleDrawMove : undefined}
585+
onMouseUp={isInterface ? this.handleDrawEnd : undefined}
586+
onMouseOut={isInterface ? this.handleDrawEnd : undefined}
587+
onTouchStart={isInterface ? this.handleDrawStart : undefined}
588+
onTouchMove={isInterface ? this.handleDrawMove : undefined}
589+
onTouchEnd={isInterface ? this.handleDrawEnd : undefined}
590+
onTouchCancel={isInterface ? this.handleDrawEnd : undefined}
594591
/>
595592
);
596593
})}

0 commit comments

Comments
 (0)