Matter.js: placing text or images on the canvas behind objects
Asked Answered
E

4

8

I'm wanting to place font or static images onto the canvas but not sure what the best approach is using Matter.js. Right now, for images, i'm just creating a body with a size of '0' and putting the image url in the render.sprite.texture property. Seems to do the trick but is there a different/better way of putting static images on the canvas?

Also, I am putting text onto the canvas using the 'afterRender' event near the top of my scripts, before anything else is created/drawn:

_sceneEvents.push(
    Events.on(_engine, 'afterRender', function(event) {
        var context = _engine.render.context;
        context.font = "45px 'Cabin Sketch'";
        context.fillText("THROW OBJECT HERE", 150, 80);
    });
);

Only problem is the text keeps getting drawn on top so my draggable objects keep going behind the text. I just want the text to stay in the background, like a static image, but I figured drawing the font onto the canvas might be better than having to make the user download another image. Any help?

Experimental answered 25/9, 2014 at 16:16 Comment(1)
Is the canvas just a way to get text onto the body? If so, you might be pushing the prototype render beyond its intent and using the DOM as your renderer might be a better approach. See Matter.js Text inside a rectangleBarrelhouse
E
5

The renderer included with Matter.js is really only intended for demonstration, so the best thing to do is make a copy of the example Render.js and then implement all of your rendering there (where Render.world is the entry method called on each tick).

Change the object name to something else like CustomRenderer, then pass your render class through in the Engine.create options:

var engine = Engine.create({
    render: {
        element: element,
        controller: CustomRenderer
    }
});
Electroplate answered 12/10, 2014 at 20:13 Comment(0)
S
4

A little late, but the documentation for this shows you had the correct method in adding the sprite to the renderer:

    let head = Bodies.circle(450, 50, 17, {
                render: {
                    sprite: {
                        texture: './img/head.png'
                    }
                }}
            );

https://github.com/liabru/matter-js/blob/master/examples/sprites.js

(No idea about the text part of the question, yet)

Scofield answered 11/2, 2016 at 6:24 Comment(0)
L
2

this is how you could return text string in a function as base image

createImage($string) {

    let drawing = document.createElement("canvas");

    drawing.width = '150'
    drawing.height = '150'

    let ctx = drawing.getContext("2d");

    ctx.fillStyle = "blue";
    //ctx.fillRect(0, 0, 150, 150);
    ctx.beginPath();
    ctx.arc(75, 75, 20, 0, Math.PI * 2, true);
    ctx.closePath();
    ctx.fill();
    ctx.fillStyle = "#fff";
    ctx.font = "20pt sans-serif";
    ctx.textAlign = "center";
    ctx.fillText($string, 75, 85);
    // ctx.strokeText("Canvas Rocks!", 5, 130);

    return drawing.toDataURL("image/png");
},

// and then e.g. in your sprites

// ...

render: {
  sprite: {
      texture: createImage("Your Text String")
      xScale: 1,
      yScale: 1
  }
}
Lung answered 1/11, 2018 at 20:38 Comment(0)
A
1

The default renderer for Matter.js isn't really intended to be used this way. Most often, you want to use either a custom renderer (like liabu talked about) OR connect Matter to your DOM elements like this post covers.

Appointive answered 15/12, 2022 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.