Textbox HitTest fixed, new GetTextboxPoints optimised

This commit is contained in:
Ivan 2022-07-13 23:40:47 +05:00
parent dd9cd24b19
commit 43a64016f2
2 changed files with 22 additions and 20 deletions

View File

@ -117,7 +117,7 @@ BaseVertex.prototype.HitTest = function (pos)
var lineFinish1 = relativePos.add(new Point(1000, 0)); var lineFinish1 = relativePos.add(new Point(1000, 0));
var lineFinish2 = relativePos.add(new Point(-1000, 0)); var lineFinish2 = relativePos.add(new Point(-1000, 0));
var pointsVertex1 = GetPointsForShape(shape, this.model.diameter + width); var pointsVertex1 = GetPointsForShape(shape, this.model.diameter + width, this.mainText);
pointsVertex1.push(pointsVertex1[0]); pointsVertex1.push(pointsVertex1[0]);
var hitNumber1 = 0; var hitNumber1 = 0;

View File

@ -70,32 +70,34 @@ function GetPentagonPoints(diameter)
function GetTextboxPoints(diameter, text) function GetTextboxPoints(diameter, text)
{ {
var res = []; var res = [];
var width = diameter;
var height = diameter;
if (text)
{
var tempContext = document.createElement('canvas').getContext('2d'); var tempContext = document.createElement('canvas').getContext('2d');
tempContext.font = "bold " + MainTextFontSize + DefaultFont; tempContext.font = "bold " + MainTextFontSize + DefaultFont;
var textWidth = tempContext.measureText(text).width + diameter / 2; width = tempContext.measureText(text).width + diameter / 2;
}
var height = diameter; res.push(new Point(-width / 2, -height / 2));
res.push(new Point(-textWidth / 2, -height / 2)); res.push(new Point(width / 2, -height / 2));
res.push(new Point(textWidth / 2, -height / 2)); res.push(new Point(width / 2, height / 2));
res.push(new Point(textWidth / 2, height / 2)); res.push(new Point(-width / 2, height / 2));
res.push(new Point(-textWidth / 2, height / 2));
return res; return res;
} }
function GetPointsForShape(shape, baseGraph) function GetPointsForShape(shape, diameter, text=null)
{ {
var pointsVertex1 = null;
var diameter = baseGraph.model.diameter;
switch (parseInt(shape)) switch (parseInt(shape))
{ {
case VertexSquareShape: pointsVertex1 = GetSquarePoints(diameter); break; case VertexSquareShape: return GetSquarePoints(diameter); break;
case VertexTriangleShape: pointsVertex1 = GetTrianglePoints(diameter); break; case VertexTriangleShape: return GetTrianglePoints(diameter); break;
case VertexPentagonShape: pointsVertex1 = GetPentagonPoints(diameter); break; case VertexPentagonShape: return GetPentagonPoints(diameter); break;
case VertextTextboxShape: pointsVertex1 = GetTextboxPoints(diameter, baseGraph.mainText); break; case VertextTextboxShape: return GetTextboxPoints(diameter, text); break;
default: return null; break;
} }
return pointsVertex1;
} }
function GetSizeForShape(shape, diameter) function GetSizeForShape(shape, diameter)
@ -106,8 +108,8 @@ function GetSizeForShape(shape, diameter)
case VertexTriangleShape: return diameter * 1.5; break; case VertexTriangleShape: return diameter * 1.5; break;
case VertexPentagonShape: return diameter * 1.2; break; case VertexPentagonShape: return diameter * 1.2; break;
case VertextTextboxShape: return diameter; break; case VertextTextboxShape: return diameter; break;
default: return diameter; break;
} }
return diameter;
} }
function BaseVertexStyle() function BaseVertexStyle()
@ -342,7 +344,7 @@ BaseVertexDrawer.prototype.DrawShape = function(baseGraph)
} }
else else
{ {
var points = GetPointsForShape(this.currentStyle.shape, baseGraph); var points = GetPointsForShape(this.currentStyle.shape, baseGraph.model.diameter, baseGraph.mainText);
this.context.moveTo(baseGraph.position.x + points[points.length - 1].x, baseGraph.position.y + points[points.length - 1].y); this.context.moveTo(baseGraph.position.x + points[points.length - 1].x, baseGraph.position.y + points[points.length - 1].y);