简体   繁体   中英

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.

Try this jsFiddle

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

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