如何使用javascript或HTML5在画布上添加文本

时间:2016-06-06 06:55:10

标签: javascript html5 canvas

我试过这样的,

    <canvas id="renderCanvas"></canvas>

   <script>                 
        var canvas = document.getElementById("renderCanvas");                   
        var engine = new BABYLON.Engine(canvas, true);                        
        var createScene = function () {                                
            var scene = new BABYLON.Scene(engine);                
            var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);                
            camera.setTarget(BABYLON.Vector3.Zero());
            camera.attachControl(canvas, true);

            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);                                
            var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);                                
            sphere.position.y = 1;

            var ground = BABYLON.Mesh.CreateGround("ground1", 12, 6, 2, scene);
            return scene;
        }; 

        var scene = createScene();


        engine.runRenderLoop(function () {
            scene.render();
        });



   </script>

我想用javascript或html5等显示文字/部分内容...... 此外,当canvas声明了area。

时,我使用了variable_name.filltext()

1 个答案:

答案 0 :(得分:0)

试试这个

var ctx = canvas.getContext("2d");
ctx.fillText("Hello World",10,50);