Aktualisieren: JSFiddle: https://jsfiddle.net/Qanary/915fg6ka/
Ich versuche, meine zu machen curveText
Funktionsarbeit (siehe unten in diesem Beitrag). Es funktioniert normalerweise mit fabric.js 1.2.0
jedoch, als ich aktualisiert habe fabric.js
1.7.9, die Curving-Funktion lokalisiert den Text unten an falschen Positionen zwei Aktionen hingerichtet der Reihe nach.
AKTIONEN: – AUSGABE 1
– Textgruppenskalierung wird geändert (ich meine, die Eckpunkte mit der Maus ziehen, um die Größe zu ändern).
-setText aufgerufen
Fabric js 1.2.0:
Stoff js 1.7.9
Ich habe es debuggt und der Grund dafür ist _updateObjectsCoords
in fabricjs, denn als ich es aus dem Code entfernt habe und 2 Aktionen, die ich oben aufgeführt habe, funktioniert es gut.
AUSGABE 2:
Aber dieses Mal bin ich auf das folgende Problem gestoßen, bei dem Gruppenelemente nicht korrekt lokalisiert werden, wenn der Text zum ersten Mal zur Leinwand hinzugefügt wird.
mit _updateObjectsCoords
ohne _updateObjectsCoords
Hier meine Funktion:
var CurvedText = (function() {
function CurvedText( canvas, options ){
this.opts = options || {};
for ( 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 ) );
}
for ( 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() {
for ( 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 with 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) ;
}
for ( 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;
})();
Ich habe JSfiddle ausprobiert und das Beispiel funktioniert gut. Können Sie einen Screenshot Ihrer Ergebnisse aus Fiddle senden?
– ProllyGeek
10. April 2017 um 1:15 Uhr
@ProllyGeek , bitte versuchen Sie die Aktionen, die ich nacheinander erwähnt habe. 1- Text hinzufügen, 2- Skalierung des Textes von den Ecken ändern, 3- Klicken Sie auf Text aktualisieren. Die Screenshots sind bereits in die Frage eingebettet. Vielen Dank.
– HOCH
10. April 2017 um 10:00 Uhr
@HOY Ich habe meine Antwort unten aktualisiert.
– Tim Harker
11. April 2017 um 16:18 Uhr