Slideshow.js 2.28 KB
Newer Older
1 2 3 4 5
import React, { View, Text, StyleSheet } from "react-native";
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
6

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

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

22 23 24 25
    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
26
      this._name = name;
27 28 29 30
      this._shader = shaders[name];
      this._uniforms = uniforms;
    }

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

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

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