CKTIS / HTML
L’attributo capture nei campi file: user ed environment
In un campo file, `capture` indica una preferenza su come creare un nuovo file multimediale. Per foto e video, `user` privilegia la fotocamera anteriore ed `environment` quella rivolta verso l’esterno.
Versione: W3C HTML Media Capture Recommendation (2018); implementation support varies
Sintassi
<input type="file" accept="image/*" capture="user">
<input type="file" accept="image/*" capture="environment">capture è un attributo di HTML Media Capture che si usa su un elemento input con type="file". Usa accept per indicare il tipo di file atteso: image/* per le immagini, video/* per i video o audio/* per l’audio. Resta un campo file: l’attributo non fornisce alla pagina un flusso in diretta e non carica il file.
Per foto e video, capture="user" dà la preferenza alla fotocamera rivolta verso chi usa il dispositivo, di solito quella anteriore. capture="environment" preferisce quella rivolta verso l’esterno, di solito la posteriore. Indica il valore esplicitamente: capture senza valore non seleziona nessuno dei due orientamenti.
È un’indicazione, non un vincolo. Se l’orientamento richiesto non è disponibile, il browser può usare la modalità predefinita. A seconda del dispositivo e del supporto del browser, può anche mostrare un menu o la normale finestra di selezione dei file. Se è importante poter scegliere un file esistente, offri un campo separato senza capture. accept non verifica il contenuto dei file: se li ricevi su un server, convalidali lì.
Esempi
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Photo capture preferences</title>
</head>
<body>
<p>
<label for="front-photo">Take a selfie (front camera preferred)</label><br>
<input id="front-photo" type="file" accept="image/*" capture="user">
</p>
<p>
<label for="rear-photo">Take a photo (rear camera preferred)</label><br>
<input id="rear-photo" type="file" accept="image/*" capture="environment">
</p>
<p>
<label for="image-file">Choose an image file</label><br>
<input id="image-file" type="file" accept="image/*">
</p>
<script>
const controls = [...document.querySelectorAll('input[type="file"]')];
const descriptions = new Map([
['user', 'requests a front-facing camera'],
['environment', 'requests an outward-facing camera'],
[null, 'makes no capture request']
]);
const expectedPreferences = [...descriptions.keys()];
const isVisible = element =>
element.getClientRects().length > 0 &&
getComputedStyle(element).visibility === 'visible';
// Check the controls and their markup, not which picker or camera opens.
if (controls.length !== expectedPreferences.length ||
!controls.every((control, index) =>
isVisible(control) &&
[...control.labels].some(label => isVisible(label) && label.innerText.trim()) &&
control.accept === 'image/*' &&
control.getAttribute('capture') === expectedPreferences[index] &&
!control.form)) {
throw new Error('Expected three visible, labeled image file controls with the specified capture preferences and no upload form.');
}
const [first, second, third] = controls.map(control =>
descriptions.get(control.getAttribute('capture'))
);
console.log(
'The page displays three labeled image file controls. ' +
`The first ${first}, the second ${second}, and the third ${third}. ` +
'The browser and device determine what opens when each control is activated. ' +
'The page does not upload anything.'
);
</script>
</body>
</html>Risultato
The page displays three labeled image file controls. The first requests a front-facing camera, the second requests an outward-facing camera, and the third makes no capture request. The browser and device determine what opens when each control is activated. The page does not upload anything.
Il primo campo preferisce la fotocamera anteriore, il secondo quella posteriore e il terzo non richiede l’acquisizione. Confronta ciò che si apre su un dispositivo mobile e su un computer. L’esempio non carica alcun file.
Lezioni correlate
- HTML · Lezione 7 · Intermedio · Versione: 2