canvas.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright (c) 2020-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as React from 'react';
  8. import { PluginUIComponent } from '../base';
  9. interface ViewportCanvasState {
  10. noWebGl: boolean
  11. showLogo: boolean
  12. }
  13. export interface ViewportCanvasParams {
  14. logo?: React.FC,
  15. noWebGl?: React.FC,
  16. parentClassName?: string,
  17. parentStyle?: React.CSSProperties,
  18. hostClassName?: string,
  19. hostStyle?: React.CSSProperties,
  20. }
  21. export class ViewportCanvas extends PluginUIComponent<ViewportCanvasParams, ViewportCanvasState> {
  22. private container = React.createRef<HTMLDivElement>();
  23. private canvas = React.createRef<HTMLCanvasElement>();
  24. state: ViewportCanvasState = {
  25. noWebGl: false,
  26. showLogo: true
  27. };
  28. private handleLogo = () => {
  29. this.setState({ showLogo: !this.plugin.canvas3d?.reprCount.value });
  30. };
  31. componentDidMount() {
  32. if (!this.canvas.current || !this.container.current || !this.plugin.initViewer(this.canvas.current!, this.container.current!)) {
  33. this.setState({ noWebGl: true });
  34. return;
  35. }
  36. this.handleLogo();
  37. this.subscribe(this.plugin.canvas3d!.reprCount, this.handleLogo);
  38. }
  39. componentWillUnmount() {
  40. super.componentWillUnmount();
  41. // TODO viewer cleanup
  42. }
  43. renderMissing() {
  44. if (this.props.noWebGl) {
  45. const C = this.props.noWebGl;
  46. return <C />;
  47. }
  48. return <div className='msp-no-webgl'>
  49. <div>
  50. <p><b>WebGL does not seem to be available.</b></p>
  51. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps. Also, make sure hardware acceleration is enabled in your browser.</p>
  52. <p>For a list of supported browsers, refer to <a href='http://caniuse.com/#feat=webgl' target='_blank'>http://caniuse.com/#feat=webgl</a>.</p>
  53. </div>
  54. </div>;
  55. }
  56. render() {
  57. if (this.state.noWebGl) return this.renderMissing();
  58. const Logo = this.props.logo;
  59. return <div className={this.props.parentClassName || 'msp-viewport'} style={this.props.parentStyle}>
  60. <div className={this.props.hostClassName || 'msp-viewport-host3d'} style={this.props.hostStyle} ref={this.container}>
  61. <canvas ref={this.canvas} />
  62. </div>
  63. {(this.state.showLogo && Logo) && <Logo />}
  64. </div>;
  65. }
  66. }