CKTIS / HTML
Atribút capture v poli na výber súboru: user a environment
Atribút `capture` v poli na výber súboru vyjadruje preferenciu pri vytváraní nového multimediálneho súboru. Pri fotkách a videách `user` uprednostňuje prednú kameru, kým `environment` kameru smerujúcu do okolia.
Verzia: W3C HTML Media Capture Recommendation (2018); implementation support varies
Syntax
<input type="file" accept="image/*" capture="user">
<input type="file" accept="image/*" capture="environment">capture je atribút z HTML Media Capture určený pre prvok input s type="file". Atribútom accept urči očakávaný druh súboru: image/* pre obrázky, video/* pre video alebo audio/* pre zvuk. Stále ide o pole na výber súboru: atribút stránke neposkytuje živý prenos ani súbor neodosiela.
Pri fotkách a videách capture="user" uprednostňuje kameru namierenú na používateľa, zvyčajne prednú. capture="environment" uprednostňuje kameru namierenú do okolia, zvyčajne zadnú. Hodnotu uveď výslovne: samotné capture nevyberie ani jeden z týchto smerov.
Je to odporúčanie, nie požiadavka. Ak požadovaný smer nie je dostupný, prehliadač môže použiť predvolený režim. V závislosti od zariadenia a podpory v prehliadači sa môže zobraziť aj ponuka alebo bežné okno na výber súboru. Ak má byť možné vybrať existujúci súbor, ponúkni samostatné pole bez capture. accept neoveruje obsah súborov: ak ich prijímaš na serveri, overuj ich tam.
Príklady
<!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>Výsledok
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.
Prvé pole uprednostňuje prednú kameru, druhé zadnú a tretie nežiada o nasnímanie obrázka. Porovnaj, čo sa otvorí v mobile a čo v počítači. Príklad nič neodosiela.
Súvisiace lekcie
- HTML · Lekcia 7 · Stredne pokročilá · Verzia: 2