viewport.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import * as React from 'react';
  8. import { PluginContext } from '../context';
  9. import { Loci, EmptyLoci, areLociEqual } from 'mol-model/loci';
  10. import { MarkerAction } from 'mol-geo/geometry/marker-data';
  11. interface ViewportProps {
  12. plugin: PluginContext
  13. }
  14. interface ViewportState {
  15. noWebGl: boolean
  16. }
  17. export class Viewport extends React.Component<ViewportProps, ViewportState> {
  18. private container: HTMLDivElement | null = null;
  19. private canvas: HTMLCanvasElement | null = null;
  20. state: ViewportState = {
  21. noWebGl: false
  22. };
  23. handleResize() {
  24. this.props.plugin.viewer.handleResize();
  25. }
  26. componentDidMount() {
  27. if (!this.canvas || !this.container || !this.props.plugin.initViewer(this.canvas, this.container)) {
  28. this.setState({ noWebGl: true });
  29. }
  30. this.handleResize();
  31. const viewer = this.props.plugin.viewer;
  32. viewer.input.resize.subscribe(() => this.handleResize());
  33. let prevLoci: Loci = EmptyLoci;
  34. viewer.input.move.subscribe(({x, y, inside, buttons}) => {
  35. if (!inside || buttons) return;
  36. const p = viewer.identify(x, y);
  37. if (p) {
  38. const loci = viewer.getLoci(p);
  39. if (!areLociEqual(loci, prevLoci)) {
  40. viewer.mark(prevLoci, MarkerAction.RemoveHighlight);
  41. viewer.mark(loci, MarkerAction.Highlight);
  42. prevLoci = loci;
  43. }
  44. }
  45. })
  46. }
  47. componentWillUnmount() {
  48. if (super.componentWillUnmount) super.componentWillUnmount();
  49. // TODO viewer cleanup
  50. }
  51. renderMissing() {
  52. return <div>
  53. <div>
  54. <p><b>WebGL does not seem to be available.</b></p>
  55. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  56. <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>
  57. </div>
  58. </div>
  59. }
  60. render() {
  61. if (this.state.noWebGl) return this.renderMissing();
  62. return <div style={{ backgroundColor: 'rgb(0, 0, 0)', width: '100%', height: '100%'}}>
  63. <div ref={elm => this.container = elm} style={{width: '100%', height: '100%'}}>
  64. <canvas ref={elm => this.canvas = elm}></canvas>
  65. </div>
  66. </div>;
  67. }
  68. }