Skip to content

Commit ebe85c4

Browse files
committed
calculate textangle sin and cos only once
1 parent ffea962 commit ebe85c4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/components/shapes/draw.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
734734
var textPadding = shapeOptions.label.padding;
735735
var shapeType = shapeOptions.type;
736736
var textAngleRad = Math.PI / 180 * actualTextAngle;
737+
var sinA = Math.sin(textAngleRad);
738+
var cosA = Math.cos(textAngleRad);
737739
var xanchor = shapeOptions.label.xanchor;
738740
var yanchor = shapeOptions.label.yanchor;
739741

@@ -788,8 +790,8 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
788790
if(textAngle === 'auto') {
789791
// Set direction to apply padding (based on `yanchor` only)
790792
var paddingDirection = paddingConstantsY[yanchor];
791-
paddingX = -textPadding * Math.sin(textAngleRad) * paddingDirection;
792-
paddingY = textPadding * Math.cos(textAngleRad) * paddingDirection;
793+
paddingX = -textPadding * sinA * paddingDirection;
794+
paddingY = textPadding * cosA * paddingDirection;
793795
} else {
794796
// Set direction to apply padding (based on `xanchor` and `yanchor`)
795797
var paddingDirectionX = paddingConstantsX[xanchor];
@@ -837,8 +839,8 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
837839
// Adjust so that text is anchored at top of first line rather than at baseline of first line
838840
var baselineAdjust = shapeOptions.label.font.size;
839841
var textHeight = textBB.height;
840-
var xshift = (textHeight * shiftFraction - baselineAdjust) * Math.sin(textAngleRad);
841-
var yshift = -(textHeight * shiftFraction - baselineAdjust) * Math.cos(textAngleRad);
842+
var xshift = (textHeight * shiftFraction - baselineAdjust) * sinA;
843+
var yshift = -(textHeight * shiftFraction - baselineAdjust) * cosA;
842844

843845
return { textx: textx + xshift, texty: texty + yshift, xanchor: xanchor };
844846
}

0 commit comments

Comments
 (0)