A query related to web-cam in react js for attendance of user

A create a gym web app where user click his picture if user inside the 5m of radius then present mark in db and show on web app .

see the first picture

in which user click on capture then a different window appear and run the camera stream but i want to show video stram in circle

after clicking the picture show two button “Retake” and “Submit” if user click on retake btn then must camera on again
if user click on submit button then image and location send to the db.

Note :-
I already do the work related to location you don’t worry.

[enter image description here](https://i.sstatic.net/9ysU83KN.png)

import { useState, useEffect } from "react";
import axios from "axios";

function MyLocation() {
  const [position, setPosition] = useState({ latitude: null, longitude: null });
  const [image, setImage] = useState(null);
  const [showCamera, setShowCamera] = useState(false);
  const [showCaptureButton, setShowCaptureButton] = useState(true);
  const [showRetakeSubmitButtons, setShowRetakeSubmitButtons] = useState(false);

  useEffect(() => {
    if ("geolocation" in navigator) {
      navigator.geolocation.getCurrentPosition(function (position) {
        setPosition({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        });
      });
    } else {
      console.log("Geolocation is not available in your browser.");
    }
  }, []);

  const takePicture = () => {
    navigator.mediaDevices
      .getUserMedia({ video: true })
      .then((stream) => {
        const video = document.createElement("video");
        const canvas = document.createElement("canvas");

        video.autoplay = true;
        document.body.appendChild(video);
        video.srcObject = stream;

        const context = canvas.getContext("2d");
        video.addEventListener("loadedmetadata", () => {
          canvas.width = video.videoWidth;
          canvas.height = video.videoHeight;
        });

        const captureImage = () => {
          context.drawImage(video, 0, 0, canvas.width, canvas.height);
          const imgDataUrl = canvas.toDataURL("image/png");
          setImage(imgDataUrl);
          stream.getTracks().forEach((track) => track.stop());
          document.body.removeChild(video);
          document.body.removeChild(canvas);
          setShowCaptureButton(false);
          setShowRetakeSubmitButtons(true);
        };

        const retakePicture = () => {
          stream.getTracks().forEach((track) => track.stop());
          document.body.removeChild(video);
          document.body.removeChild(canvas);
          setShowCaptureButton(true);
          setShowRetakeSubmitButtons(false);
          takePicture(); 
        };

        const submitPicture = () => {
          if (image && position.latitude && position.longitude) {
          
            const formData = new FormData();
            formData.append("image", image);
            formData.append("latitude", position.latitude);
            formData.append("longitude", position.longitude);

            axios
              .post("YOUR_BACKEND_URL", formData)
              .then((response) => {
                console.log("Image submitted:", response.data);
              })
              .catch((error) => {
                console.error("Error submitting image:", error);
              });

            
            setImage(null);
            setShowCamera(false);
            setShowCaptureButton(true);
            setShowRetakeSubmitButtons(false);
          }
        };

        const submitButton = document.createElement("button");
        submitButton.innerText = "Submit";
        submitButton.addEventListener("click", submitPicture);

        const retakeButton = document.createElement("button");
        retakeButton.innerText = "Retake";
        retakeButton.addEventListener("click", retakePicture);

        const buttonsContainer = document.createElement("div");
        buttonsContainer.classList.add("flex", "justify-around", "mt-4");
        buttonsContainer.appendChild(submitButton);
        buttonsContainer.appendChild(retakeButton);

        const captureButton = document.createElement("button");
        captureButton.innerText = "Capture";
        captureButton.classList.add(
          "bg-blue-500",
          "text-white",
          "px-4",
          "py-2",
          "rounded-full"
        );
        captureButton.addEventListener("click", captureImage);

        const container = document.createElement("div");
        container.classList.add("flex", "flex-col", "items-center", "mt-8");
        container.appendChild(video);
        container.appendChild(captureButton);
        container.appendChild(buttonsContainer);

        document.body.appendChild(container);
      })
      .catch((error) => {
        console.error("Error accessing the camera:", error);
      });
  };

  return (
    <div className="flex items-center justify-center h-screen">
      <div className="bg-transparent p-4 rounded-lg border-2 border-dashed border-pink-800 text-white flex flex-col justify-center items-start">
        <h1 className="mx-auto my-5">
          Take a selfie to verify your identity!
        </h1>
        <div className="rounded-full h-[300px] w-[300px] bg-gray-400 mx-auto px-auto">
          {image && (
            <img
              src={image}
              alt="Captured"
              className="rounded-full h-[300px] w-[300px] object-cover"
            />
          )}
        </div>

        {showCaptureButton && (
          <div className="btn-div mx-auto my-2">
            <button
              onClick={takePicture}
              className="bg-pink-600 hover:bg-pink-900 rounded-lg border-none px-8 py-2 font-bold"
            >
              Capture
            </button>
          </div>
        )}

        {showRetakeSubmitButtons && (
          <div className="btn-div flex justify-between gap-x-3 w-full">
            <button
              onClick={retakePicture}
              className="bg-pink-600 hover:bg-pink-900 rounded-lg border-none px-8 py-2 font-bold"
            >
              Retake
            </button>
            <button
              onClick={submitPicture}
              className="bg-pink-600 hover:bg-pink-900 rounded-lg border-none px-8 py-2 font-bold"
            >
              Submit
            </button>
          </div>
        )}
      </div>
    </div>
  );
}

export default MyLocation;

in which user click on capture then a different window appear and run the camera stream but i want to show video stram in circle

after clicking the picture show two button “Retake” and “Submit” if user click on retake btn then must camera on again
if user click on submit button then image and location send to the db.

New contributor

hello_world is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật