2016-12-14 79 views
0

我遇到问题。我无法使用画布绘制完整图像。我在下面解释我的代码。无法显示使用HTML和Javascript的完整图像绘制画布

<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); 
    }) 

这里我得到原单图像的图像的一半,而绘制图像的.The右侧没有显示。请帮我解决这个问题。

+1

是图像比画布本身更大? – haxxxton

+0

图片大小为754 * 300。 – satya

+1

比你的画布大吗? – haxxxton

回答

1

喜尝试用下面的代码...

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); 
    } 
+0

其工作。谢谢。但在这里我不能增加字体大小。 – satya

+0

您可以通过'ctx.font =“50px Calibri”;'...更改它,并且您可以将此答案标记为正确的答案;-) – spankajd

+0

谢谢您。其按照预期工作。 – satya