Skip to content

Commit ceded0e

Browse files
committed
react -> react-native: Zdog and Tests step5 document.createElement('canvas') with offscreenCanvas works well
1 parent a361313 commit ceded0e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

app/components/ZdogAndTests.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ export default class ZdogAndTests extends Component {
396396
this.canvas = null;
397397
this.state = {
398398
debugInfo: 'Click me to draw some on canvas',
399+
hasOc1: false,
399400
};
400401

401402
// only useful on Android, because it's always true on iOS
@@ -499,6 +500,23 @@ export default class ZdogAndTests extends Component {
499500
<TouchableOpacity onPress={this.drawSome}>
500501
<Text style={styles.welcome}>{this.state.debugInfo}</Text>
501502
</TouchableOpacity>
503+
{Platform.OS !== 'web' && (
504+
<GCanvasView
505+
style={{
506+
width: 1000, // 1000 should enough for offscreen canvas usage
507+
height: 1000, // or Dimensions.get('window').height * 2 like https://github.com/flyskywhy/react-native-babylonjs/commit/d5df5d2
508+
position: 'absolute',
509+
left: 1000, // 1000 should enough to not display on screen means offscreen canvas :P
510+
top: 0,
511+
zIndex: -100, // -100 should enough to not bother onscreen canvas
512+
}}
513+
offscreenCanvas={true}
514+
onCanvasCreate={(canvas) => this.setState({hasOc1: true})} // it's better to setState some as describe in https://github.com/flyskywhy/react-native-gcanvas/blob/master/README.MD
515+
devicePixelRatio={1} // should not 1 < devicePixelRatio < 2 as float to avoid pixel offset flaw when GetImageData with PixelsSampler in @flyskywhy/react-native-gcanvas/core/src/support/GLUtil.cpp
516+
isGestureResponsible={false} // who will need gesture with offscreen canvas?
517+
/>
518+
)}
519+
502520
{Platform.OS === 'web' ? (
503521
<canvas
504522
id={'canvasExample'}
@@ -510,7 +528,7 @@ export default class ZdogAndTests extends Component {
510528
} /* canvas with react-native-web can't use width and height in styles.gcanvas */
511529
}
512530
/>
513-
) : (
531+
) : (this.state.hasOc1 && (
514532
<GCanvasView
515533
onCanvasResize={this.onCanvasResize}
516534
onCanvasCreate={this.initCanvas}
@@ -526,7 +544,7 @@ export default class ZdogAndTests extends Component {
526544
}
527545
style={styles.gcanvas}
528546
/>
529-
)}
547+
))}
530548
<TouchableOpacity onPress={this.runTests}>
531549
<Text style={styles.welcome}>
532550
Click me runTests react-native-browser-polyfill

0 commit comments

Comments
 (0)