We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

draw text to canvas

Home Forums Programming draw text to canvas

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #31863
    kubuz
    Customer

    Hi,
    Can someone please explain a bit more in depth how to draw text on a canvas.
    In the manual its just one line, i am a bit confused here.
    Is there a puzzle available for this ?

    Thanks.
    Best Dennis

    #31870
    GLiFTeK
    Customer

    Hi, Can someone please explain a bit more in depth how to draw text on a canvas. In the manual its just one line, i am a bit confused here. Is there a puzzle available for this ?
    Thanks. Best Dennis

    You can use “fillText“.

    You can also use the “texture from text puzzle” in the material portion of the menu.

    • This reply was modified 2 years, 3 months ago by GLiFTeK.
    #31881
    GLiFTeK
    Customer

    here kubuz…
    use this.. (in either runCode, call JS externalInterface, OR the new script text puzzles…
    (just need to arrange them in execution accordingly)

    var canvasTex = v3d.puzzles.canvasTextures['my_canvas'];
    var canvas = canvasTex.image;
    var ctx = canvas.getContext("2d");
    
    ctx.font = "30px Arial";
    ctx.fillText("Hello World", 10, 50); 
    canvasTex.needsUpdate = true;

    with the according canvas replacement puzzles seen in the user manual setup here..
    v

    :good:

    #32139
    kubuz
    Customer

    Hi Guys,

    @Gliftek
    , many thanks for your solution earlier. You helped me a lot.
    By the time I figured this out myself, I would be grey.
    Thanks.

    So I managed to draw text on a canvas. Struggled a bit with the custom font part. And then I noticed there was a piece of puzzle for that.

    Now I have some strange behaviour.
    If I preview the app local, it works like a charm. But, when i upload it to the Verge CDN, it doesn’t load the font.

    Image of the working app

    The link to the app on CDN

    Does anyone have a clue ?

    #32145

    Hmm.. when I visit your link it loads exactly like on your screenshot (that is with nice font). Can you try again – may be it was cache issue (by default, the apps on Verge3D Network are refreshed only after 15 min)

    Chief 3D Verger | LinkedIn | Twitter

    #32149
    kubuz
    Customer

    It must be some cache issue. Thanks a lot! :bye:

    #32158

    :good:

    Chief 3D Verger | LinkedIn | Twitter

    #32199
    kubuz
    Customer

    Hi Guys,
    Looks like every milestone has some issues :-)
    Now, i have my font in place, but for some odd reason it doesnt update after changing the text….

    First i tought, I need to update in the script. like on How-to-update-things.html but that doesn’t change anything. The string is passed to the script correct.
    Does anyone have a clue what i miss ? It must be something simple.

    Puzzle

    function prepareExternalInterface(app) {
        app.ExternalInterface.myJSFunction = function(myText) {
            //window.addEventListener("load", function(){
                var canvasTxt = window.canvasTxt.default;
                var canvasTex = v3d.puzzles.canvasTextures['my_canvas'];
                var canvas = canvasTex.image;
                var ctx = canvas.getContext("2d");
                var txt = myText;
                console.log(txt);
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.restore();
                canvasTxt.fontSize = 80;
                ctx.fillStyle = '#0234d6'
                canvasTxt.font = 'BacktoBlack'
                canvasTxt.drawText(ctx,txt,100,100,800,800);
                ctx.save();
                ctx.needsUpdate = true;
                //canvasTxt.drawText(ctx,txt,x,y,width,height);
            //});
        };
    };
    #32202

    cannot it be that the line that draws text is commented out?
    //canvasTxt.drawText(ctx,txt,x,y,width,height);

    Chief 3D Verger | LinkedIn | Twitter

    #32203
    kubuz
    Customer

    Hi Yuri,
    Its just a placholder with the parameters as a comments. 2 Lines up we have the one that executes.

    #32204

    Got it! May be the issue lies in using canvasTxt rather than ctx (which is associated with the material).

    Also can you try the following method which looks simpler?

    https://www.w3schools.com/graphics/canvas_text.asp

    Chief 3D Verger | LinkedIn | Twitter

    #32234
    kubuz
    Customer

    Hi Yuri,
    Thanks a lot pal, sometimes the answer is so plain simple that I can’t see it anymore. I use the Canvas TXT library to center and multiline texts.
    I keep this tread updated for later reference.

    Thanks again. You guys are awesome. B-)

    #32240
    GLiFTeK
    Customer

    did you include a “load font” puzzle in your initialization tab in your puzzles editor for your specific font?

    doesn’t look like you’re using the “texture from text” puzzle, which needs that to function, but maybe referencing the font specifically might help with the issue.

    :yes:

    EDIT: oops.. look like you got it! :good:

    • This reply was modified 2 years, 3 months ago by GLiFTeK.
    #32242
    kubuz
    Customer

    did you include a “load font” puzzle in your initialization tab in your puzzles editor for your specific font?

    Jeah, i did. I am Beginning to understand how the whole canvas stuff works.
    Got a nice big project for it in the near future

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.