Text 如何将文本添加到画布中
我对canvas fillText函数有问题。我有以下代码:Text 如何将文本添加到画布中,text,canvas,Text,Canvas,我对canvas fillText函数有问题。我有以下代码: scene.shapes['shop1'] = new Shape(); var ps1 = scene.shapes['shop1'].points; // for convenience ps1[0] = new Point(1024, 66, 10); // left bottom ps1[1] = new Point(694, 66, 10); // r
scene.shapes['shop1'] = new Shape();
var ps1 = scene.shapes['shop1'].points; // for convenience
ps1[0] = new Point(1024, 66, 10); // left bottom
ps1[1] = new Point(694, 66, 10); // right bottom
ps1[2] = new Point(694, 466, 10); // right top
ps1[3] = new Point(1024, 466, 10); // left top
// Background
scene.shapes['shop1'].polygons.push(new Polygon(
[ps1[0], ps1[1], ps1[2], ps1[3] ],
new Point(0, 0, 1),
true /* double-sided */,
Polygon.SOLID,
[177, 216, 249]
));
scene.shapes['shop1Header'] = new Shape();
var ps1h = scene.shapes['shop1Header'].points; // for convenience
ps1h[0] = new Point(1024, 400, 11); // left bottom
ps1h[1] = new Point(694, 400, 11); // right bottom
ps1h[2] = new Point(694, 466, 11); // right top
ps1h[3] = new Point(1024, 466, 11); // left top
// Background
scene.shapes['shop1Header'].polygons.push(new Polygon(
[ps1h[0], ps1h[1], ps1h[2], ps1h[3] ],
new Point(0, 0, 1),
true /* double-sided */,
Polygon.SOLID,
[175, 175, 175]
));
var x = -1024;
var y = -500;
ctx.font = '60pt Calibri';
ctx.lineWidth = 3;
// fill color
ctx.fillStyle = 'blue';
ctx.fillText('Hello World!', x, y);
但它不创建文本,只显示商店。你知道怎么解决这个问题吗?非常感谢。x和y的值为负值,因此您在画布外绘制。如果将x设置为0,y设置为0+字体大小,它将在左上角绘制文本 编辑:您需要将字体大小添加到y
ctx.font = "60px Calibri"; // Use pixels instead of points
ctx.fillText("Hello World", 0, 60);
这个负值不是问题,因为我有相机z位置=2048,它必须显示。我试着把它改成O,O,但还是没有。对此我很抱歉。我更正了我的答案。二维画布上没有z位置。如何分配ctx?这是完整的代码。如何分配ctx:canvas=document.getElementById(“cv”);ctx=canvas.getContext(“2d”);还有z位置的摄像机定位:scene.camera.x=0;scene.camera.y=0;scene.camera.z=2048;如果我使用了您之前发送的代码来渲染文本,而没有使用此代码来渲染画布,那么它可以正常工作。但是如果我使用了这个代码,它就不会。