|
| 1 | +import { AABaseElement, html, type AAPropertiesMap } from '../aa-base-element/aa-base-element'; |
| 2 | + |
| 3 | +export class AACamera extends AABaseElement { |
| 4 | + |
| 5 | + static get category(): string { |
| 6 | + return "response item"; |
| 7 | + } |
| 8 | + |
| 9 | + static get tag(): string { |
| 10 | + return 'aa-camera'; |
| 11 | + } |
| 12 | + |
| 13 | + static get properties(): AAPropertiesMap { |
| 14 | + return { |
| 15 | + name: { |
| 16 | + type: String, |
| 17 | + userDefined: true |
| 18 | + }, |
| 19 | + 'facing': { |
| 20 | + type: String, |
| 21 | + userDefined: true, |
| 22 | + value: "environment", |
| 23 | + valuesAllowed: ["environment", "user"] |
| 24 | + }, |
| 25 | + 'value': { |
| 26 | + type: String, |
| 27 | + userDefined: false |
| 28 | + }, |
| 29 | + 'width': { |
| 30 | + type: Number, |
| 31 | + userDefined: true, |
| 32 | + value: 640 |
| 33 | + }, |
| 34 | + 'height': { |
| 35 | + type: Number, |
| 36 | + userDefined: true, |
| 37 | + value: 480 |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static get acceptsElements(): null { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + static get observedAttributes(): string[] { |
| 47 | + return Object.keys(AACamera.properties); |
| 48 | + } |
| 49 | + |
| 50 | + root: ShadowRoot; |
| 51 | + _stream: MediaStream | null = null; |
| 52 | + _videoEl: HTMLVideoElement | null = null; |
| 53 | + _canvasEl: HTMLCanvasElement | null = null; |
| 54 | + _imgEl: HTMLImageElement | null = null; |
| 55 | + _captureBtn: HTMLButtonElement | null = null; |
| 56 | + _retakeBtn: HTMLButtonElement | null = null; |
| 57 | + _flipBtn: HTMLButtonElement | null = null; |
| 58 | + |
| 59 | + get value(): string | null { |
| 60 | + return this.getAttribute('value'); |
| 61 | + } |
| 62 | + |
| 63 | + set value(val: string | null) { |
| 64 | + if (val) { |
| 65 | + this.setAttribute('value', val); |
| 66 | + } else { |
| 67 | + this.removeAttribute('value'); |
| 68 | + } |
| 69 | + let memory = this.getMemory(); |
| 70 | + if (memory) memory.setData(AABaseElement.getVariableName(this), val); |
| 71 | + } |
| 72 | + |
| 73 | + constructor() { |
| 74 | + super(); |
| 75 | + this.root = this.attachShadow({ mode: 'open' }); |
| 76 | + } |
| 77 | + |
| 78 | + connectedCallback(): void { |
| 79 | + super.connectedCallback(); |
| 80 | + this.root.innerHTML = this.css + this.htmlTemplate; |
| 81 | + |
| 82 | + this._videoEl = this.root.querySelector('.preview') as HTMLVideoElement; |
| 83 | + this._canvasEl = this.root.querySelector('.canvas') as HTMLCanvasElement; |
| 84 | + this._imgEl = this.root.querySelector('.captured') as HTMLImageElement; |
| 85 | + this._captureBtn = this.root.querySelector('.capture-btn') as HTMLButtonElement; |
| 86 | + this._retakeBtn = this.root.querySelector('.retake-btn') as HTMLButtonElement; |
| 87 | + this._flipBtn = this.root.querySelector('.flip-btn') as HTMLButtonElement; |
| 88 | + |
| 89 | + this._captureBtn.addEventListener('click', () => this._capture()); |
| 90 | + this._retakeBtn.addEventListener('click', () => this._retake()); |
| 91 | + this._flipBtn.addEventListener('click', () => this._flipCamera()); |
| 92 | + |
| 93 | + this._startCamera(); |
| 94 | + } |
| 95 | + |
| 96 | + disconnectedCallback(): void { |
| 97 | + this._stopStream(); |
| 98 | + } |
| 99 | + |
| 100 | + async _startCamera(): Promise<void> { |
| 101 | + this._stopStream(); |
| 102 | + try { |
| 103 | + const facingMode = this.getAttribute('facing') || 'environment'; |
| 104 | + this._stream = await navigator.mediaDevices.getUserMedia({ |
| 105 | + video: { facingMode: facingMode } |
| 106 | + }); |
| 107 | + if (this._videoEl) { |
| 108 | + this._videoEl.srcObject = this._stream; |
| 109 | + } |
| 110 | + } catch (err) { |
| 111 | + console.error('aa-camera: could not access camera', err); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + _stopStream(): void { |
| 116 | + if (this._stream) { |
| 117 | + this._stream.getTracks().forEach(track => track.stop()); |
| 118 | + this._stream = null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + _capture(): void { |
| 123 | + if (!this._videoEl || !this._canvasEl || !this._imgEl) return; |
| 124 | + |
| 125 | + const w = Number(this.getAttribute('width')) || 640; |
| 126 | + const h = Number(this.getAttribute('height')) || 480; |
| 127 | + this._canvasEl.width = w; |
| 128 | + this._canvasEl.height = h; |
| 129 | + |
| 130 | + const ctx = this._canvasEl.getContext('2d'); |
| 131 | + if (!ctx) return; |
| 132 | + ctx.drawImage(this._videoEl, 0, 0, w, h); |
| 133 | + |
| 134 | + const dataUrl = this._canvasEl.toDataURL('image/jpeg'); |
| 135 | + this.value = dataUrl; |
| 136 | + |
| 137 | + this._imgEl.src = dataUrl; |
| 138 | + this._imgEl.style.display = 'block'; |
| 139 | + this._videoEl.style.display = 'none'; |
| 140 | + |
| 141 | + if (this._captureBtn) this._captureBtn.style.display = 'none'; |
| 142 | + if (this._flipBtn) this._flipBtn.style.display = 'none'; |
| 143 | + if (this._retakeBtn) this._retakeBtn.style.display = 'inline-block'; |
| 144 | + |
| 145 | + this.dispatchEvent(new CustomEvent('change')); |
| 146 | + } |
| 147 | + |
| 148 | + _retake(): void { |
| 149 | + this.value = null; |
| 150 | + |
| 151 | + if (this._imgEl) { |
| 152 | + this._imgEl.style.display = 'none'; |
| 153 | + this._imgEl.src = ''; |
| 154 | + } |
| 155 | + if (this._videoEl) { |
| 156 | + this._videoEl.style.display = 'block'; |
| 157 | + } |
| 158 | + if (this._captureBtn) this._captureBtn.style.display = 'inline-block'; |
| 159 | + if (this._flipBtn) this._flipBtn.style.display = 'inline-block'; |
| 160 | + if (this._retakeBtn) this._retakeBtn.style.display = 'none'; |
| 161 | + |
| 162 | + this._startCamera(); |
| 163 | + } |
| 164 | + |
| 165 | + _flipCamera(): void { |
| 166 | + const current = this.getAttribute('facing') || 'environment'; |
| 167 | + const next = current === 'environment' ? 'user' : 'environment'; |
| 168 | + this.setAttribute('facing', next); |
| 169 | + this._startCamera(); |
| 170 | + } |
| 171 | + |
| 172 | + get css(): string { |
| 173 | + return html`<style> |
| 174 | + :host { |
| 175 | + display: block; |
| 176 | + overflow: hidden; |
| 177 | + } |
| 178 | + .container { |
| 179 | + display: flex; |
| 180 | + flex-direction: column; |
| 181 | + align-items: center; |
| 182 | + gap: 12px; |
| 183 | + } |
| 184 | + .preview, .captured { |
| 185 | + width: 100%; |
| 186 | + max-width: 640px; |
| 187 | + border-radius: 8px; |
| 188 | + background: #000; |
| 189 | + object-fit: cover; |
| 190 | + } |
| 191 | + .canvas { |
| 192 | + display: none; |
| 193 | + } |
| 194 | + .captured { |
| 195 | + display: none; |
| 196 | + } |
| 197 | + .controls { |
| 198 | + display: flex; |
| 199 | + gap: 12px; |
| 200 | + justify-content: center; |
| 201 | + flex-wrap: wrap; |
| 202 | + } |
| 203 | + .controls button { |
| 204 | + font-family: sans-serif; |
| 205 | + font-size: 16px; |
| 206 | + padding: 10px 20px; |
| 207 | + border: 1px solid #ccc; |
| 208 | + border-radius: 6px; |
| 209 | + background: #fff; |
| 210 | + cursor: pointer; |
| 211 | + transition: background 0.15s; |
| 212 | + } |
| 213 | + .controls button:hover { |
| 214 | + background: #f0f0f0; |
| 215 | + } |
| 216 | + .capture-btn { |
| 217 | + background: #4CAF50 !important; |
| 218 | + color: #fff; |
| 219 | + border-color: #4CAF50 !important; |
| 220 | + } |
| 221 | + .capture-btn:hover { |
| 222 | + background: #43A047 !important; |
| 223 | + } |
| 224 | + </style>`; |
| 225 | + } |
| 226 | + |
| 227 | + get htmlTemplate(): string { |
| 228 | + return html` |
| 229 | + <div class="container"> |
| 230 | + <video class="preview" autoplay playsinline></video> |
| 231 | + <canvas class="canvas"></canvas> |
| 232 | + <img class="captured" alt="Captured photo"> |
| 233 | + <div class="controls"> |
| 234 | + <button class="flip-btn" type="button">\u27F2 Flip</button> |
| 235 | + <button class="capture-btn" type="button">\uD83D\uDCF7 Capture</button> |
| 236 | + <button class="retake-btn" type="button" style="display:none">\u21BA Retake</button> |
| 237 | + </div> |
| 238 | + </div>`; |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +AABaseElement.registerAAElement('aa-camera', AACamera); |
0 commit comments