index.js 10.5 KB
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1
import React, { Component } from "react";
2 3 4 5 6 7 8 9
import {
  StyleSheet,
  Text,
  View,
  Image,
  TextInput,
  TouchableOpacity,
  Slider,
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
10
  Switch
11
} from "react-native";
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
12

13
import { Surface } from "gl-react-native";
14

15 16 17 18 19 20 21
import RNFS from "react-native-fs";
import HelloGL from "./HelloGL";
import Saturation from "./Saturation";
import HueRotate from "./HueRotate";
import PieProgress from "./PieProgress";
import OneFingerResponse from "./OneFingerResponse";
import AnimatedHelloGL from "./AnimatedHelloGL";
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
22
import { Blur } from "gl-react-blur";
23
import Button from "./Button";
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
24 25

class Demo extends Component {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
26
  render() {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
27
    const { title, children } = this.props;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
28 29 30 31 32 33 34 35
    return (
      <View>
        <Text style={styles.demoTitle}>
          {title}
        </Text>
        <View style={styles.demo}>
          {children}
        </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
36
      </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
37
    );
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
38 39 40 41
  }
}

class Demos extends Component {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
42
  render() {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
43
    const { children, onChange, value } = this.props;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    return (
      <View>
        <View style={styles.nav}>
          {React.Children.map(children, (demo, i) =>
            <Text
              style={{ flex: 1, padding: 10 }}
              textStyle={{
                textAlign: "center",
                color: i !== value ? "#123" : "#69c",
                fontWeight: "bold"
              }}
              onPress={() => onChange(i)}
            >
              {"" + (i + 1)}
            </Text>
          )}
        </View>
        <View style={styles.demos}>
          {children[value]}
        </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
64
      </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
65
    );
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
66 67 68 69
  }
}

