Converting a React JS Div into a PNG image or taking a screenshot of the

Looking for a solution to capture the div with id my-char when char is clicked? I want to take a screenshot without the parent background color and use it on serverside. I've tried some methods but none seem to work.

Here is the code snippet and jsFiddle link: https://jsfiddle.net/wyrustaaruz/umL3zdk2/12/

const HasanRiza = () => {

  const TestButton = () => {
    /* Want to screenshot the div with id my-char when char is clicked */
    /* Sending the screenshot to serverside with axios */
    /* Do not want to include background Color */

  }
  return (
    <
    div style = {
      {
        backgroundColor: 'blue'
      }
    } >
    <
    div onClick = {
      () => TestButton()
    }
    id = {
      "my-char"
    }
    className = "character-absolute" >

    <
    img id = "canvas"
    src = {
      "http://178.128.68.183/images/43ae4572-bb41-4a7c-8541-eafc49627d84.png"
    }
    style = {
      {
        width: '43%',
        maxWidth: 435,
        minWidth: 200
      }
    }
    /> <
    img src = {
      "http://178.128.68.183/images/ffb64bf0-5da4-4de5-bd83-de9cd4e570f5.png"
    }
    style = {
      {
        width: '43%',
        maxWidth: 435,
        minWidth: 200
      }
    }
    /> <
    img src = {...
    
  
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

.character-absolute img {
  position: absolute;
  top: 10px;
  left: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>

Answer №1

Learn how to utilize the html2canvas npm module for seizing canvas elements and transforming them into images with the following code snippet

import html2canvas from 'html2canvas'

const CaptureCanvas = () => {
  html2canvas(document.body).then(canvas => 
    canvas.toDataURL()
  ) 
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When a div is floated to the right, it can lead to unexpected outcomes

Here is the code structure in the JSfiddle link below. When I apply float:right to the "Add" text, it creates extra space above the "Quick Links" & "Top 20 Sites" sections. #home-content { padding: 5px; } #home-content .css-table #row1-col3 .col-body { ...

Tips for incorporating the multiply times async function into mocha tests

I am attempting to utilize the async function foo multiple times in my mocha tests. Here is how I have structured it: describe('This test', () => { const foo = async () => { const wrapper = mount(Component); const button ...

Using Angular to create a dynamic form with looping inputs that reactively responds to user

I need to implement reactive form validation for a form that has dynamic inputs created through looping data: This is what my form builder setup would be like : constructor(private formBuilder: FormBuilder) { this.userForm = this.formBuilder.group({ ...

Browse through websites without refreshing a shared component specific to those pages in Angular 2

Check out this example app: This is the main component of my Angular application: import { Component } from '@angular/core'; @Component({ selector: 'root', template: ` <h1>My Dummy Angular App</h1> <router-out ...

Display a modal popup and simultaneously launch a link in a new tab

I need help setting up my modal popup. I would like to customize it so that a specific link opens in a new window instead of within an iframe. Currently, this is the code I am using: <div class="modal-content" style="max-width:767px;"> <span c ...

Transform a flexbox child into a complete row that is wider than the container

I'm currently in the process of developing a portfolio section using Bootstrap 4 and delving into flexbox knowledge. Each item in the portfolio starts with a thumbnail that can be expanded or collapsed to reveal more details when clicked. When the vi ...

What is the best way to vertically align a container beneath a navbar without triggering the appearance of a scrollbar?

I'm in the process of developing an application using Bootstrap 4. The app features a navigation bar and a container located beneath it. Below you'll find the structure of the application: <body> <div id="root"> ...

Ensure that the checkbox is always selected

My goal is to have a checkbox always checked unless we intentionally want it to be unchecked. Check out the following HTML code: <input type="checkbox" class="custom-control-input" [(ngModel)]="productDetails.iscanRetrunable" name="isc ...

I'm curious if it's possible to create a scrolling marquee on an SVG path with the use of HTML and CSS?

I am interested in creating a unique effect similar to what is showcased on this website or in the main menu of Colin McRae Rally 2.0. My goal is to generate a path and animate text along it, with the letters wrapping around to the start as they reach the ...

Achieve a full-page height navigation bar using CSS

Currently in the process of building a website utilizing CSS and HTML, I have successfully added a navigation bar to the left side of the page using this specific CSS code. However, an issue arises when the screen is scrolled down as the navigation bar doe ...

Update the typing of a React component based on a specified prop

Currently, I am in the process of developing a React component called text that will have the ability to change the tag that is rendered. export interface TextProps extends HTMLAttributes<HTMLElement> { children: ReactNode; as?: React.ElementType ...

Automatic resizing for elements with the class "Div AutoResize."

I am facing an issue with a row of divs. While all the divs on the left automatically resize to fit their content, I want the leftmost div to adjust its size based on the content, but be influenced by the divs on the right. In other words, if the right div ...

The Instagram API is unable to access follower numbers

I have implemented the following code to fetch Followers from an Instagram user: function getInstagramFollowers(){ jQuery.ajax({ url: 'https://api.instagram.com/v1/users/self/', dataType: 'jsonp', type: 'GET', ...

Instructions on extracting the initial 16 bytes of data from a base64 decoded string

Looking to extract the first 16 bytes of data from a Base64Decode string. Here is my code snippet, seeking suggestions on how to achieve this: let base64Encodeded = "Hf8Qqpr1klv+Mjle4v0yfOXbQpiXwICyEDF4bYGt/ve7h9a1cEqd5z3QpvnPMLzRpDxSBEutaOdh8SN/Yi9 ...

How to Customize the Drawer Color in Material-UI v5

I'm currently using MUI v5 in my project and I am encountering some challenges while attempting to style a Drawer component with a black background color. As this update is relatively new, I have not been able to find much information on customizing t ...

Is it considered fundamentally inappropriate to call $scope.$digest within $scope.$on?

I recently inherited some AngularJS code, and my knowledge of both the codebase and Angular itself is limited. Within the code I inherited, there are instances where $scope.$digest is being called inside a $scope.$on method within a controller. Here's ...

What is the best way to distinguish between various objects that are all in a single line?

After creating an array of objects with data such as name and id, I used the res.json() method to convert it into json format for browser use. However, when I input the array of object data, it looked like this: [ { id: 1, name: 'albany sof ...

What is the best way to differentiate the handling of a 401 Unauthorized response from other errors within an Angular 8 service that utilizes RxJS?

My REST API implementation requires an access token for user identification with each call. If the token is missing or expired, the endpoint returns a 401 UNAUTHORIZED response. There are instances where I make API calls using a timer in my service class: ...

What is the most efficient way to identify the first duplicate number in an array that contains numbers ranging from 1 to the length of the array, ensuring that the second occurrence of the duplicate

Having some trouble with this code snippet. Tried running it but no luck. Here are the test cases: Input: 2,1,3,5,3,2 Expected Output: 3; Input: 2,4,3,5,1 Expected Output: -1 Input: 2,4,3,5,1,7 Check out the code below: function FirstDuplicate(array) ...

What is the best way to incorporate a percentage-based width scrollbar?

Help needed: I want to implement a scroll feature for the parent div but here's the catch - the width of the text inside the div needs to be in percentage. So, if there is a long name, it should scroll within the parent div. HTML: <div id="list"& ...