screenshot.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2019 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 { ParamDefinition as PD } from '../../mol-util/param-definition';
  9. import { ParameterControls } from '../controls/parameters';
  10. import { PluginUIComponent } from '../base';
  11. import { Icon } from '../controls/icons';
  12. import { debounceTime } from 'rxjs/operators';
  13. import { Subject } from 'rxjs';
  14. import { ViewportScreenshotHelper } from '../../mol-plugin/util/viewport-screenshot';
  15. interface ImageControlsState {
  16. showPreview: boolean
  17. resolution?: ViewportScreenshotHelper.ResolutionSettings,
  18. transparent?: boolean,
  19. isDisabled: boolean
  20. }
  21. export class DownloadScreenshotControls extends PluginUIComponent<{ close: () => void }, ImageControlsState> {
  22. state: ImageControlsState = {
  23. showPreview: true,
  24. resolution: this.plugin.helpers.viewportScreenshot?.currentResolution,
  25. transparent: this.plugin.helpers.viewportScreenshot?.transparent,
  26. isDisabled: false
  27. } as ImageControlsState
  28. private imgRef = React.createRef<HTMLImageElement>()
  29. private updateQueue = new Subject();
  30. private preview = async () => {
  31. if (!this.imgRef.current) return;
  32. this.imgRef.current!.src = await this.plugin.helpers.viewportScreenshot!.imageData();
  33. }
  34. private download = () => {
  35. this.plugin.helpers.viewportScreenshot?.download();
  36. this.props.close();
  37. }
  38. private openTab = () => {
  39. // modified from https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript/16245768#16245768
  40. const base64 = this.imgRef.current!.src;
  41. const byteCharacters = atob(base64.substr(`data:image/png;base64,`.length));
  42. const byteArrays = [];
  43. const sliceSize = Math.min(byteCharacters.length, 1024 * 1024);
  44. for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
  45. const byteNumbers = new Uint8Array(Math.min(sliceSize, byteCharacters.length - offset));
  46. for (let i = 0, _i = byteNumbers.length; i < _i; i++) {
  47. byteNumbers[i] = byteCharacters.charCodeAt(offset + i);
  48. }
  49. byteArrays.push(byteNumbers);
  50. }
  51. const blob = new Blob(byteArrays, { type: 'image/png' });
  52. const blobUrl = URL.createObjectURL(blob);
  53. window.open(blobUrl, '_blank');
  54. this.props.close();
  55. }
  56. private handlePreview() {
  57. if (this.state.showPreview) {
  58. this.preview()
  59. }
  60. }
  61. componentDidUpdate() {
  62. this.updateQueue.next();
  63. }
  64. componentDidMount() {
  65. if (!this.plugin.canvas3d) return;
  66. this.subscribe(debounceTime(250)(this.updateQueue), () => this.handlePreview());
  67. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => {
  68. this.plugin.helpers.viewportScreenshot!.imagePass.setProps({
  69. multiSample: { mode: 'on', sampleLevel: 2 },
  70. postprocessing: this.plugin.canvas3d?.props.postprocessing
  71. })
  72. this.updateQueue.next();
  73. })
  74. this.subscribe(debounceTime(250)(this.plugin.canvas3d.didDraw), () => {
  75. if (this.state.isDisabled) return;
  76. this.updateQueue.next();
  77. })
  78. this.subscribe(this.plugin.state.data.events.isUpdating, v => {
  79. this.setState({ isDisabled: v })
  80. if (!v) this.updateQueue.next();
  81. })
  82. this.handlePreview();
  83. }
  84. private setProps = (p: { param: PD.Base<any>, name: string, value: any }) => {
  85. if (p.name === 'resolution') {
  86. const resolution = p.value as ViewportScreenshotHelper.ResolutionSettings
  87. if (resolution.name === 'custom') {
  88. this.plugin.helpers.viewportScreenshot!.currentResolution.type = 'custom';
  89. this.plugin.helpers.viewportScreenshot!.currentResolution.width = resolution.params.width;
  90. this.plugin.helpers.viewportScreenshot!.currentResolution.height = resolution.params.height;
  91. } else {
  92. this.plugin.helpers.viewportScreenshot!.currentResolution.type = resolution.name;
  93. }
  94. this.setState({ resolution });
  95. } else if (p.name === 'transparent') {
  96. this.plugin.helpers.viewportScreenshot!.transparent = p.value;
  97. this.setState({ transparent: p.value });
  98. }
  99. }
  100. render() {
  101. return <div>
  102. <div className='msp-image-preview'>
  103. <img ref={this.imgRef} /><br />
  104. <span>Right-click the image to Copy.</span>
  105. </div>
  106. <div className='msp-btn-row-group'>
  107. <button className='msp-btn msp-btn-block msp-form-control' onClick={this.download} disabled={this.state.isDisabled}><Icon name='download' /> Download</button>
  108. <button className='msp-btn msp-btn-block msp-form-control' onClick={this.openTab} disabled={this.state.isDisabled}><Icon name='export' /> Open in new Tab</button>
  109. </div>
  110. <ParameterControls params={this.plugin.helpers.viewportScreenshot!.params} values={this.plugin.helpers.viewportScreenshot!.values} onChange={this.setProps} isDisabled={this.state.isDisabled} />
  111. </div>
  112. }
  113. }