简体   繁体   中英

Fabric.js: on text submit, create an object for each line

here is my fuction to add text objects on Fabric, is there a way to modify it so it creates several objects (one for each line) if line breaks are detected in input ?

self.addText = function(str) {
        str = str || 'Votre Texte...';

        var object = new FabricWindow.Text(str, self.textDefaults);
        object.id = self.createId();

        self.addObjectToCanvas(object);
};

I assume you have line break \\n in your string. So let's try this

let strArr = str.split('\n');
strArr.forEach(s => {
    let object = new FabricWindow.Text(s, self.textDefaults);
    object.id = self.createId(); //you should probably start using es6 arrow syntax to avoid having to use self

    self.addObjectToCanvas(object);
})

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