How to add text into canvas

孤人 提交于 2019-12-24 21:18:45

问题


I have problem with canvas fillText function. I have this code :

        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);

But it don´t create the text and only display the shop. Do you know, how to solve this problem? Thank you very much.


回答1:


The value of x and y is negative so you are drawing outside of the canvas. If you set x to 0 and y to 0 + font-size it will draw the text in the top left corner.

EDIT: you need to add the font size to y.

ctx.font = "60px Calibri"; // Use pixels instead of points
ctx.fillText("Hello World", 0, 60);


来源:https://stackoverflow.com/questions/13786836/how-to-add-text-into-canvas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!