javascript - Fabric JS _updateObjectsCoords alternative? (Migration issue to 1.7.9) -
update: jsfiddle: https://jsfiddle.net/qanary/915fg6ka/
i trying make curvetext function work (see bottom of post). works fabric.js 1.2.0 when updated fabric.js 1.7.9, curving function locates text in wrong positions when below two actions executed sequentially.
actions : - issue 1
-text group scale changed (i mean dragging corner points mouse change size).
-settext called
fabric js 1.2.0:
fabric js 1.7.9
i debugged , reason _updateobjectscoords in fabricjs because when removed code , 2 actions listed above works fine.
issue 2: time have faced below problem group items not correctly located when adding text canvas first time.
with _updateobjectscoords
without _updateobjectscoords
here function:
var curvedtext = (function() { function curvedtext( canvas, options ){ this.opts = options || {}; ( var prop in curvedtext.defaults ) { if (prop in this.opts) { continue; } this.opts[prop] = curvedtext.defaults[prop]; } this.canvas = canvas; this.group = new fabric.group([], {selectable: this.opts.selectable,radiusval:this.opts.radius,spacingval:this.opts.spacing,textfliping:this.opts.reverse}); this.canvas.add( this.group ) ; this.canvas.centerobject( this.group ); this.settext( this.opts.text ); this.canvas.setactiveobject( this.group ); this.canvas.getactiveobject().setcoords(); } curvedtext.prototype.setobj = function(obj) { this.group=obj; }; curvedtext.prototype.settext = function( newtext ) { this.opts.top=this.group.top; this.opts.left=this.group.left; while ( newtext.length !== 0 && this.group.size() > newtext.length ) { this.group.remove( this.group.item( this.group.size()-1 ) ); } ( var i=0; i<newtext.length; i++ ){ if ( this.group.item(i) === undefined ){ var letter = new fabric.text(newtext[i], { selectable: true }); this.group.add( letter ); } else{ this.group.item(i).text = newtext[i]; } } this.opts.text = newtext; this._setfontstyles(); this._render(); }; curvedtext.prototype._setfontstyles = function() { ( var i=0; i<this.group.size(); i++ ){ if( this.opts.textstylename ) { if( this.opts.textstylename === 'fontfamily' ) { this.group.item(i).setfontfamily( this.opts.fontfamily ); } if( this.opts.textstylename === 'fontcolor' ) { this.group.item(i).setfill( this.opts.fontcolor ); } } else { this.group.item(i).setfontfamily( this.opts.fontfamily ); this.group.item(i).setfill( this.opts.fontcolor ); } this.group.item(i).setfontsize( this.opts.fontsize ); this.group.item(i).fontweight = this.opts.fontweight ; } }; curvedtext.prototype._render = function() { var curangle=0,angleradians=0, align=0; // object may have been moved drag&drop if ( this.group.hasmoved() ) { this.opts.top = this.group.top; this.opts.left = this.group.left; } this.opts.angle = this.group.getangle(); this.opts.scalex = this.group.scalex; this.opts.scaley = this.group.scaley; this.opts.radius = this.group.radiusval; this.opts.spacing = this.group.spacingval; this.opts.reverse = this.group.textfliping; // text align if ( this.opts.align === 'center' ) { align = ( this.opts.spacing / 2) * ( this.group.size() - 1) ; } else if ( this.opts.align === 'right' ) { align = ( this.opts.spacing ) * ( this.group.size() - 1) ; } ( var i=0; i<this.group.size(); i++) { // find coords of each letters (radians : angle*(math.pi / 180) if ( this.opts.reverse ) { curangle = (-i * parseint( this.opts.spacing, 10 )) + align; angleradians = curangle * (math.pi / 180); this.group.item(i).setangle( curangle ); this.group.item(i).set( 'top', (math.cos( angleradians ) * this.opts.radius) ); this.group.item(i).set( 'left', (-math.sin( angleradians ) * this.opts.radius) ); } else { curangle = (i * parseint( this.opts.spacing, 10)) - align; angleradians = curangle * (math.pi / 180); this.group.item(i).setangle( curangle ); this.group.item(i).set( 'top', (-math.cos( angleradians ) * this.opts.radius) ); this.group.item(i).set( 'left', (math.sin( angleradians ) * this.opts.radius) ) ; } } // update group coords this.group._calcbounds(); this.group._updateobjectscoords(); this.group.top = this.opts.top; this.group.left = this.opts.left; this.group.savecoords(); this.canvas.renderall(); }; curvedtext.defaults = { top: 0, left: 0, scalex: 1, scaley: 1, angle: 0, spacing:0, radius:0, text: '', align: 'center', reverse:'', fontsize:16, fontweight: 'normal', selectable: true, fontfamily:'', fontcolor:'black', textstylename:'' }; return curvedtext; })();
this should it, if understood correctly:
just small tweak updatetext() function:
function updatetext() { var original = canvas.getactiveobject(); canvas.remove(original); settext(); canvas.getactiveobject().set({ angle: original.angle, top: original.top, left: original.left, scalex: original.scalex, scaley: original.scaley }).setcoords(); canvas.renderall(); } and finally, here's important jsfiddle updated, https://jsfiddle.net/rekrah/pkj82n4b/.
update (v2) - since did plead in bounty make function work, ;-).
change line:
this.group = new fabric.group([], {selectable: this.opts.selectable,name:'arc',radiusval:this.opts.radius,spacingval:this.opts.spacing,textfliping:this.opts.reverse,itemname:'text'}); to this:
this.group = new fabric.group([], {selectable: this.opts.selectable,name:'arc',radiusval:this.opts.radius,spacingval:this.opts.spacing,textfliping:this.opts.reverse,itemname:'text',originx:'center',originy:'center'}); and little more esthetically pleasing might want to...
change line: canvas = new fabric.canvas('c',);
to this: canvas = new fabric.canvas('c',{centeredscaling: true});
and here's original plunker updated again, https://jsfiddle.net/rekrah/c7cjzkfd/.
this leaves updatetext() function had it.
let me know if have other questions. happy help!





Comments
Post a Comment