Canvas How to - Show text from password input on canvas








Question

We would like to know how to show text from password input on canvas.

Answer


<!--  ww  w. j  a va2 s. c  om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
function showPasswordOnCanvas() {
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    context.font = "bold 16px Arial";
    context.fillText(document.getElementById("level2pass").value, 100, 100);
}
//]]>  
</script>
</head>
<body>
  <canvas id="canvas" width="200" height="200"></canvas>
    <form id="passes">Level 2 password:
        <input type="text" id="level2pass" value="password" />
        <button type="button" onclick="showPasswordOnCanvas()">Click Me!</button>
    </form>
    
</body>
</html>

The code above is rendered as follows: