|
@@ -15,25 +15,47 @@ import { download } from '../../mol-util/download';
|
|
|
import { ParamDefinition as PD } from '../../mol-util/param-definition';
|
|
|
import { SyncRuntimeContext } from '../../mol-task/execution/synchronous';
|
|
|
|
|
|
-export class ViewportScreenshotWrapper {
|
|
|
- get params() {
|
|
|
+export { ViewportScreenshotHelper }
|
|
|
+
|
|
|
+namespace ViewportScreenshotHelper {
|
|
|
+ export type ResolutionSettings = PD.Values<ReturnType<ViewportScreenshotHelper['createParams']>>['resolution']
|
|
|
+ export type ResolutionTypes = ResolutionSettings['name']
|
|
|
+}
|
|
|
+
|
|
|
+class ViewportScreenshotHelper {
|
|
|
+ private createParams() {
|
|
|
const max = Math.min(this.plugin.canvas3d ? this.plugin.canvas3d.webgl.maxRenderbufferSize : 4096, 4096)
|
|
|
- const { width, height } = this.size
|
|
|
return {
|
|
|
- size: PD.MappedStatic(this.size.type, {
|
|
|
- canvas: PD.Group({}),
|
|
|
+ resolution: PD.MappedStatic('full-hd', {
|
|
|
+ viewport: PD.Group({}),
|
|
|
+ hd: PD.Group({}),
|
|
|
+ 'full-hd': PD.Group({}),
|
|
|
+ 'ultra-hd': PD.Group({}),
|
|
|
custom: PD.Group({
|
|
|
- width: PD.Numeric(width, { min: 128, max, step: 1 }),
|
|
|
- height: PD.Numeric(height, { min: 128, max, step: 1 }),
|
|
|
+ width: PD.Numeric(1920, { min: 128, max, step: 1 }),
|
|
|
+ height: PD.Numeric(1080, { min: 128, max, step: 1 }),
|
|
|
}, { isFlat: true })
|
|
|
- }, { options: [['canvas', 'Canvas'], ['custom', 'Custom']] })
|
|
|
+ }, {
|
|
|
+ options: [
|
|
|
+ ['viewport', 'Viewport'],
|
|
|
+ ['hd', 'HD (1280 x 720)'],
|
|
|
+ ['full-hd', 'Full HD (1920 x 1080)'],
|
|
|
+ ['ultra-hd', 'Ultra HD (3840 x 2160)'],
|
|
|
+ ['custom', 'Custom']
|
|
|
+ ]
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
+ private _params: ReturnType<ViewportScreenshotHelper['createParams']> = void 0 as any;
|
|
|
+ get params() {
|
|
|
+ if (this._params) return this._params;
|
|
|
+ return this._params = this.createParams();
|
|
|
+ }
|
|
|
|
|
|
get values() {
|
|
|
- return this.size.type === 'canvas'
|
|
|
- ? { size: { name: 'canvas', params: {} } }
|
|
|
- : { size: { name: 'custom', params: { width: this.size.width, height: this.size.height } } }
|
|
|
+ return this.currentResolution.type === 'custom'
|
|
|
+ ? { resolution: { name: 'custom', params: { width: this.currentResolution.width, height: this.currentResolution.height } } }
|
|
|
+ : { resolution: { name: this.currentResolution.type, params: { } } };
|
|
|
}
|
|
|
|
|
|
private getCanvasSize() {
|
|
@@ -43,15 +65,20 @@ export class ViewportScreenshotWrapper {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- size = {
|
|
|
- type: 'custom' as 'canvas' | 'custom',
|
|
|
+ currentResolution = {
|
|
|
+ type: 'full-hd' as ViewportScreenshotHelper.ResolutionTypes,
|
|
|
width: 1920,
|
|
|
height: 1080
|
|
|
- }
|
|
|
-
|
|
|
- getSize() {
|
|
|
- if (this.size.type === 'canvas') return this.getCanvasSize();
|
|
|
- return { width: this.size.width, height: this.size.height };
|
|
|
+ };
|
|
|
+
|
|
|
+ private getSize() {
|
|
|
+ switch (this.currentResolution.type ) {
|
|
|
+ case 'viewport': return this.getCanvasSize();
|
|
|
+ case 'hd': return { width: 1280, height: 720 };
|
|
|
+ case 'full-hd': return { width: 1920, height: 1080 };
|
|
|
+ case 'ultra-hd': return { width: 3840, height: 2160 };
|
|
|
+ default: return { width: this.currentResolution.width, height: this.currentResolution.height }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private _imagePass: ImagePass;
|
|
@@ -80,6 +107,28 @@ export class ViewportScreenshotWrapper {
|
|
|
return canvas;
|
|
|
}();
|
|
|
|
|
|
+ // private preview = () => {
|
|
|
+ // const { width, height } = this.getSize()
|
|
|
+ // if (width <= 0 || height <= 0) return
|
|
|
+
|
|
|
+ // let w: number, h: number
|
|
|
+ // const aH = maxHeightUi / height
|
|
|
+ // const aW = maxWidthUi / width
|
|
|
+ // if (aH < aW) {
|
|
|
+ // h = Math.round(Math.min(maxHeightUi, height))
|
|
|
+ // w = Math.round(width * (h / height))
|
|
|
+ // } else {
|
|
|
+ // w = Math.round(Math.min(maxWidthUi, width))
|
|
|
+ // h = Math.round(height * (w / width))
|
|
|
+ // }
|
|
|
+ // setCanvasSize(this.canvas, w, h)
|
|
|
+ // const pixelRatio = this.plugin.canvas3d?.webgl.pixelRatio || 1
|
|
|
+ // const pw = Math.round(w * pixelRatio)
|
|
|
+ // const ph = Math.round(h * pixelRatio)
|
|
|
+ // const imageData = this.imagePass.getImageData(pw, ph)
|
|
|
+ // this.canvasContext.putImageData(imageData, 0, 0)
|
|
|
+ // }
|
|
|
+
|
|
|
private async draw(ctx: RuntimeContext) {
|
|
|
const { width, height } = this.getSize();
|
|
|
if (width <= 0 || height <= 0) return;
|
|
@@ -106,6 +155,14 @@ export class ViewportScreenshotWrapper {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ downloadCurrent() {
|
|
|
+ return this.plugin.runTask(Task.create('Download Image', async ctx => {
|
|
|
+ await ctx.update('Downloading image...')
|
|
|
+ const blob = await canvasToBlob(this.canvas, 'png')
|
|
|
+ download(blob, this.getFilename())
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
async imageData() {
|
|
|
await this.draw(SyncRuntimeContext)
|
|
|
return this.canvas.toDataURL();
|