【发布时间】:2019-05-20 04:38:51
【问题描述】:
我对此主题有疑问
How to add text on image using Javascript and Canvas
我无法在第一时间获得图像。但是当我点击刷新时会加载图像。
这是我的 php 文件中的代码:
<canvas id="canvas"> </canvas>
<form class="myNameForm">
<?php echo '<img style="display:none" class="myimage"
src="./save/'.$filenamewithoutExt.'" crossorigin="anonymous">';?>
<label class="inpLable" for="input">Enter Name: </label>
<input class="inp" maxlength="12" type="text" id="inp"/>
</form>
这是我的 js 文件中的代码:
window.onload = function() {
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
canvas.width = $('img').width();
canvas.crossOrigin = "Anonymous";
canvas.height = $('img').height();
ctx.drawImage($('img').get(0), 0, 0);
$(document).on('input', '#inp', function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = "20pt Verdana";
ctx.drawImage($('img').get(0), 0, 0);
ctx.fillStyle = "white";
ctx.fillText($(this).val(), 170, 590);
});
};
【问题讨论】:
-
发布您的代码,向我们展示您到目前为止所做的尝试。
-
尝试使用fabric.js
-
@Mirakurun 我已经更新了问题并添加了代码。
-
问题是因为“图像异步加载”,但我无法修复它。 stackoverflow.com/questions/30009528/…
-
我猜问题是
<img>有display: none因此它可以没有有offsetWidth
标签: javascript php html canvas