Skip to content

Commit 3255412

Browse files
committed
feat: support rotate
1 parent 7004eae commit 3255412

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ all callback funtion will one parammeter: `(gesture) => {}`
136136
</tbody>
137137
</table>
138138

139-
#### Pan
139+
#### Pan (**Unsupported Yet*)
140140
<table class="table table-bordered table-striped">
141141
<thead>
142142
<tr>

examples/simple.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* tslint:disable:no-console */
1+
/* tslint:disable:no-console no-unused-expression */
22

33
import Gesture from '../src/index';
44
import React, { Component } from 'react';
@@ -25,23 +25,45 @@ const style = `
2525
class Demo extends Component<any, any> {
2626
private root: any;
2727
private rootNode: any;
28+
private _scale: number;
29+
private _rotation: number;
2830

2931
constructor(props) {
3032
super(props);
3133
}
3234

3335
log = (type: string, keys?: string[]) => (...args) => {
34-
console.log(type, ...args);
36+
this.doTapOrPress(type, keys, ...args);
37+
this.doTransform(type, ...args);
38+
}
39+
doTapOrPress = (type, keys, ...args) => {
40+
if (['onTap', 'onPress', 'onPressUp'].indexOf(type) === -1) {
41+
return;
42+
}
3543
const extInfo = keys ? keys.map(key => `${key} = ${args[0][key]}`).join(', ') : '';
3644
const logEl = this.refs.log as any;
3745
logEl.innerHTML += `<p>${type} ${extInfo}</p>`;
3846
logEl.scrollTop = logEl.scrollHeight;
47+
}
48+
doTransform = (type, ...args) => {
3949
if (type === 'onPinch') {
4050
const { scale } = args[0];
41-
this.rootNode = ReactDOM.findDOMNode(this.root);
42-
this.rootNode.style.transform = `scale(${scale})`;
51+
this._scale = scale;
4352
}
53+
if (type === 'onRotate') {
54+
const { rotation } = args[0];
55+
this._rotation = rotation;
56+
}
57+
let transform: any = [];
58+
// console.log(type, ...args); let transform: any = [];
59+
this._scale && transform.push(`scale(${this._scale})`);
60+
this._rotation && transform.push(`rotate(${this._rotation}deg)`);
61+
62+
transform = transform.join(' ');
63+
this.rootNode = ReactDOM.findDOMNode(this.root);
64+
this.rootNode.style.transform = transform;
4465
}
66+
4567
render() {
4668
return (
4769
<div>

src/index.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,14 @@ export default class Gesture extends Component<IGesture, any> {
170170
}
171171
}
172172
cleanGestureState = () => {
173-
console.log('clean gesture state');
173+
console.log('clean pre gesture state');
174174
delete this.gesture;
175175
}
176176
getTouches = (e) => {
177-
return Array.prototype.slice.call(e.touches).map(item => ([item.pageX, item.pageY]));
177+
return Array.prototype.slice.call(e.touches).map(item => ({
178+
x: item.pageX,
179+
y: item.pageY,
180+
}));
178181
}
179182

180183
_handleTouchStart = (e) => {
@@ -265,8 +268,6 @@ export default class Gesture extends Component<IGesture, any> {
265268
this.triggerCombineEvent('onPinch', 'move');
266269
}
267270
if (rotate) {
268-
// Todo: wait to see if we can use below hammerjs way?
269-
// https://github.com/hammerjs/hammer.js/blob/master/src/inputjs/get-rotation.js
270271
const rotation = calcRotation(startMutliFingerStatus, mutliFingerStatus);
271272
this.setGestureState({
272273
rotation,
@@ -331,9 +332,7 @@ export default class Gesture extends Component<IGesture, any> {
331332
}
332333

333334
checkIfEndWithTapOrSwipe = () => {
334-
const {
335-
startTime, startTouches, moveStatus, pinch, rotate, press, time,
336-
} = this.gesture;
335+
const { moveStatus, pinch, rotate, press } = this.gesture;
337336

338337
if (pinch || rotate) {
339338
return;

src/util.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,10 @@ export function calcMoveStatus(startTouches, touches, time) {
4545
};
4646
}
4747
export function calcRotation(startMutliFingerStatus, mutliFingerStatus) {
48-
const { x: startX, y: startY, z: startZ } = startMutliFingerStatus;
49-
const { x, y, z } = mutliFingerStatus;
50-
if (startZ === 0 || z === 0) {
51-
return 0;
52-
}
53-
// https://en.wikipedia.org/wiki/Dot_product
54-
let cosine = (startX * x + startY * y) / startZ * z;
55-
if (cosine > 1) {
56-
cosine = 1;
57-
}
58-
return Math.acos(cosine);
48+
const { angle: startAngle } = startMutliFingerStatus;
49+
const { angle } = mutliFingerStatus;
50+
51+
return angle - startAngle;
5952
}
6053

6154
export function getEventName(prefix, status) {

0 commit comments

Comments
 (0)