Adding text on image using canvas and save to image - Javascript Canvas

Javascript examples for Canvas:image

Description

Adding text on image using canvas and save to image

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 {//from w w w  . j av a 2s .  c o m
   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