I have tried this code but it doesn't work out. Please tell errors(if any) or suggest a great and short code for displaying an image on canvas and then displaying text(by user input) over the image.
<html>
<body>
<form>
<input type="text" id="nm" placeholder="Name" value="some name">
<button onclick="ck()">Preview</button>
</form>
<canvas style="border:1px solid black;" height="500" width="700" id="can"> </canvas>
<script>
var can=document.getElementById('can').getcontext('2d');
function ck() {
function def(){
var img=new Image();
can.font='40px algerian';
img.src="form.jpg";
img.onload=function() {
can.drawImage(img,0,0,700,500);
can.fillText("your name",400,200);
};
}
function nondef() {
var img=new Image();
can.font='40px algerian';
img.src="form.jpg";
img.onload=function() {
var nm=document.getElementById("nm").value;
can.drawImage(img,0,0,700,500);
can.fillText("nm",400,200);
};
}
if(document.getElementById(nm).value=="")
def();
else
nondef();
}
</script>
</body>
</html>