我想删掉画布文字..帮帮我

时间:2016-05-17 11:51:19

标签: javascript html canvas

尊敬的开发者。

在写这个问题之前,我的英语很弱。

我正在学习javascript。

但有一个问题就是工作..

我想放下画布的文字..但是..我不知道怎么放下这个文字..

如何修改此代码..?请帮帮我..

 <script type="text/javascript">


 var Text_array = new Array("exam1", "exam2", "exam3");
 var canvas = document.getElementById("myCanvas");
 var ctx = canvas.getContext("2d");
 ctx.font = "30px Arial";
 for (i = 0; i < 3; i++) {
        ctx.fillText(Text_array[i], 100 * i + 10, 50);
 }

function removeText()
{
	var removeText = document.getElementById("txtWord").value;
	var position = Text_array.indexOf(removeText);
	if( position  > -1)
	{
		Text_array.splice(position,1);
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		
		for (i = 0; i < Text_array.length; i++) {
        		ctx.fillText(Text_array[i], 100 * i + 10, 50);
 		}		
	}
}
 </script>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">

    .box {
            position: relative;
            width: 800px;
            height: 600px;
     }
         .box canvas {
                border: 1px solid red;
         }
         .box .control {
                position: relative;
                bottom: 30px;
                z-index: 999;
                width: 100%;
                padding: 5px;
                border: 1px solid blue;
                display: inline-block;
         }
            .box .control input {
                    float: left;
                    width: 45%;
             }
             .box .control button {
                    float: right;
                    width: 45%;
                    color: #000;
                   margin-right: 15px;
             }
 </style>
 </head>
 <body>
 <div class="box">
     
     <canvas id="myCanvas" width="800" height="600">
         Your browser does not support the canvas element.
     </canvas>
     <div class="control">
         <input id="txtWord" type="text" name="word" value=""> 
         <button type="button" class="btn" title="" onclick="javascript:removeText();" >Btn</button>
     </div>
 </div>

 </body>
 </html>

1 个答案:

答案 0 :(得分:0)

使用此代码一切正常,只是我删除了<script type="text/javascript"></script>表单代码,其工作正在......

&#13;
&#13;
 var Text_array = new Array("exam1", "exam2", "exam3");
 var canvas = document.getElementById("myCanvas");
 var ctx = canvas.getContext("2d");
 ctx.font = "30px Arial";
 for (i = 0; i < 3; i++) {
        ctx.fillText(Text_array[i], 100 * i + 10, 50);
 }

function removeText()
{
	var removeText = document.getElementById("txtWord").value;
	var position = Text_array.indexOf(removeText);
	if( position  > -1)
	{
		Text_array.splice(position,1);
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		
		for (i = 0; i < Text_array.length; i++) {
        		ctx.fillText(Text_array[i], 100 * i + 10, 50);
 		}		
	}
}
&#13;
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">

    .box {
            position: relative;
            width: 800px;
            height: 600px;
     }
         .box canvas {
                border: 1px solid red;
         }
         .box .control {
                position: relative;
                bottom: 30px;
                z-index: 999;
                width: 100%;
                padding: 5px;
                border: 1px solid blue;
                display: inline-block;
         }
            .box .control input {
                    float: left;
                    width: 45%;
             }
             .box .control button {
                    float: right;
                    width: 45%;
                    color: #000;
                   margin-right: 15px;
             }
 </style>
 </head>
 <body>
 <div class="box">
     
     <canvas id="myCanvas" width="800" height="600">
         Your browser does not support the canvas element.
     </canvas>
     <div class="control">
         <input id="txtWord" type="text" name="word" value=""> 
         <button type="button" class="btn" title="" onclick="javascript:removeText();" >Btn</button>
     </div>
 </div>

 </body>
 </html>
&#13;
&#13;
&#13;