8

Are there software/services for automated conversion of a typical image format (png, bmp, jpg/gif even) to Canvas / HTML5?

ina
  • 19,167
  • 39
  • 122
  • 201

6 Answers6

14

Here is a tool that will generate JavaScript code to draw the image on canvas: http://lab.abhinayrathore.com/img2canvas/

Abhinay
  • 141
  • 1
  • 2
  • 4
    Thanks for actually answering OP's question. The other answers on this page are irrelevant. –  Feb 22 '12 at 16:53
  • 1
    This tool does the job, but it's also really inefficient. It does a fillRect for each pixel and doesn't recognize continuous lines or boxes at all, much less circles or other shapes. – Samuel Neff Apr 04 '13 at 19:15
9

You don't need no conversion, just use the image (either new by url or any one in the DOM) by

canvas.drawImage(image, dx, dy)
canvas.drawImage(image, dx, dy, dw, dh)
canvas.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)

(taken from here).

See the tutorial on developer.mozilla.org.

groovecoder
  • 1,551
  • 1
  • 17
  • 27
mkluwe
  • 3,823
  • 2
  • 28
  • 45
1

You can use the site listed above, but here is the relevant code:

function convertImage(canvas, callback) {
var image = new Image();
image.onload = function(){
callback(image);
}
image.src = canvas.toDataURL("image/png");
}

Also, I put together a working jsfiddle demo.

Dan H
  • 51
  • 2
1

w3school has the answer: http://www.w3schools.com/tags/canvas_drawimage.asp

window.onload = function() {
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    var img=document.getElementById("scream");
    ctx.drawImage(img,10,10);
};
Savaratkar
  • 1,974
  • 1
  • 24
  • 44
0
     <!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      // draw cloud
      context.beginPath();
      context.moveTo(170, 80);
      context.bezierCurveTo(130, 100, 130, 150, 230, 150);
      context.bezierCurveTo(250, 180, 320, 180, 340, 150);
      context.bezierCurveTo(420, 150, 420, 120, 390, 100);
      context.bezierCurveTo(430, 40, 370, 30, 340, 50);
      context.bezierCurveTo(320, 5, 250, 20, 250, 50);
      context.bezierCurveTo(200, 5, 150, 20, 170, 80);
      context.closePath();
      context.lineWidth = 5;
      context.fillStyle = '#8ED6FF';
      context.fill();
      context.strokeStyle = '#0000ff';
      context.stroke();

      // save canvas image as data url (png format by default)
      var dataURL = canvas.toDataURL();
    </script>
  </body>
</html>      
Chetan Ghadiya
  • 366
  • 5
  • 16
0

Inkscape can actually save files to HTML5 Canvas format. Tested for SVG.