Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to capture only viewport using Canvas drawImage() in camera app?

I'm making simple camera component in React, with functionality similar to those from Messenger or Snapchat. I used CSS to make video stream responsive and implemented drawImage function to capture it. The problem is that it captures whole range of camera instead of viewport only. The difference is mostly seen when taking a picture on mobile vertically, because although video stream is cropped, captured image shows cropped parts too and is condensed. I know it propably can be fixed with changing drawImage() parameters but I didn't find working solution yet.

App.js

  const getVideo = () => {
    navigator.mediaDevices
      .getUserMedia({
        video: { width: window.screen.width, height: window.screen.height}
      })
      .then(stream => {
        let video = videoRef.current;
        video.srcObject = stream;
        video.play();
      })
      .catch(err => {
        console.error(err);
      })
  }

  const takePhoto = () => {
    const width =  window.innerWidth;
    const height =  window.innerHeight;

    let video = videoRef.current;
    let photo = photoRef.current;

    photo.width = width;
    photo.height = height;

    let ctx = photo.getContext('2d');
    ctx.drawImage(video, 0, 0, width, height);
    setHasPhoto(true)
  }

app.css

.camera video {
  width: 100%;
  object-fit: cover;
  height: 100vh;
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!