title: jsPDF and image loading
body: <p>I'm tryna use the jsPDF library. I'd like to load and insert an image, and export the PDF file.</p>
  
  <p>My issue is about the image loading. I'm doing this: <code>var imageData = getBase64Image('thinking-monkey.jpg');</code> and I should get the dataURL in base64 inside <code>imageData</code>.</p>
  
  <p>My <code>getBase64Image()</code> function does the following:</p>
  
  <pre><code>function getBase64Image(url) {
      var img = new Image();
      var dataURL;
  
      img.src = url;
  
      img.onload = function() {
          var canvas = document.createElement('canvas');
          canvas.width = img.width;
          canvas.height = img.height;
  
          var context = canvas.getContext('2d');
          context.drawImage(img, 0, 0);
  
          dataURL = canvas.toDataURL('image/jpeg');
  
      }
  
      return dataURL;
  }
  </code></pre>
  
  <p>But it returns 'undefined', because the image is like 65 Kb and doesn't load up at once. So when at <code>return dataURL;</code> the variable is still undefined.</p>
  
  <p>I've tried to add a <code>setTimeout()</code> right before <code>return dataURL;</code> but it doesn't seem to be working.</p>
  
  <p>How can I wait until the image is fully loaded to return dataURL?</p>
  
  <p>Thanks.</p>