Skip to content

Commit cf49cf7

Browse files
committed
first impl of gl-react-native (wip)
it works but remain some bugs and image texture is not yet supported
1 parent 4f1622a commit cf49cf7

File tree

299 files changed

+25319
-121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+25319
-121
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright (c) 2016 <[email protected]>
2+
Copyright (c) 2016 - 2017 <[email protected]>
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of
55
this software and associated documentation files (the "Software"), to deal in

cookbook-exp/.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": ["react-native-stage-0/decorator-support"],
3+
"env": {
4+
"development": {
5+
"plugins": ["transform-react-jsx-source"]
6+
}
7+
}
8+
}

cookbook-exp/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/**/*
2+
.exponent/*
3+
npm-debug.*

cookbook-rn/exp.json renamed to cookbook-exp/exp.json

+6
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@
1717
},
1818
"packagerOpts": {
1919
"assetExts": ["ttf", "mp4"]
20+
},
21+
"android": {
22+
"package": "fr.greweb.glrncookbook"
23+
},
24+
"ios": {
25+
"bundleIdentifier": "fr.greweb.glrncookbook"
2026
}
2127
}
File renamed without changes.

cookbook-exp/package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "glrn-cookbook",
3+
"version": "0.0.0",
4+
"description": "GL React Native cookbook",
5+
"author": "Gaëtan Renaudeau",
6+
"private": true,
7+
"main": "main.js",
8+
"dependencies": {
9+
"@exponent/ex-navigation": "^2.7.1",
10+
"exponent": "^13.0.0",
11+
"gl-react": "^3.0.0-alpha.3",
12+
"gl-react-exponent": "^3.0.0-alpha.3",
13+
"glsl-transitions": "^2017.1.16",
14+
"ndarray": "^1.0.18",
15+
"raf": "^3.3.0",
16+
"react": "~15.4.2",
17+
"react-motion": "^0.4.7",
18+
"react-native": "github:exponentjs/react-native#sdk-13.0.0",
19+
"seedrandom": "github:gre/seedrandom#released"
20+
},
21+
"scripts": {
22+
"generate-examples": "cd src/examples; ./gen.sh 1> index.js"
23+
}
24+
}

cookbook-exp/src/0BJobQo.jpg

57 KB
Loading

cookbook-exp/src/0PkQEk1.jpg

131 KB
Loading

cookbook-exp/src/0bUSEBX.jpg

97.9 KB
Loading

cookbook-exp/src/2VP5osy.jpg

241 KB
Loading

cookbook-exp/src/5EOyTDQ.jpg

155 KB
Loading

cookbook-exp/src/CKlmtPs.jpg

80 KB
Loading

cookbook-exp/src/G2Whuq3.jpg

80.2 KB
Loading

cookbook-exp/src/GQo1KWq.jpg

296 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//@flow
2+
import React, { Component } from "react";
3+
import { PanResponder, View } from "react-native";
4+
import hoistNonReactStatics from "hoist-non-react-statics";
5+
6+
type Pos = { x: number, y: number };
7+
type State = { touching: boolean, touchPosition: Pos };
8+
9+
export default (Comp: ReactClass<*>, {
10+
initialPosition = { x: 0.5, y: 0.5 }
11+
}: { initialPosition: Pos } = {}) => {
12+
class TouchPositionResponding extends Component {
13+
state: State = {
14+
touching: false,
15+
touchPosition: initialPosition,
16+
};
17+
initialContainerPos: [number, number];
18+
initialDragPos: [number, number];
19+
size: [number, number];
20+
panResponder = PanResponder.create({
21+
onStartShouldSetPanResponder: () => true,
22+
onStartShouldSetPanResponderCapture: () => true,
23+
onMoveShouldSetPanResponder: () => true,
24+
onMoveShouldSetPanResponderCapture: () => true,
25+
onPanResponderGrant: (evt) => {
26+
const { pageX, pageY } = evt.nativeEvent;
27+
this.initialDragPos = [ pageX, pageY ];
28+
this.refs.root.measure((x, y, w, h, initialPageX, initialPageY) => {
29+
this.initialContainerPos = [ initialPageX, initialPageY ];
30+
this.size = [ w, h ];
31+
this.setState({
32+
touching: true,
33+
touchPosition: {
34+
x: (pageX - initialPageX) / w,
35+
y: 1 - (pageY - initialPageY) / h,
36+
}
37+
});
38+
});
39+
},
40+
onPanResponderMove: (evt, gestureState) => {
41+
const [ pageX, pageY ] = this.initialDragPos;
42+
const [ initialPageX, initialPageY ] = this.initialContainerPos;
43+
const { dx, dy } = gestureState;
44+
const [ w, h ] = this.size;
45+
this.setState({
46+
touchPosition: {
47+
x: (pageX + dx - initialPageX) / w,
48+
y: 1 - (pageY + dy - initialPageY) / h,
49+
}
50+
});
51+
},
52+
onPanResponderTerminationRequest: () => true,
53+
onPanResponderRelease: () => this._onEnd(),
54+
onPanResponderTerminate: () => this._onEnd(),
55+
onShouldBlockNativeResponder: () => true,
56+
});
57+
_onEnd = () => {
58+
if (this.state.touching) {
59+
this.setState({
60+
touching: false,
61+
});
62+
}
63+
};
64+
render() {
65+
return (
66+
<View ref="root" {...this.panResponder.panHandlers}>
67+
<Comp {...this.props} {...this.state} />
68+
</View>
69+
);
70+
}
71+
}
72+
73+
hoistNonReactStatics(TouchPositionResponding, Comp);
74+
75+
return TouchPositionResponding;
76+
};

cookbook-exp/src/HOC/timeLoop.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//@flow
2+
import React, { PureComponent } from "react";
3+
import raf from "raf";
4+
import hoistNonReactStatics from "hoist-non-react-statics";
5+
6+
// NB this is only an utility for the examples
7+
export default (
8+
C: ReactClass<*>,
9+
{ refreshRate = 60 }: { refreshRate?: number } = {}
10+
): ReactClass<*> => {
11+
class TL extends PureComponent {
12+
static displayName = `timeLoop(${C.displayName||C.name||""})`;
13+
state: { time: number };
14+
state = {
15+
time: 0,
16+
tick: 0,
17+
};
18+
_r: any;
19+
componentDidMount() {
20+
let startTime: number, lastTime: number;
21+
let interval = 1000 / refreshRate;
22+
lastTime = -interval;
23+
const loop = (t: number) => {
24+
this._r = raf(loop);
25+
if (!startTime) startTime = t;
26+
if (t - lastTime > interval) {
27+
lastTime = t;
28+
this.setState({
29+
time: t - startTime,
30+
tick: this.state.tick + 1,
31+
});
32+
}
33+
};
34+
this._r = raf(loop);
35+
}
36+
componentWillUnmount() {
37+
raf.cancel(this._r);
38+
}
39+
render() {
40+
return <C
41+
{...this.props}
42+
{...this.state}
43+
/>;
44+
}
45+
};
46+
47+
hoistNonReactStatics(TL, C);
48+
49+
return TL;
50+
}

0 commit comments

Comments
 (0)