10.折线连接--从零起步实现基于Html5的WEB设计器Jquery插件(含源码)

摘要:
上一章描述了如何通过曲线(Bezier曲线)连接两个组件。通常,在实际应用中,Bezier曲线被广泛应用于数据流图和思维导图中,许多如组织图都是通过虚线连接的。本文描述了如何在上一个组件的基础上快速连接两个组件之间的虚线:上一个示例使用复选框来指示是否绘制线。现在添加了一条线,因此您需要修改它以将三种状态之一标识为选择/曲线/多段线。index.html中的代码片段如下:˂ul class=“lineTyp

前面章节已讲到如何在两个组件间通过曲线(贝塞尔曲线)进行连接,一般在实际应用中,贝塞尔曲线在数据流图、思维导图中应用比较多,许多如组织架构图等通过折线连接,本文在之前的基础上如何快速实现两个组件间的折线连接:

10.折线连接--从零起步实现基于Html5的WEB设计器Jquery插件(含源码)第1张

之前示例是用checkbox来指示是否画线状态,现在增加了一种线条所以需要修改一下用三种状态来识别是选择/曲线/折线之一,index.html中代码片断如下:

<ul class="lineTypeUL"><li class="arrowli active"></li><li class="lineCurve"></li><li class="linePoly"></li></ul>
//样式定义如下:
    <style>
        .lineTypeUL{
            display:inline-block;
            text-align:center;
            border:1px solid lightgray;
            border-radius: 3px;
        }
        .lineTypeUL li{
            display:inline-block;
            background-image: url("icons.png");
            18px;
            height:18px;
            margin:2px;
        }
        .lineTypeUL li:hover{
            background-color:lightgray;
        }
        .lineTypeUL .active{
            background-color:lightgray;
        }
        .arrowli{
            background-position:0 0px;
        }
        .lineCurve{
            background-position: -18px 0px;
        }
        .linePoly{
            background-position:-36px 0px;
        }
    </style>
//事件代码:
        $(".lineTypeUL li").each(
            function(){
                $(this).on("click", function () {
                    $(".lineTypeUL li").removeClass("active");
                    view.setLineStatus(this.className);
                    $(this).addClass("active");
                })
            }
        )

对于前端开发来说必须掌握的一个技能就是spirit图标,一个网站用到的图标通过合并在一个文件中,能够减少网站资源请求的次数(虽然是异步并行请求),提高效率,注意background的用法。

在visualDesigner.js中,增加一个PolyLine类,同BezierLine的写法:

function PolyLine() {
        this.properties={};
        this.properties.typeName = "折线";
        this.properties.strokeWidth = 2;
        this.properties.strokeColor = 'red';      
    }
    PolyLine.prototype = $.extend({}, Component.prototype);
    PolyLine.prototype = $.extend(PolyLine.prototype, {
        render: function (options) {

            this.properties=$.extend(this.properties,options)
            this.properties.x = Math.min(this.properties.sxy.x, this.properties.txy.x);
            this.properties.y = Math.min(this.properties.sxy.y, this.properties.txy.y);
            this.properties.width = Math.abs(this.properties.txy.x - this.properties.sxy.x);
            this.properties.height = Math.abs(this.properties.txy.y - this.properties.sxy.y);

            this.group=new paper.Group();
            this.properties.x=Math.min(this.properties.sxy.x,this.properties.txy.x);
            this.properties.y=Math.min(this.properties.sxy.y,this.properties.txy.y);
            this.properties.width = Math.abs(this.properties.txy.x - this.properties.sxy.x);
            this.properties.height = Math.abs(this.properties.txy.y - this.properties.sxy.y);

            if (this.properties.targetType=="left" || this.properties.targetType=="right")
            {
                if (this.properties.mxy1==undefined && this.properties.mxy2==undefined){
                    this.properties.mxy1=[this.properties.sxy.x+(this.properties.txy.x-this.properties.sxy.x)/2,this.properties.sxy.y];
                    this.properties.mxy2=[this.properties.sxy.x+(this.properties.txy.x-this.properties.sxy.x)/2,this.properties.txy.y];
                }
                else
                {
                    this.properties.mxy1[1]=this.properties.sxy.y;
                    this.properties.mxy2[1]=this.properties.txy.y;
                }
            }
            else
            {
                if (this.properties.mxy1==undefined && this.properties.mxy2==undefined){
                    this.properties.mxy1=[this.properties.sxy.x,(this.properties.txy.y-this.properties.sxy.y)/2+this.properties.sxy.y];
                    this.properties.mxy2=[this.properties.txy.x,(this.properties.txy.y-this.properties.sxy.y)/2+this.properties.sxy.y];
                }
                else
                {
                    this.properties.mxy1[0]=this.properties.sxy.x;
                    this.properties.mxy2[0]=this.properties.txy.x;
                }
            }
            
            this.group=new paper.Group();
            var me = this;
            var drag = false;
            var line = new paper.Path();
            line.strokeWidth = 2;

            line.strokeColor = this.properties.strokeColor;    
            line.add(this.properties.sxy);
            line.add(this.properties.mxy1);
            line.add(this.properties.mxy2);
            line.add(this.properties.txy);
            //BezierArrow(line,targetType,this.properties.txy.x, this.properties.txy.y);
            this.group.addChild(line);

            //this.group.translate(this.properties.x, this.properties.y);
            return this;
        }
    });

