Maciej Aniol
Maciej Aniol

Reputation: 33

Fabric.js clip text to rectangle

I am using fabric.js to render objects on canvas. When I add text and change it's width, text is beyond selection area. How to clip text to rectangle that represents width and height properties?

EDIT: This is the solution that I found to be working:

var text = 'Some text';        
var textSample = new fabric.Text(text, textDefaults);

textSample.clipTo = function (ctx) {
    ctx.rect(-textSample.width / 2, -textSample.height / 2, textSample.width, textSample.height );
}

canvas.add(textSample);

Where textDefaults is a text properties.

Upvotes: 2

Views: 6731

Answers (1)

Nistor Cristian
Nistor Cristian

Reputation: 1266

Try this jsFiddle

I belive this is what you want. Maybe you forgot canvas.renderAll();

Upvotes: 2

Related Questions