BS版图形系统 - 画线
一晃两个月过去了,儿子也考了个喜欢的大学,重新,又稳定了下来。
我也重操旧业,编点小程序。时不时发个小博客,自娱自乐一下。
今天处理图元 - 线。
话不多说,直接上代码。
export class TLine extends TTwoPointMeta {
beginArrow: TArrow = new TArrow;
endArrow: TArrow = new TArrow;
Initial() {
this.setClassType(CbwClassType.Line);
super.Initial();
}
RefreshConnectPoints() {
this.newDefaultConnectPoint(1);
let r = this.boundRect;
this.SetConnectPoint(0, r.xCenter, r.yCenter);
}
containBrush(): boolean { return false; }
doDraw(ctx: CanvasRenderingContext2D) {
if (!this.LastPoint.IsValid) return;
if (this.ApplyPen(ctx) != 0) {
ctx.moveTo(this.toCanvasX(this.points[0].x), this.toCanvasY(this.points[0].y));
ctx.lineTo(this.toCanvasX(this.points[1].x), this.toCanvasY(this.points[1].y));
ctx.stroke();
ctx.fillStyle = this.LineColor.toString();
let theta = this.points[0].thetaToPoint(this.points[1]);
this.beginArrow.doDraw(ctx, new TPoint(this.toCanvasX(this.points[0].x), this.toCanvasY(this.points[0].y)), 180 + theta, this.paintBox!.Ratio);
this.endArrow.doDraw(ctx, new TPoint(this.toCanvasX(this.points[1].x), this.toCanvasY(this.points[1].y)), theta, this.paintBox!.Ratio);
}
}
doPaintForSelect(ctx: CanvasRenderingContext2D, idx: number) {
ctx.moveTo(this.toCanvasX(this.points[0].x), this.toCanvasY(this.points[0].y));
ctx.lineTo(this.toCanvasX(this.points[1].x), this.toCanvasY(this.points[1].y));
ctx.stroke();
let theta = this.points[0].thetaToPoint(this.points[1]);
this.beginArrow.doDraw(ctx, new TPoint(this.toCanvasX(this.points[0].x), this.toCanvasY(this.points[0].y)), 180 + theta, this.paintBox!.Ratio);
this.endArrow.doDraw(ctx, new TPoint(this.toCanvasX(this.points[1].x), this.toCanvasY(this.points[1].y)), theta, this.paintBox!.Ratio);
}
get Length(): number {
let result = this.points[0].DistanceTo(this.points[1]);
result = Math.floor(result * 100 + 0.5);
result = result / 100;
return result;
}
set Length(value: number) {
let theta = this.points[0].thetaToPoint(this.points[1]);
this.points[1].x = this.points[0].x + value * Math.cos(theta);
this.points[1].y = this.points[0].y + value * Math.sin(theta);
this.updateBorder();
}
SaveToXmlNode(parentXmlNode: CbwXmlNode): CbwXmlNode {
let thisNode = super.SaveToXmlNode(parentXmlNode);
new TEnum(NArrow, NArrow.None).SaveToXmlNode(thisNode, "beginType", this.beginArrow.type);
if (this.beginArrow.length != 20) thisNode.AddAttribute("beginLength", this.beginArrow.length.toString());
if (this.beginArrow.theta != 30) thisNode.AddAttribute("beginTheta", this.beginArrow.theta.toString());
new TEnum(NArrow, NArrow.None).SaveToXmlNode(thisNode, "endType", this.endArrow.type);
if (this.endArrow.length != 20) thisNode.AddAttribute("endLength", this.endArrow.length.toString());
if (this.endArrow.theta != 30) thisNode.AddAttribute("endTheta", this.endArrow.theta.toString());
return thisNode;
}
LoadFromXmlNode(thisNode: CbwXmlNode): CbwXmlNode {
super.LoadFromXmlNode(thisNode);
this.beginArrow.type = new TEnum(NArrow, NArrow.None).LoadFromXmlNode(thisNode, "beginType");
this.beginArrow.length = thisNode.IntAttributeValueByName("beginLength", "20");
this.beginArrow.theta = thisNode.IntAttributeValueByName("beginTheta", "30");
this.endArrow.type = new TEnum(NArrow, NArrow.None).LoadFromXmlNode(thisNode, "endType");
this.endArrow.length = thisNode.IntAttributeValueByName("endLength", "20");
this.endArrow.theta = thisNode.IntAttributeValueByName("endTheta", "30");
return thisNode;
}
DrawHint(ctx: CanvasRenderingContext2D) {
if (this.currentPointIndex > 0) {
let rect = new Rect().From(this.points[0], this.points[1]);
DrGraph.TCanvas.DrawHint_Length(ctx, this.toCanvasPoint(this.points[1].add(-5, 5)), this.toCanvasPoint(this.points[0].add(-5, 5)), rect.dialogLength);
DrGraph.TCanvas.DrawHint_Angle(ctx, this.toCanvasPoint(this.points[1]), this.toCanvasPoint(this.points[0]))
}
}
}
这种代码都烂大街的,好象也没什么解释的必要,毕竟我也是从网上借鉴过来的。
![BS版图形系统 - 画线_第1张图片](http://img.e-com-net.com/image/info8/cca75b88b07a4f97b0ac8ea2654397e5.jpg)
拿走不谢。