BS版图形系统 - 画线

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]))
        }
    }
}

这种代码都烂大街的,好象也没什么解释的必要,毕竟我也是从网上借鉴过来的。
在这里插入图片描述
拿走不谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值