Slideshow.js 2.14 KB
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1
const React = require("react-native");
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
2
const { View, Text, StyleSheet } = React;
3
const GL = require("gl-react");
4
const {Surface} = require("gl-react-native");
5
const TransitionGenerator = require("./TransitionGenerator");
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
6 7
const Transition = require("./Transition");

8
const shaders = GL.Shaders.create(TransitionGenerator.shaders);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
9 10

class Slideshow extends React.Component {
11 12 13 14
  constructor (props) {
    super(props);
    this._currentTransition = -1;
  }
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
15
  render () {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
16 17
    const { transitionDuration, pauseDuration, width, height, time, images } = this.props;
    const duration = transitionDuration + pauseDuration;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
18
    const slide = time / duration;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
19
    let transitionProgress = Math.min(1, (duration/transitionDuration) * (slide % 1));
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
20 21 22
    let transitionFrom = images[Math.floor(slide) % images.length];
    let transitionTo = images[Math.floor(slide+1) % images.length];

23 24 25 26
    const currentTransition = Math.floor(slide);
    if (currentTransition !== this._currentTransition) {
      this._currentTransition = currentTransition;
      const { name, uniforms } = TransitionGenerator.random();
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
27
      this._name = name;
28 29 30 31
      this._shader = shaders[name];
      this._uniforms = uniforms;
    }

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
32
    const transitionName = this._name;
33 34 35
    const transitionShader = this._shader;
    const transitionUniforms = this._uniforms;

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
36
    return <View style={styles.root}>
37 38 39 40 41 42 43 44 45
      <Surface  width={width} height={height}>
        <Transition
          progress={transitionProgress}
          from={transitionFrom}
          to={transitionTo}
          shader={transitionShader}
          uniforms={transitionUniforms}
        />
      </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
46 47 48 49 50
    <View style={styles.legend}>
        <Text style={styles.textName}>{transitionName}</Text>
        <Text style={styles.textInfo}>(GLSL.io)</Text>
      </View>
    </View>;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
51 52 53
  }
}

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
const styles = StyleSheet.create({
  root: {
    position: "relative"
  },
  legend: {
    position: "absolute",
    bottom: 5,
    right: 4,
    backgroundColor: "transparent",
    flexDirection: "row"
  },
  textName: {
    color: "#fff",
    fontWeight: "bold",
  },
  textInfo: {
    color: "#fff",
    marginLeft: 8,
    opacity: 0.5
  }
});

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
76
module.exports = Slideshow;