Question

Here is the code which isn't working. Basically I'm allowing the user to edit some text in the div and then display its screenshot. Can someone please tell why is it not working?

<!doctype html>
<html>
<head>
  <link type="text/css" rel="stylesheet" href="main.css" />
  <title>Poster</title>
</head>
<body>
  <div id="main_image">
    <img src="image.jpg" id= "image"/>
    <div id="user_text">IT IS TOTALLY AWESOME</div>
  </div>
  <div id="edit_text_box">
    <textarea id="user_input" placeholder="Your text here.." rows="2" cols="30" ></textarea>
    <div id="change_size">
      <span style="float: left;">FONT-SIZE : </span>
      <button id="decrease_size">-</button>
      <button id="increase_size">+</button>
    </div>
    <input id="submit" type="submit" value="SUBMIT" />
  </div>
    <script type="text/javascript">
    window.onload = function() {
      html2canvas( [ document.body ], {
      onrendered: function( canvas ) {
        document.body.appendChild( canvas );
      }
    });
  };
  </script>
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/edit_text.js"></script>
</body>
</html>
Was it helpful?

Solution

I changed your code a little bit and it works:

<!doctype html>
<html>
<head>
  <link type="text/css" rel="stylesheet" href="main.css" />
  <title>Poster</title>
  <script type="text/javascript" src="html2canvas.js"></script>
  <script type="text/javascript">
    var Submit = function() {
      html2canvas(document.body, {
        onrendered: function(canvas) {
          document.body.appendChild(canvas);
        }
      });
  };
  </script>
</head>
<body>
  <div id="main_image">
    <img src="image.jpg" id= "image"/>
    <div id="user_text">IT IS TOTALLY AWESOME</div>
  </div>
  <div id="edit_text_box">
    <textarea id="user_input" placeholder="Your text here.." rows="2" cols="30" ></textarea>
    <div id="change_size">
      <span style="float: left;">FONT-SIZE : </span>
      <button id="decrease_size">-</button>
      <button id="increase_size">+</button>
    </div>
    <button onclick="Submit();" >Submit</button>
  </div>
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/edit_text.js"></script>
</body>
</html>

First, I added html2canvas JavaScript file (<script type="text/javascript" src="html2canvas.js"></script>). You can download it at https://github.com/niklasvh/html2canvas/releases/download/0.4.1/html2canvas.js and put that file to the same folder as your HTML. Apparently, you are trying to create something like a form, but there's no form tag in your code. You can call html2canvas either when you submit a form (you have to add <form> then) or using onclick attribute of a button. I changed your submit input element to button and bind Submit method to its onclick attribute. When user clicks the button, screenshot appears below the form.

OTHER TIPS

Just use this code

document.querySelector('button').addEventListener('click', function() {
  html2canvas(document.querySelector('.specific'), {
    onrendered: function(canvas) {
      // document.body.appendChild(canvas);
      return Canvas2Image.saveAsPNG(canvas);
    }
  });
});
.btn {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857143;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  border-radius: 4px;
}
.btn-default {
  color: #333;
  background-color: #fff;
  border-color: #ccc;
}
h1 {
  font-size: 36px;
  font-family: inherit;
  font-weight: 500;
  line-height: 1.1;
  color: inherit;
}
h1 small {
  font-size: 65%;
  font-weight: 400;
  line-height: 1;
  color: #777;
  display: block;
  padding-top: 15px;
}
.specific {
  background-color: #fff;
}
p a {
  padding: 5px;
}
<script src="https://superal.github.io/canvas2image/canvas2image.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<div class="specific">
  <h1>Click to Take a Screenshot & Download it! <small>using html2canvas.js + canvas2image.js</small></h1> 
  <p>This is a simple demo.</p>
  <p>Use html2canvas.js to take a screenshot of a specific div and then use canvas2image.js to download the screenshot as an image locally to your filesystem.</p>
  <button type="button" class="btn btn-default">Take a Screenshot!</button>
  <p>References: <a href="http://html2canvas.hertzen.com/">html2canvas.js</a><a href="https://github.com/SuperAL/canvas2image">canvas2image.js</a>
  </p>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top