class Simple extends Component {
70 71 72 73 74 75 76 77 78 79 80 81 82
  state = {
    current: 0,
    saturationFactor: 1,
    hue: 0,
    progress: 0,
    factor: 0,
    text: "and I will return leading the pack",
    switch1: false,
    switch2: false,
    switch3: false,
    captured: null,
    captureConfig: null
  };
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
83

84
  onCapture1 = () => {
85
    const captureConfig = {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
86 87
      quality: Math.round(Math.random() * 100) / 100,
      type: Math.random() < 0.5 ? "jpg" : "png",
88 89 90
      format: Math.random() < 0.5 ? "base64" : "file"
    };
    if (captureConfig.format === "file") {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
91 92
      captureConfig.filePath =
        RNFS.DocumentDirectoryPath + "/hellogl_capture.png";
93 94
    }
    this.refs.helloGL
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
95 96
      .captureFrame(captureConfig)
      .then(captured => this.setState({ captured, captureConfig }));
97
  };
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
98

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
99
  render() {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
100 101 102 103 104 105 106 107 108 109
    const {
      current,
      saturationFactor,
      hue,
      text,
      progress,
      factor,
      switch1,
      switch2,
      switch3,
110 111
      captured,
      captureConfig
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
112 113
    } = this.state;

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
    return (
      <View style={styles.container}>
        <Demos onChange={current => this.setState({ current })} value={current}>
          <Demo id={1} title="1. Hello GL">
            <Surface width={256} height={171} ref="helloGL">
              <HelloGL />
            </Surface>
            <View style={{ marginTop: 20, flexDirection: "row" }}>
              <Button onPress={this.onCapture1}>captureFrame()</Button>
              {captured &&
                <Image
                  source={{ uri: captured }}
                  style={{ marginLeft: 20, width: 51, height: 34 }}
                />}
128
            </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            {captureConfig &&
              <View
                style={{
                  paddingTop: 20,
                  alignItems: "center",
                  flexDirection: "row",
                  justifyContent: "space-between"
                }}
              >
                <Text style={{ fontSize: 10 }}>
                  format={captureConfig.format}
                </Text>
                <Text style={{ fontSize: 10 }}>
                  type={captureConfig.type}
                </Text>
                <Text style={{ fontSize: 10 }}>
                  quality={captureConfig.quality + ""}
                </Text>
              </View>}
            {captured &&
              <Text
                numberOfLines={1}
                style={{ marginTop: 10, fontSize: 10, color: "#aaa" }}
              >
                {captured.slice(0, 100)}
              </Text>}
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
156

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
157 158 159 160 161 162 163 164 165 166 167
          <Demo id={2} title="2. Saturate an Image">
            <Surface width={256} height={171}>
              <Saturation
                factor={saturationFactor}
                image={{ uri: "https://i.imgur.com/iPKTONG.jpg" }}
              />
            </Surface>
            <Slider
              maximumValue={8}
              onValueChange={saturationFactor =>
                this.setState({ saturationFactor })}
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
168
            />
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
169
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
170

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
          <Demo id={3} current={current} title="3. Hue Rotate on Text+Image">
            <Surface autoRedraw width={256} height={180}>
              <HueRotate hue={hue}>
                <View key="hue" style={{ width: 256, height: 180 }}>
                  <Image
                    style={{ width: 256, height: 244 }}
                    source={{ uri: "https://i.imgur.com/qVxHrkY.jpg" }}
                  />
                  <Text style={styles.demoText1}>Throw me to the wolves</Text>
                  <Text style={styles.demoText2}>
                    {text}
                  </Text>
                </View>
              </HueRotate>
            </Surface>
            <Slider
              maximumValue={2 * Math.PI}
              onValueChange={hue => this.setState({ hue })}
            />
            <TextInput
              style={{ height: 40, borderColor: "#aaa", borderWidth: 1 }}
              onChangeText={text => this.setState({ text })}
              value={text}
            />
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
196

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
          <Demo id={4} current={current} title="4. Progress Indicator">
            <View style={{ position: "relative", width: 256, height: 180 }}>
              <TouchableOpacity>
                <Image
                  source={{ uri: "https://i.imgur.com/qM9BHCy.jpg" }}
                  style={{
                    width: 256,
                    height: 180,
                    position: "absolute",
                    top: 0,
                    left: 0
                  }}
                />
              </TouchableOpacity>
              <View
                pointerEvents="box-none"
213 214 215
                style={{
                  position: "absolute",
                  top: 0,
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
216 217
                  left: 0,
                  backgroundColor: "transparent"
218
                }}
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
219 220 221 222 223 224 225 226 227 228 229
              >
                <Surface
                  width={256}
                  height={180}
                  backgroundColor="transparent"
                  setZOrderOnTop
                  eventsThrough
                >
                  <PieProgress progress={progress} />
                </Surface>
              </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
230
            </View>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
231 232 233 234 235
            <Slider
              maximumValue={1}
              onValueChange={progress => this.setState({ progress })}
            />
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
236

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
237 238 239
          <Demo id={5} current={current} title="5. Touch Responsive">
            <OneFingerResponse width={256} height={180} />
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
240

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
241 242 243
          <Demo id={6} current={current} title="6. Animation">
            <AnimatedHelloGL width={256} height={180} />
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
244

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
245 246 247 248
          <Demo id={7} current={current} title="7. Blur">
            <Surface preload width={256} height={180}>
              <Blur factor={factor * 2} passes={4}>
                https://i.imgur.com/3On9QEu.jpg
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
249
              </Blur>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
250 251 252 253 254 255
            </Surface>
            <Slider
              maximumValue={2}
              onValueChange={factor => this.setState({ factor })}
            />
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
256

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
          <Demo id={8} current={current} title="8. Blur+Hue over UI">
            <Surface
              width={256}
              height={160}
              autoRedraw
              eventsThrough
              visibleContent
            >
              <HueRotate hue={-switch1 + 2 * switch2 + 4 * switch3}>
                <Blur factor={factor}>
                  <View
                    key="blur"
                    style={{
                      width: 256,
                      height: 160,
                      padding: 10,
                      backgroundColor: "#f9f9f9"
                    }}
                  >
                    <Slider
                      style={{ height: 80 }}
                      maximumValue={1}
                      onValueChange={factor => this.setState({ factor })}
                    />
                    <View
                      style={{
                        height: 60,
                        flexDirection: "row",
                        alignItems: "center"
                      }}
                    >
                      <Switch
                        style={{ flex: 1 }}
                        value={switch1}
                        onValueChange={switch1 => this.setState({ switch1 })}
                      />
                      <Switch
                        style={{ flex: 1 }}
                        value={switch2}
                        onValueChange={switch2 => this.setState({ switch2 })}
                      />
                      <Switch
                        style={{ flex: 1 }}
                        value={switch3}
                        onValueChange={switch3 => this.setState({ switch3 })}
                      />
                    </View>
                  </View>
                </Blur>
              </HueRotate>
            </Surface>
            <Text>
              Note: This is highly experimental and not yet performant enough.
            </Text>
          </Demo>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
312

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
313 314 315 316 317 318
          <Demo id={9} current={current} title="9. Texture from array">
            <Text>Not Supported Yet</Text>
          </Demo>
        </Demos>
      </View>
    );
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
319 320 321 322 323 324 325
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#F9F9F9",
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
326
    paddingTop: 50
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
327 328 329 330 331 332 333 334 335 336
  },
  nav: {
    flexDirection: "row",
    marginBottom: 20
  },
  demos: {
    flex: 1,
    justifyContent: "center",
    marginLeft: 40,
    width: 276,
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
337
    marginBottom: 40
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
338 339 340 341 342 343 344
  },
  demoTitle: {
    marginBottom: 16,
    fontStyle: "italic",
    alignSelf: "flex-start",
    color: "#999",
    fontWeight: "300",
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
345
    fontSize: 20
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
346 347 348
  },
  demo: {
    marginBottom: 64,
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
349
    marginLeft: 20
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  },
  demoText1: {
    position: "absolute",
    top: 0,
    left: 0,
    width: 256,
    textAlign: "center",
    color: "#f16",
    backgroundColor: "transparent",
    fontWeight: "400",
    fontSize: 24,
    letterSpacing: 0
  },
  demoText2: {
    position: "absolute",
    bottom: 4,
    left: 0,
    width: 256,
    textAlign: "center",
    color: "#7bf",
    backgroundColor: "transparent",
    fontWeight: "300",
    fontSize: 32,
    letterSpacing: -1
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
374
  }
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
375 376 377
});

module.exports = Simple;