0

我对画布 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);   // 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);

但它不创建文本,只显示商店。你知道,如何解决这个问题吗?非常感谢。

4

1 回答 1

2

x 和 y 的值为负数,因此您在画布之外进行绘制。如果将 x 设置为 0 并将 y 设置为 0 + font-size ,它将在左上角绘制文本。

编辑:您需要将字体大小添加到 y。

ctx.font = "60px Calibri"; // Use pixels instead of points
ctx.fillText("Hello World", 0, 60);
于 2012-12-09T11:30:17.467 回答