Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / HTML

Atribut capture u souborového pole: user a environment

Atribut `capture` u souborového pole vyjadřuje preferenci při pořizování nového multimediálního souboru. U fotek a videí `user` upřednostňuje přední kameru, zatímco `environment` kameru směřující do okolí.

Verze: W3C HTML Media Capture Recommendation (2018); implementation support varies

Syntaxe

<input type="file" accept="image/*" capture="user">
<input type="file" accept="image/*" capture="environment">

capture je atribut z HTML Media Capture určený pro prvek input s type="file". Atributem accept urči očekávaný druh souboru: image/* pro obrázky, video/* pro video nebo audio/* pro zvuk. Stále jde o souborové pole: atribut stránce neposkytuje živý přenos ani soubor neodesílá.

U fotek a videí capture="user" upřednostňuje kameru mířící na uživatele, zpravidla přední. capture="environment" upřednostňuje kameru mířící do okolí, zpravidla zadní. Hodnotu zapiš výslovně: samotné capture nevybere ani jeden z těchto směrů.

Jde o doporučení, nikoli požadavek. Pokud požadovaný směr není dostupný, prohlížeč může použít výchozí režim. Podle zařízení a podpory v prohlížeči se také může zobrazit nabídka nebo běžný dialog pro výběr souboru. Pokud má být možné vybrat existující soubor, nabídni samostatné pole bez capture. accept neověřuje obsah souboru: pokud soubory přijímáš na serveru, ověřuj je tam.

Pří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ýsledek

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.

První pole upřednostňuje přední kameru, druhé zadní a třetí o pořízení snímku nežádá. Porovnej, co se otevře na mobilu a co na počítači. Příklad nic neodesílá.

Související lekce

  • HTML · Lekce 7 · Středně pokročilá · Verze: 2

Zdroje