简体   繁体   中英

How to add text on the canvas using javascript or HTML5

I have tried like this,

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

I want to display the text/some content using javascript or html5 or etc... Moreover i used variable_name.filltext(),when canvas declared area.

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM