瀏覽代碼

Improve the code

customisations
alemart 10 月之前
父節點
當前提交
97bb1d4525
共有 2 個文件被更改,包括 41 次插入22 次删除
  1. 40
    21
      src/core/session.ts
  2. 1
    1
      src/ui/gizmos.ts

+ 40
- 21
src/core/session.ts 查看文件

22
 
22
 
23
 import Speedy from 'speedy-vision';
23
 import Speedy from 'speedy-vision';
24
 import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
24
 import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
25
+import { SpeedyMediaSourceNativeElement } from 'speedy-vision/types/core/speedy-media-source';
25
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26
 import { Nullable, Utils } from '../utils/utils';
27
 import { Nullable, Utils } from '../utils/utils';
27
 import { AREvent, AREventTarget } from '../utils/ar-events';
28
 import { AREvent, AREventTarget } from '../utils/ar-events';
525
     }
526
     }
526
 
527
 
527
     /**
528
     /**
528
-     * Render the user media to the background canvas
529
+     * Render content to the background canvas
529
      */
530
      */
530
-    private _renderUserMedia(): void
531
+    private _renderBackground(): void
531
     {
532
     {
532
-        const media = this._primarySource?._internalMedia;
533
         const canvas = this._viewport._backgroundCanvas;
533
         const canvas = this._viewport._backgroundCanvas;
534
         const ctx = canvas.getContext('2d', { alpha: false });
534
         const ctx = canvas.getContext('2d', { alpha: false });
535
 
535
 
536
-        if(ctx && media && media.type != 'data') {
537
-            ctx.imageSmoothingEnabled = false;
536
+        // error?
537
+        if(!ctx)
538
+            return;
539
+        ctx.imageSmoothingEnabled = false;
538
 
540
 
539
-            // draw user media
540
-            const image = media.source as CanvasImageSource;
541
-            ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
541
+        // render user media
542
+        if(this._primarySource !== null) {
543
+            const media = this._primarySource._internalMedia;
544
+            this._renderMedia(ctx, media);
545
+        }
542
 
546
 
543
-            // render output image(s)
544
-            for(let i = 0; i < this._trackers.length; i++) {
545
-                const media = this._trackers[i]._output.image;
546
-                if(media !== undefined) {
547
-                    const image = media.source as CanvasImageSource;
548
-                    ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
549
-                    //ctx.drawImage(image, canvas.width - media.width, canvas.height - media.height, media.width, media.height);
550
-                }
551
-            }
547
+        // render output image(s)
548
+        for(let i = 0; i < this._trackers.length; i++) {
549
+            const media = this._trackers[i]._output.image;
550
+            if(media !== undefined)
551
+                this._renderMedia(ctx, media);
552
+        }
553
+
554
+        // render gizmos
555
+        this._gizmos._render(this._viewport, this._trackers);
556
+    }
552
 
557
 
553
-            // render gizmos
554
-            this._gizmos._render(this._viewport, this._trackers);
558
+    /**
559
+     * Render a SpeedyMedia
560
+     * @param ctx rendering context
561
+     * @param media
562
+     */
563
+    private _renderMedia(ctx: CanvasRenderingContext2D, media: SpeedyMedia): void
564
+    {
565
+        const canvas = ctx.canvas;
566
+
567
+        if(media.type != 'data') {
568
+            const image = media.source as Exclude<SpeedyMediaSourceNativeElement, ImageData>;
569
+            ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
570
+        }
571
+        else {
572
+            const image = media.source as ImageData;
573
+            ctx.putImageData(image, 0, 0, 0, 0, canvas.width, canvas.height);
555
         }
574
         }
556
     }
575
     }
557
 
576
 
676
                 const rafQueue = this._rafQueue.slice(0);
695
                 const rafQueue = this._rafQueue.slice(0);
677
                 this._rafQueue.length = 0;
696
                 this._rafQueue.length = 0;
678
 
697
 
679
-                // render user media
698
+                // render content to the background canvas
680
                 if(!skipUserMedia)
699
                 if(!skipUserMedia)
681
-                    this._renderUserMedia();
700
+                    this._renderBackground();
682
 
701
 
683
                 // render frame
702
                 // render frame
684
                 for(let i = 0; i < rafQueue.length; i++)
703
                 for(let i = 0; i < rafQueue.length; i++)

+ 1
- 1
src/ui/gizmos.ts 查看文件

88
         const canvas = viewport._backgroundCanvas;
88
         const canvas = viewport._backgroundCanvas;
89
         const ctx = canvas.getContext('2d', { alpha: false });
89
         const ctx = canvas.getContext('2d', { alpha: false });
90
         if(!ctx)
90
         if(!ctx)
91
-            throw new IllegalOperationError();
91
+            return;
92
 
92
 
93
         // debug
93
         // debug
94
         //ctx.fillStyle = '#000';
94
         //ctx.fillStyle = '#000';

Loading…
取消
儲存