同时修改createLine方法

    VisualDesigner.prototype.createLine= function (typeName, options) {
        if (!options.id)
            options.id = this.createId(); //为元素增加id属性
        var element = null;
        switch (typeName) {
            case "曲线":
                element = new BezierLine().init().render(options);
                break;
            case "折线":
                element=new PolyLine().init().render(options);
                break;
        }
        this.lines[element.properties.id] = element;
        element.designer = this;

    } 

增加一创建线条的分支,当然还需要修改当前画线类型和画线结束的代码

    VisualDesigner.prototype.setLineStatus = function (status) {
        if (status=="arrowli")
            this.lining = false;
            else
            {
                this.lining=true;
                if (status=="lineCurve")
                    this.lineType="曲线";
                else if (status="linePoly")
                    this.lineType="折线";
            }
    }
。。。。
        dragEnd:function(co,pos)
        {
            var xy = co.node.getConnectorCenterByPos(pos); //获取当前鼠标位置处连接点的中央坐标
            if (this.line !== null  ) {
                if (this.start.node.properties.id!=co.node.properties.id){
                    this.designer.createLine(this.designer.lineType,{sourceType:this.start.node.getConnectorDirection(this.startPos),targetType:co.node.getConnectorDirection(pos),source:this.start.node.properties.id,target:co.node.properties.id,sxy:this.startPos,txy:xy});
                }
                this.line.remove();

            }
            this.start=null; //清除画线状态,等待重新画线
            this.startPos=null;
            
        },

至此就大功告成了,得益于之前我们以OOP的思路构建的框架,在扩展新的组件或连线时,代码变得如些精简。

同学们快动手试试增加更多的连线方式吧。

源代码:sample.1.8.rar

直接运行查看

(本文为原创,在引用代码和文字时请注明出处)

免责声明:文章转载自《10.折线连接--从零起步实现基于Html5的WEB设计器Jquery插件(含源码)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇嵌套矩形(动态规划)MacBookPro磁盘空间不够下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Druid连接池的简单使用

感谢原文作者:chenhongyong原文链接:https://www.cnblogs.com/chy18883701161/p/12594889.html更多请查阅阿里官方API文档:https://github.com/alibaba/druid/wiki 目录 Druid简介 druid的优点 Druid的使用 方式一:纯代码方式 方式二:配置...

移动端-手机端-日历选择控件(支持Zepto和JQuery)

一. 效果图 二. 功能说明   1. 支持切换年份,月份。   2. 支持点击选中日期,也可以点击确定选择日期。 三. 使用方法   1. 添加Input   在你的页面中添加Input输入框。可以再html里,也可以JS动态加载。   我这里使用的时Input-group形式的输入框,是JS加载的。   一般使用的话,直接写个input输入框就行。...

jquery/js记录点击事件,单击次数加一,双击清零

目的:点击按钮,点击后在网页上显示点击次数,双击清零 实现:js或者jquery 代码如下: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE HTML> <html&g...

Java读取properties配置文件时,中文乱码解决方法

Java读取properties配置文件时,中文乱码解决方法 转自:http://pig345.iteye.com/blog/725974 碰到了用java.util.Properties读取中文内容(UTF-8格式)的配置文件,发生中文乱码的现象,   Java代码   Properties prop=new Properties(); ...

Java读取Properties配置文件

1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集。不过Properties的键和值都是字符串类型。 2.Properties中的主要方法 (1)load(InputStream inStream) 此方法可以从.properties属性文件对应的文件...

【实战HTML5与CSS3 第二篇】绚丽的快速导航!

目录 【实战HTML5与CSS3 第一篇】初探水深,美丽的导航,绚丽的图片爆炸!! 【实战HTML5与CSS3 第二篇】绚丽的快速导航! 【实战HTML5与CSS3 第三篇】我第一个HTML5网页诞生了(提供源码) 前言 今天9点就起来了,因为下午出去有个聚会,所以就早点起来进行,否则这个进度有点吃紧啊,昨天初略的完成了导航以及爆炸的图片,这里来回顾下:...