Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

canvas-source.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2025 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * canvas-source.ts
  20. * HTMLCanvasElement-based source of data
  21. */
  22. import Speedy from 'speedy-vision';
  23. import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
  24. import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
  25. import { Utils, Nullable } from '../utils/utils';
  26. import { IllegalOperationError } from '../utils/errors';
  27. import { Source, SourceType } from './source';
  28. /**
  29. * HTMLCanvasElement-based source of data
  30. */
  31. export class CanvasSource implements Source
  32. {
  33. /** canvas element */
  34. private _canvas: HTMLCanvasElement;
  35. /** media source */
  36. protected _media: Nullable<SpeedyMedia>;
  37. /**
  38. * Constructor
  39. */
  40. constructor(canvas: HTMLCanvasElement)
  41. {
  42. Utils.assert(canvas instanceof HTMLCanvasElement, 'Expected a canvas element');
  43. this._canvas = canvas;
  44. this._media = null;
  45. }
  46. /**
  47. * The underlying <canvas> element
  48. */
  49. get canvas(): HTMLCanvasElement
  50. {
  51. return this._canvas;
  52. }
  53. /**
  54. * A type-identifier of the source of data
  55. * @internal
  56. */
  57. get _type(): keyof SourceType
  58. {
  59. return 'canvas';
  60. }
  61. /**
  62. * Check if this source is of a certain type
  63. * @internal
  64. */
  65. _is<T extends keyof SourceType>(type: T): this is SourceType[T]
  66. {
  67. return type === this._type;
  68. }
  69. /**
  70. * Get media
  71. * @internal
  72. */
  73. get _internalMedia(): SpeedyMedia
  74. {
  75. if(this._media == null)
  76. throw new IllegalOperationError(`The media of the source of data isn't loaded`);
  77. return this._media;
  78. }
  79. /**
  80. * Stats related to this source of data
  81. * @internal
  82. */
  83. get _stats(): string
  84. {
  85. const media = this._media;
  86. if(media != null)
  87. return `${media.width}x${media.height} canvas`;
  88. else
  89. return 'uninitialized canvas';
  90. }
  91. /**
  92. * Initialize this source of data
  93. * @returns a promise that resolves as soon as this source of data is initialized
  94. * @internal
  95. */
  96. _init(): SpeedyPromise<void>
  97. {
  98. return Speedy.load(this._canvas).then(media => {
  99. Utils.log(`Source of data is a ${media.width}x${media.height} ${this._type}`);
  100. this._media = media;
  101. });
  102. }
  103. /**
  104. * Release this source of data
  105. * @returns a promise that resolves as soon as this source of data is released
  106. * @internal
  107. */
  108. _release(): SpeedyPromise<void>
  109. {
  110. if(this._media)
  111. this._media.release();
  112. this._media = null;
  113. return Speedy.Promise.resolve();
  114. }
  115. }