如何使用按钮的 onclick 函数在 canvas 中插入文本?

How do I insert the text inside the canvas using the buttons onclick function?

如标题中所述,我希望能够使用按钮的 onclick 功能将文本从文本框插入到 canvas 中。现在请记住,我是这方面的新手,所以可能到处都是错误。

function startGame() {
    myGameArea.start();
}
    
var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        this.gravity = 0.05;
        this.gravitySpeed = 0;
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
            
        }
}
    
function myFunction() {
    
}
canvas { 
  border: 1px solid #d3d3d3;
  background-color: #f1f1f1;
}
<body onload="startGame()">
<input type="text" name="Box" id="Box" value="" placeholder="Write whatever"/>
<button onclick="myFunction()">Submit</button>
</body>

@sosa123,检查下面的解决方案,让我知道这个解决方案是否适合你?

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>
function startGame() {
    myGameArea.start();
}

var myGameArea = {
    canvas : createElementOnDom('CANVAS','myCANVAS'),
    start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        this.gravity = 0.05;
        this.gravitySpeed = 0;
        document.body.insertBefore(this.canvas,document.body.childNodes[0]);

        }
}

    function createElementOnDom (type,id) {
   var element=document.createElement(type);
   element.id=id;
   return element;
}
function myFunction() {
    var canvas = document.getElementById("myCANVAS");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText(document.getElementById("Box").value,10,50);
}
</script>
<style>
canvas { 
  border: 1px solid #d3d3d3;
  background-color: #f1f1f1;
}
</style>
</head>
<body>
<body onload="startGame()">
<input type="text" name="Box" id="Box" value="" placeholder="Write whatever"/>
<button onclick="myFunction()">Submit</button>
</body>
</body>
</html>

从我的角度来看,最好将方法直接存储在您的对象下 myGameArea。方法可以直接调用,只需要get 2D context of canvas,然后fill it.

所需的代码示例,完整代码如下:

<button onclick="myGameArea.myFunction()">Submit</button>

var myGameArea = {
...
    myFunction : function() {
        var txt=document.getElementById("Box");

        var ctx=this.canvas.getContext("2d");
        ctx.font="20px Georgia";
        ctx.fillText(txt.value, 10, 20);
    }
}

function startGame() {
    myGameArea.start();
}
    
var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        this.gravity = 0.05;
        this.gravitySpeed = 0;
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        },
    myFunction : function() {
        var txt=document.getElementById("Box");

        var ctx=this.canvas.getContext("2d");
        ctx.font="20px Georgia";
        ctx.fillText(txt.value, 10, 20);
    }
}
canvas { 
  border: 1px solid #d3d3d3;
  background-color: #f1f1f1;
}
<body onload="startGame()">
<input type="text" name="Box" id="Box" value="" placeholder="Write whatever"/>
<button onclick="myGameArea.myFunction()">Submit</button>
</body>