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

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
      <Surface width={width} height={height}
        onLoad={() => console.log("Slideshow onLoad")}
        onProgress={e => console.log("Slideshow onProgress", e.nativeEvent)}>
40 41 42 43 44 45
        <Transition
          progress={transitionProgress}
          from={transitionFrom}
          to={transitionTo}
          shader={transitionShader}
          uniforms={transitionUniforms}
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
46 47
          width={width}
          height={height}
48 49
        />
      </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
50 51 52 53 54
    <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
55 56 57
  }
}

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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
80
module.exports = Slideshow;