简体   繁体   English

无法使用 HTML 和 JavaScript 显示完整的图像绘制画布

[英]Could not display the full image draw canvas using HTML and JavaScript

I can not able to draw the full image using canvas.我无法使用画布绘制完整图像。 Here is my code:这是我的代码:

<div class="form-group">
 <label for="exampleInputEmail1">Coupon Title</label>
<input type="text" name="emails" id="copTitle" class="form-control" placeholder="Add Coupon Title" value="<?php echo $email; ?>" required>
</div>
 <div class="couponimg" style="display:none;" id="blankImagediv">
<img class="img-responsive thumbnail" style="margin-bottom:9px; display:none;" src="http://oditek.inimages/coupon-banner-blank.jpg" crossorigin="anonymous" id="requiredImage">
<canvas id="canvas" class="img-responsive thumbnail" style="margin-bottom:9px;"></canvas>
</div>
var canvas = document.getElementById('canvas'),
    ctx = canvas.getContext('2d');
    canvas.width = $('#requiredImage').width();
    canvas.crossOrigin = "Anonymous";
    canvas.height = $('#requiredImage').height();
    ctx.drawImage($('#requiredImage').get(0), 0, 0);
    ctx.font = ":26pt Calibri";
    $(document).on('input','#copTitle',function(){
        $('#blankImagediv').css('display','block');
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.drawImage($('#requiredImage').get(0), 0, 0);
        ctx.fillStyle = "#0D5F90";
        ctx.fillText($(this).val(),40,80);
    })

Here I am getting the half of the image of original image while drawing.在这里,我在绘图时获得了原始图像的一半图像。 The right hand side of the image is not displaying.图像的右侧不显示。

hi try with following code...嗨尝试使用以下代码...

 var canvas = document.getElementById('canvas'),
  img = document.getElementById('requiredImage'),
  ctx = canvas.getContext('2d');
  img.onload = drawImage;
  canvas.width = img.width;
  canvas.height = img.height;
  ctx.font = ":26pt Calibri";
  $(document).on('input', '#copTitle', function() {
      $('#blankImagediv').css('display', 'block');
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.drawImage(img, 0, 0);
      ctx.fillStyle = "#0D5F90";
      ctx.fillText($(this).val(), 40, 80);
  });
  function drawImage()
  {
     ctx.drawImage(img, 0, 0);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM