Skip to content

Commit ee3f3fb

Browse files
add mobile touch support
1 parent 64e5cf3 commit ee3f3fb

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/fluid-animation.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ export default class FluidAnimation {
123123
this._pointers[0].down = false
124124
}
125125

126+
onTouchStart = (e) => {
127+
for (let i = 0; i < e.touches.length; ++i) {
128+
this._pointers[i].down = true
129+
this._pointers[i].color = [
130+
Math.random() + 0.2,
131+
Math.random() + 0.2,
132+
Math.random() + 0.2
133+
]
134+
}
135+
}
136+
137+
onTouchMove = (e) => {
138+
for (let i = 0; i < e.touches.length; ++i) {
139+
const touch = e.touches[i]
140+
this._pointers[i].moved = this._pointers[i].down
141+
this._pointers[i].dx = (touch.clientX - this._pointers[i].x) * 10.0
142+
this._pointers[i].dy = (touch.clientY - this._pointers[i].y) * 10.0
143+
this._pointers[i].x = touch.clientX
144+
this._pointers[i].y = touch.clientY
145+
}
146+
}
147+
148+
onTouchEnd = (e) => {
149+
for (let i = 0; i < e.touches.length; ++i) {
150+
this._pointers[i].down = false
151+
}
152+
}
153+
126154
_initPrograms() {
127155
const gl = this._gl
128156
const ext = this._ext

src/react-fluid-animation.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class ReactFluidAnimation extends Component {
7474
onMouseDown={this._onMouseDown}
7575
onMouseMove={this._onMouseMove}
7676
onMouseUp={this._onMouseUp}
77+
onTouchStart={this._onTouchStart}
78+
onTouchMove={this._onTouchMove}
79+
onTouchEnd={this._onTouchEnd}
7780
style={{
7881
width: '100%',
7982
height: '100%'
@@ -106,6 +109,21 @@ class ReactFluidAnimation extends Component {
106109
this._animation.onMouseUp(event.nativeEvent)
107110
}
108111

112+
_onTouchStart = (event) => {
113+
event.preventDefault()
114+
this._animation.onTouchStart(event.nativeEvent)
115+
}
116+
117+
_onTouchMove = (event) => {
118+
event.preventDefault()
119+
this._animation.onTouchMove(event.nativeEvent)
120+
}
121+
122+
_onTouchEnd = (event) => {
123+
event.preventDefault()
124+
this._animation.onTouchEnd(event.nativeEvent)
125+
}
126+
109127
_onResize = () => {
110128
this._canvas.width = this._container.clientWidth
111129
this._canvas.height = this._container.clientHeight

0 commit comments

Comments
 (0)