0

i have created a javascript canvas in which when user types a text, it will print on the image. the code is below:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style type="text/css">
  canvas {
    height: 500px;
    width: 500px;
  }
</style>


<div class="container">
  <div style="margin-top: 5%;" class="row">


    <div class="col-md-6"><canvas id="imageCanvas"></canvas></div>



    <div class="col-md-6"><input type="text" id="name" placeholder="Name" /></div>

  </div>
</div>





<script type="text/javascript">
  var text_title = "Heading";

  var canvas = document.getElementById('imageCanvas');
  var ctx = canvas.getContext('2d');
  var img = new Image();
  // img.crossOrigin = "anonymous";

  window.addEventListener('load', DrawPlaceholder)

  function DrawPlaceholder() {
    img.onload = function() {
      DrawOverlay(img);
      DrawText(text_title);
      DynamicText(img)
    };
    img.src = 'card.png';

  }

  function DrawOverlay(img) {

    ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);


    ctx.fillStyle = 'rgba(230, 14, 14, 0)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
  }

  function DrawText(text) {
    ctx.fillStyle = "black";
    ctx.textBaseline = 'middle';
    ctx.font = "50px 'Montserrat'";
    ctx.fillText(text, 50, 50);
  }

  function DynamicText(img) {
    document.getElementById('name').addEventListener('keyup', function() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      DrawOverlay(img);
      text_title = this.value;
      DrawText(text_title);
    });
  }
</script>

everything is fine except the image. The image is not correctly coming in the canvas. Its being stretched or not getting its original size. i tried resizing the canvas and all. I want the original size of the image to appear as it is. Can anyone please tell me what should i do in my code to get the original size of the image. thanks in advance

14
  • Place the image to the canvas using the basic arguments ctx.drawImage(img, 0, 0);, instead of cropping the image. Commented Jan 8, 2020 at 12:51
  • i tried your code which is giving me cropped image Commented Jan 8, 2020 at 12:52
  • Please kindly resize the canvas according to the original image, before drawing the image. The original size of the image is stored in naturalWidth/Height properties of the image, width and height represent the presentation dimensions of the image placed on the page. Commented Jan 8, 2020 at 12:53
  • @Teemu the image size is 500px * 500px. and the canvas size also same Commented Jan 8, 2020 at 12:59
  • Then it is not cropped, if you're not messing with the dimensions. Commented Jan 8, 2020 at 13:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.