canvasframework 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,536 @@
1
+ import Component from '../core/Component.js';
2
+
3
+ /**
4
+ * Composant QR Code Reader autonome
5
+ * Analyse en temps réel le flux vidéo pour détecter les QR codes
6
+ */
7
+ class QRCodeReader extends Component {
8
+ constructor(framework, options = {}) {
9
+ super(framework, options);
10
+
11
+ this.facingMode = options.facingMode || 'environment';
12
+ this.autoStart = options.autoStart !== false;
13
+ this.onQRCodeDetected = options.onQRCodeDetected || null; // Callback quand un QR code est détecté
14
+ this.onError = options.onError || null;
15
+
16
+ // Options de scan
17
+ this.scanInterval = options.scanInterval || 300; // ms entre chaque analyse
18
+ this.continuous = options.continuous !== false; // Continue à scanner même après détection
19
+ this.vibrateOnDetect = options.vibrateOnDetect !== false; // Vibration mobile
20
+
21
+ this.stream = null;
22
+ this.video = null;
23
+ this.loaded = false;
24
+ this.error = null;
25
+
26
+ // État du scan
27
+ this.isScanning = false;
28
+ this.scanTimer = null;
29
+ this.lastScanTime = 0;
30
+ this.currentQRCode = null;
31
+ this.scanHistory = []; // Historique des codes scannés
32
+
33
+ // UI
34
+ this.showControls = true;
35
+ this.showScannerOverlay = true;
36
+ this.scannerFrameSize = 250; // Taille du cadre de scan
37
+ this.scannerFrameColor = '#00ff00';
38
+ this.scannerLineColor = '#ff0000';
39
+ this.scannerLineHeight = 2;
40
+ this.scannerLineSpeed = 2;
41
+ this.scannerLinePosition = 0;
42
+ this.scannerLineDirection = 1;
43
+
44
+ this.torchSupported = false;
45
+ this.torchOn = false;
46
+
47
+ this.isStarting = false;
48
+
49
+ // Charger la librairie QR code (jsQR)
50
+ this.loadQRScanner();
51
+ }
52
+
53
+ async _mount() {
54
+ super._mount?.();
55
+
56
+ if (this.autoStart && !this.stream && !this.isStarting) {
57
+ this.isStarting = true;
58
+ await this.startCamera();
59
+ this.isStarting = false;
60
+ }
61
+
62
+ this.setupEventListeners();
63
+ }
64
+
65
+ destroy() {
66
+ this.stopScanning();
67
+ this.removeEventListeners();
68
+ this.stopCamera();
69
+ super.destroy?.();
70
+ }
71
+
72
+ // Charger jsQR depuis CDN
73
+ loadQRScanner() {
74
+ if (typeof jsQR === 'undefined') {
75
+ const script = document.createElement('script');
76
+ script.src = 'https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.min.js';
77
+ script.onload = () => {
78
+ console.log('jsQR loaded');
79
+ if (this.loaded && !this.isScanning) {
80
+ this.startScanning();
81
+ }
82
+ };
83
+ script.onerror = () => {
84
+ console.error('Failed to load jsQR');
85
+ this.error = 'Échec du chargement du scanner QR';
86
+ };
87
+ document.head.appendChild(script);
88
+ }
89
+ }
90
+
91
+ setupEventListeners() {
92
+ this.onTouchStart = this.handleTouchStart.bind(this);
93
+ this.onMouseDown = this.handleMouseDown.bind(this);
94
+
95
+ const canvas = this.framework.canvas;
96
+ canvas.addEventListener('touchstart', this.onTouchStart, { passive: false });
97
+ canvas.addEventListener('mousedown', this.onMouseDown);
98
+ }
99
+
100
+ removeEventListeners() {
101
+ const canvas = this.framework.canvas;
102
+ canvas.removeEventListener('touchstart', this.onTouchStart);
103
+ canvas.removeEventListener('mousedown', this.onMouseDown);
104
+ }
105
+
106
+ getLocalPos(clientX, clientY) {
107
+ const rect = this.framework.canvas.getBoundingClientRect();
108
+ const globalX = clientX - rect.left;
109
+ const globalY = clientY - rect.top - this.framework.scrollOffset;
110
+ return {
111
+ x: globalX - this.x,
112
+ y: globalY - this.y
113
+ };
114
+ }
115
+
116
+ handleTouchStart(e) {
117
+ e.preventDefault();
118
+ const touch = e.touches[0];
119
+ const pos = this.getLocalPos(touch.clientX, touch.clientY);
120
+ this.handlePress(pos.x, pos.y);
121
+ }
122
+
123
+ handleMouseDown(e) {
124
+ const pos = this.getLocalPos(e.clientX, e.clientY);
125
+ this.handlePress(pos.x, pos.y);
126
+ }
127
+
128
+ async startCamera() {
129
+ if (this.stream) return;
130
+
131
+ try {
132
+ this.stopCamera();
133
+
134
+ this.stream = await navigator.mediaDevices.getUserMedia({
135
+ video: {
136
+ facingMode: this.facingMode,
137
+ width: { ideal: 1280 },
138
+ height: { ideal: 720 },
139
+ frameRate: { ideal: 30 }
140
+ }
141
+ });
142
+
143
+ const [track] = this.stream.getVideoTracks();
144
+ const caps = track.getCapabilities?.() || {};
145
+ this.torchSupported = !!caps.torch;
146
+
147
+ this.video = document.createElement('video');
148
+ this.video.autoplay = true;
149
+ this.video.playsInline = true;
150
+ this.video.muted = true;
151
+ this.video.srcObject = this.stream;
152
+
153
+ this.video.style.position = 'fixed';
154
+ this.video.style.left = '-9999px';
155
+ this.video.style.top = '-9999px';
156
+ this.video.style.width = '1px';
157
+ this.video.style.height = '1px';
158
+ document.body.appendChild(this.video);
159
+
160
+ await new Promise(resolve => {
161
+ this.video.onloadedmetadata = () => {
162
+ this.loaded = true;
163
+ this.video.play().then(() => {
164
+ if (typeof jsQR !== 'undefined') {
165
+ this.startScanning();
166
+ }
167
+ resolve();
168
+ }).catch(e => {
169
+ console.warn('Play auto échoué:', e);
170
+ resolve();
171
+ });
172
+ };
173
+ this.video.onerror = (e) => {
174
+ this.error = 'Erreur vidéo';
175
+ console.error('Video error:', e);
176
+ resolve();
177
+ };
178
+ });
179
+
180
+ this.markDirty();
181
+ } catch (err) {
182
+ this.error = err.message || 'Accès caméra refusé';
183
+ console.error('Échec getUserMedia:', err);
184
+ if (this.onError) this.onError(this.error);
185
+ this.markDirty();
186
+ }
187
+ }
188
+
189
+ stopCamera() {
190
+ this.stopScanning();
191
+ if (this.stream) {
192
+ this.stream.getTracks().forEach(track => track.stop());
193
+ this.stream = null;
194
+ }
195
+ if (this.video) {
196
+ if (this.video.parentNode) {
197
+ this.video.parentNode.removeChild(this.video);
198
+ }
199
+ this.video.srcObject = null;
200
+ this.video = null;
201
+ }
202
+ this.loaded = false;
203
+ this.markDirty();
204
+ }
205
+
206
+ startScanning() {
207
+ if (this.isScanning || !this.loaded || !this.video) return;
208
+
209
+ this.isScanning = true;
210
+ this.scanTimer = setInterval(() => {
211
+ this.scanQRCode();
212
+ }, this.scanInterval);
213
+ }
214
+
215
+ stopScanning() {
216
+ if (this.scanTimer) {
217
+ clearInterval(this.scanTimer);
218
+ this.scanTimer = null;
219
+ }
220
+ this.isScanning = false;
221
+ }
222
+
223
+ async switchCamera() {
224
+ this.stopScanning();
225
+ this.stopCamera();
226
+ this.facingMode = this.facingMode === 'user' ? 'environment' : 'user';
227
+ await this.startCamera();
228
+ }
229
+
230
+ async toggleTorch() {
231
+ if (!this.torchSupported || !this.stream) return;
232
+
233
+ const [track] = this.stream.getVideoTracks();
234
+ try {
235
+ await track.applyConstraints({
236
+ advanced: [{ torch: !this.torchOn }]
237
+ });
238
+ this.torchOn = !this.torchOn;
239
+ this.markDirty();
240
+ } catch (err) {
241
+ console.warn('Torch impossible:', err);
242
+ }
243
+ }
244
+
245
+ scanQRCode() {
246
+ if (!this.loaded || !this.video || typeof jsQR === 'undefined') return;
247
+
248
+ const canvas = document.createElement('canvas');
249
+ const ctx = canvas.getContext('2d');
250
+
251
+ // Définir la taille optimale pour la détection
252
+ const width = this.video.videoWidth;
253
+ const height = this.video.videoHeight;
254
+
255
+ canvas.width = width;
256
+ canvas.height = height;
257
+
258
+ // Dessiner l'image vidéo
259
+ ctx.drawImage(this.video, 0, 0, width, height);
260
+
261
+ // Extraire les données d'image
262
+ const imageData = ctx.getImageData(0, 0, width, height);
263
+
264
+ // Scanner le QR code
265
+ const code = jsQR(imageData.data, imageData.width, imageData.height, {
266
+ inversionAttempts: 'dontInvert',
267
+ });
268
+
269
+ if (code) {
270
+ this.handleQRCodeDetected(code.data, code.location);
271
+ } else {
272
+ this.currentQRCode = null;
273
+ }
274
+
275
+ this.markDirty();
276
+ }
277
+
278
+ handleQRCodeDetected(data, location) {
279
+ const now = Date.now();
280
+
281
+ // Éviter les doublons rapides
282
+ if (this.scanHistory.length > 0) {
283
+ const lastScan = this.scanHistory[this.scanHistory.length - 1];
284
+ if (lastScan.data === data && now - lastScan.timestamp < 2000) {
285
+ return; // Code déjà scanné récemment
286
+ }
287
+ }
288
+
289
+ this.currentQRCode = {
290
+ data: data,
291
+ location: location,
292
+ timestamp: now
293
+ };
294
+
295
+ // Ajouter à l'historique
296
+ this.scanHistory.push({
297
+ data: data,
298
+ timestamp: now
299
+ });
300
+
301
+ // Limiter l'historique
302
+ if (this.scanHistory.length > 10) {
303
+ this.scanHistory.shift();
304
+ }
305
+
306
+ // Feedback haptique
307
+ if (this.vibrateOnDetect && navigator.vibrate) {
308
+ navigator.vibrate(200);
309
+ }
310
+
311
+ // Callback
312
+ if (this.onQRCodeDetected) {
313
+ this.onQRCodeDetected(data);
314
+ }
315
+
316
+ // Stop si pas en mode continu
317
+ if (!this.continuous) {
318
+ this.stopScanning();
319
+ }
320
+
321
+ console.log('QR Code détecté:', data);
322
+ }
323
+
324
+ handlePress(relX, relY) {
325
+ // Bouton switch caméra (haut gauche)
326
+ if (relX < 60 && relY < 60) {
327
+ this.switchCamera();
328
+ return;
329
+ }
330
+
331
+ // Torch (haut droite)
332
+ if (this.torchSupported && relX > this.width - 60 && relY < 60) {
333
+ this.toggleTorch();
334
+ return;
335
+ }
336
+
337
+ // Zone centrale pour réinitialiser le scan
338
+ if (relX > this.width/2 - 100 && relX < this.width/2 + 100 &&
339
+ relY > this.height/2 - 100 && relY < this.height/2 + 100) {
340
+ if (!this.isScanning && this.loaded) {
341
+ this.startScanning();
342
+ }
343
+ return;
344
+ }
345
+ }
346
+
347
+ drawScannerOverlay(ctx) {
348
+ if (!this.showScannerOverlay) return;
349
+
350
+ const centerX = this.x + this.width / 2;
351
+ const centerY = this.y + this.height / 2;
352
+ const frameSize = this.scannerFrameSize;
353
+ const halfSize = frameSize / 2;
354
+
355
+ // Fond semi-transparent autour
356
+ ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
357
+
358
+ // Haut
359
+ ctx.fillRect(this.x, this.y, this.width, centerY - halfSize);
360
+ // Bas
361
+ ctx.fillRect(this.x, centerY + halfSize, this.width, this.height - (centerY + halfSize));
362
+ // Gauche
363
+ ctx.fillRect(this.x, centerY - halfSize, centerX - halfSize, frameSize);
364
+ // Droite
365
+ ctx.fillRect(centerX + halfSize, centerY - halfSize, centerX - halfSize, frameSize);
366
+
367
+ // Cadre de scan
368
+ ctx.strokeStyle = this.scannerFrameColor;
369
+ ctx.lineWidth = 4;
370
+ ctx.strokeRect(centerX - halfSize, centerY - halfSize, frameSize, frameSize);
371
+
372
+ // Coins décoratifs
373
+ const cornerSize = 20;
374
+
375
+ // Coin haut gauche
376
+ ctx.beginPath();
377
+ ctx.moveTo(centerX - halfSize, centerY - halfSize + cornerSize);
378
+ ctx.lineTo(centerX - halfSize, centerY - halfSize);
379
+ ctx.lineTo(centerX - halfSize + cornerSize, centerY - halfSize);
380
+ ctx.stroke();
381
+
382
+ // Coin haut droit
383
+ ctx.beginPath();
384
+ ctx.moveTo(centerX + halfSize - cornerSize, centerY - halfSize);
385
+ ctx.lineTo(centerX + halfSize, centerY - halfSize);
386
+ ctx.lineTo(centerX + halfSize, centerY - halfSize + cornerSize);
387
+ ctx.stroke();
388
+
389
+ // Coin bas gauche
390
+ ctx.beginPath();
391
+ ctx.moveTo(centerX - halfSize, centerY + halfSize - cornerSize);
392
+ ctx.lineTo(centerX - halfSize, centerY + halfSize);
393
+ ctx.lineTo(centerX - halfSize + cornerSize, centerY + halfSize);
394
+ ctx.stroke();
395
+
396
+ // Coin bas droit
397
+ ctx.beginPath();
398
+ ctx.moveTo(centerX + halfSize - cornerSize, centerY + halfSize);
399
+ ctx.lineTo(centerX + halfSize, centerY + halfSize);
400
+ ctx.lineTo(centerX + halfSize, centerY + halfSize - cornerSize);
401
+ ctx.stroke();
402
+
403
+ // Ligne animée
404
+ this.scannerLinePosition += this.scannerLineSpeed * this.scannerLineDirection;
405
+ if (this.scannerLinePosition > frameSize || this.scannerLinePosition < 0) {
406
+ this.scannerLineDirection *= -1;
407
+ }
408
+
409
+ const lineY = centerY - halfSize + this.scannerLinePosition;
410
+ ctx.fillStyle = this.scannerLineColor;
411
+ ctx.fillRect(centerX - halfSize, lineY, frameSize, this.scannerLineHeight);
412
+ }
413
+
414
+ drawControls(ctx) {
415
+ // Bouton switch caméra
416
+ ctx.fillStyle = 'rgba(0,0,0,0.5)';
417
+ ctx.beginPath();
418
+ ctx.arc(this.x + 30, this.y + 30, 25, 0, Math.PI * 2);
419
+ ctx.fill();
420
+
421
+ // Icône caméra
422
+ ctx.fillStyle = '#fff';
423
+ ctx.font = '20px Arial';
424
+ ctx.textAlign = 'center';
425
+ ctx.textBaseline = 'middle';
426
+ ctx.fillText('🔄', this.x + 30, this.y + 30);
427
+
428
+ // Torch
429
+ if (this.torchSupported) {
430
+ ctx.fillStyle = 'rgba(0,0,0,0.5)';
431
+ ctx.beginPath();
432
+ ctx.arc(this.x + this.width - 30, this.y + 30, 25, 0, Math.PI * 2);
433
+ ctx.fill();
434
+
435
+ ctx.fillStyle = this.torchOn ? '#ffeb3b' : '#fff';
436
+ ctx.fillText('⚡', this.x + this.width - 30, this.y + 30);
437
+ }
438
+
439
+ // État du scan
440
+ const scanStatusY = this.y + this.height - 40;
441
+ ctx.fillStyle = this.isScanning ? '#4CAF50' : '#FF5722';
442
+ ctx.font = '14px Arial';
443
+ ctx.textAlign = 'center';
444
+ ctx.fillText(
445
+ this.isScanning ? 'Scan en cours...' : 'Scan arrêté',
446
+ this.x + this.width / 2,
447
+ scanStatusY
448
+ );
449
+
450
+ // Dernier QR code détecté
451
+ if (this.currentQRCode) {
452
+ ctx.fillStyle = 'rgba(0, 150, 0, 0.8)';
453
+ ctx.fillRect(this.x, this.y + this.height - 90, this.width, 40);
454
+
455
+ ctx.fillStyle = '#fff';
456
+ ctx.font = '12px Arial';
457
+ ctx.textAlign = 'center';
458
+
459
+ // Tronquer si trop long
460
+ const displayText = this.currentQRCode.data.length > 50
461
+ ? this.currentQRCode.data.substring(0, 47) + '...'
462
+ : this.currentQRCode.data;
463
+
464
+ ctx.fillText(
465
+ `QR Code: ${displayText}`,
466
+ this.x + this.width / 2,
467
+ this.y + this.height - 70
468
+ );
469
+ }
470
+ }
471
+
472
+ draw(ctx) {
473
+ ctx.save();
474
+
475
+ // Fond
476
+ ctx.fillStyle = '#000';
477
+ ctx.fillRect(this.x, this.y, this.width, this.height);
478
+
479
+ if (this.error) {
480
+ ctx.fillStyle = '#ff4444';
481
+ ctx.font = '16px Arial';
482
+ ctx.textAlign = 'center';
483
+ ctx.textBaseline = 'middle';
484
+ ctx.fillText(this.error, this.x + this.width/2, this.y + this.height/2);
485
+ } else if (!this.loaded) {
486
+ ctx.fillStyle = '#fff';
487
+ ctx.font = '16px Arial';
488
+ ctx.textAlign = 'center';
489
+ ctx.textBaseline = 'middle';
490
+ ctx.fillText('Démarrage caméra...', this.x + this.width/2, this.y + this.height/2);
491
+ } else if (this.video && this.loaded) {
492
+ // Ajustement de la vidéo (cover pour remplir l'écran)
493
+ const videoRatio = this.video.videoWidth / this.video.videoHeight;
494
+ const canvasRatio = this.width / this.height;
495
+
496
+ let drawWidth = this.width;
497
+ let drawHeight = this.height;
498
+ let offsetX = 0;
499
+ let offsetY = 0;
500
+
501
+ if (videoRatio > canvasRatio) {
502
+ drawHeight = this.height;
503
+ drawWidth = drawHeight * videoRatio;
504
+ offsetX = (this.width - drawWidth) / 2;
505
+ } else {
506
+ drawWidth = this.width;
507
+ drawHeight = drawWidth / videoRatio;
508
+ offsetY = (this.height - drawHeight) / 2;
509
+ }
510
+
511
+ ctx.drawImage(this.video, this.x + offsetX, this.y + offsetY, drawWidth, drawHeight);
512
+
513
+ // Dessiner l'overlay de scan
514
+ this.drawScannerOverlay(ctx);
515
+
516
+ // Dessiner les contrôles
517
+ this.drawControls(ctx);
518
+ }
519
+
520
+ // Instructions
521
+ if (this.loaded && !this.currentQRCode) {
522
+ ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
523
+ ctx.font = '16px Arial';
524
+ ctx.textAlign = 'center';
525
+ ctx.fillText(
526
+ 'Placez un QR code dans le cadre',
527
+ this.x + this.width / 2,
528
+ this.y + this.height / 2 + 150
529
+ );
530
+ }
531
+
532
+ ctx.restore();
533
+ }
534
+ }
535
+
536
+ export default QRCodeReader;
@@ -56,6 +56,7 @@ import SliverAppBar from '../components/SliverAppBar.js';
56
56
  import AudioPlayer from '../components/AudioPlayer.js';
57
57
  import Camera from '../components/Camera.js';
58
58
  import TimePicker from '../components/TimePicker.js';
59
+ import QRCodeReader from '../components/QRCodeReader.js';
59
60
 
60
61
  // Utils
61
62
  import SafeArea from '../utils/SafeArea.js';
package/index.js CHANGED
@@ -64,6 +64,7 @@ export { default as SliverAppBar } from './components/SliverAppBar.js';
64
64
  export { default as AudioPlayer } from './components/AudioPlayer.js';
65
65
  export { default as Camera } from './components/Camera.js';
66
66
  export { default as TimePicker } from './components/TimePicker.js';
67
+ export { default as QRCodeReader } from './components/QRCodeReader.js';
67
68
 
68
69
  // Utils
69
70
  export { default as SafeArea } from './utils/SafeArea.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvasframework",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Canvas-based cross-platform UI framework (Material & Cupertino)",
5
5
  "type": "module",
6
6
  "main": "./index.js",