draw image and text - Javascript Canvas

Javascript examples for Canvas:image

Description

draw image and text

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <style id="compiled-css" type="text/css">

canvas {// w w w .j  a  va  2 s.  c om
   background:black;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
draw();
function draw() {
    var canvas = document.getElementById('idCanvas');
    var context = canvas.getContext('2d');
    var imageObj = new Image();
  imageObj.onload = function() {
    context.drawImage(imageObj, 0, 0);
    context.font = "40px Calibri";
    context.fillStyle = "red";
    context.fillText("My TEXT!", 50, 300);
    var canvas = document.getElementById('idCanvas');
    var dataURL = canvas.toDataURL();
    console.log(dataURL);
  }
imageObj.setAttribute('crossOrigin', 'anonymous');
imageObj.src = "http://lorempixel.com/400/200/";
};
    });

      </script> 
   </head> 
   <body> 
      <canvas id="idCanvas" width="576" height="577"></canvas>  
   </body>
</html>

Related Tutorials