怎么在Canvas中输入文本
2个回答
展开全部
canvas 中可以用 fillText() 绘出文字。
例如:
<html><body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "20px Georgia";
ctx.fillText("Hello World!", 10, 50);
ctx.font = "30px Verdana";
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
ctx.fillStyle = gradient;
ctx.fillText("Big smile!", 10, 90);
</script>
<p><strong>Note:</strong> The canvas tag is not supported in Internet
Explorer 8 and earlier versions.</p>
</body>
</html>
====
如果想插入显示一个文件或网页,可以用 <iframe>, 而不是用 画布<canvas>,
例如:
<iframe src="这里写网页的URL"></iframe>
例如:
<html><body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "20px Georgia";
ctx.fillText("Hello World!", 10, 50);
ctx.font = "30px Verdana";
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
ctx.fillStyle = gradient;
ctx.fillText("Big smile!", 10, 90);
</script>
<p><strong>Note:</strong> The canvas tag is not supported in Internet
Explorer 8 and earlier versions.</p>
</body>
</html>
====
如果想插入显示一个文件或网页,可以用 <iframe>, 而不是用 画布<canvas>,
例如:
<iframe src="这里写网页的URL"></iframe>
2017-04-01
展开全部
在canvas类里调用textbox
/**
* TextBox's instance
*/
private TextBox inputName = null;
/**
* 「cancel」Command
* command in TextBox
*/
private Command cCancel = new Command("cancel", Command.CANCEL, 1);
/**
* 「ok」Command
* command in TextBox
*/
private Command cOK = new Command("ok", Command.OK, 1);
/**
* 保存在TextBox中已输入的值
*/
private String name = "";
/**
* 调用TextBox
* The method disposes the key pressing action
* the code of pressed key
* @param key
*/
public void keyPressed(int key) {
if (key == Canvas.FIRE) {
inputName = new TextBox("请输入姓名", name, 8, TextField.ANY);
inputName.addCommand(cCancel);
inputName.addCommand(cOK);
inputName.setCommandListener(this);
Display.getDisplay(BustRoyal.instance).setCurrent(inputName);
//其中BustRoyal.instance为MIDlet的实例
}
}
/**
* TextBox中的command的处理
*/
public void commandAction(Command c, Displayable d) {
if (c == cCancel) {
Display.getDisplay(BustRoyal.instance).setCurrent(this);
} else if (c == cOK) {
name = inputName.getString().trim();
Display.getDisplay(BustRoyal.instance).setCurrent(this);
}
}
/**
* TextBox's instance
*/
private TextBox inputName = null;
/**
* 「cancel」Command
* command in TextBox
*/
private Command cCancel = new Command("cancel", Command.CANCEL, 1);
/**
* 「ok」Command
* command in TextBox
*/
private Command cOK = new Command("ok", Command.OK, 1);
/**
* 保存在TextBox中已输入的值
*/
private String name = "";
/**
* 调用TextBox
* The method disposes the key pressing action
* the code of pressed key
* @param key
*/
public void keyPressed(int key) {
if (key == Canvas.FIRE) {
inputName = new TextBox("请输入姓名", name, 8, TextField.ANY);
inputName.addCommand(cCancel);
inputName.addCommand(cOK);
inputName.setCommandListener(this);
Display.getDisplay(BustRoyal.instance).setCurrent(inputName);
//其中BustRoyal.instance为MIDlet的实例
}
}
/**
* TextBox中的command的处理
*/
public void commandAction(Command c, Displayable d) {
if (c == cCancel) {
Display.getDisplay(BustRoyal.instance).setCurrent(this);
} else if (c == cOK) {
name = inputName.getString().trim();
Display.getDisplay(BustRoyal.instance).setCurrent(this);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询