How to Create a luminous Loop

Source: Internet
Author: User

First, let's take a look at the running effect of the luminous link:

This demo mainly contains three technical points: one is how to make the entire loop glow when selecting a link, and the other is how to draw a curve link with an arrow; third, how to set the link style to make the overall control.
1. How to obtain all links of the entire loop and make it glow
In the past, we had customer support and handled similar problems. At that time, the solution was to get the current link, and then get the fromnode and tonode of the link through continuous traversal, then obtain the node link to obtain all links and nodes in the loop. The disadvantage of this implementation is that it is complicated to implement through a large number of traversal. The solution in this article is to set a client attribute when creating a link, and when selecting a loop, read the client property directly and traverse it once. The code for creating a loop is as follows:

function createCircuit(nodes, linename, flag) {    if (flag) {        var startNode = nodes[0];        var stopNode = nodes[nodes.length - 1];        var link = new CLink(stopNode, startNode);        link.setStyle(‘link.type‘, "extend.left");//        link.setName(linename);        link.setClient(‘linename‘, linename);        box.add(link);    }    for (var i = 0; i < nodes.length - 1; i++) {        var fromNode = nodes[i];        var toNode = nodes[i + 1];        var link = new CLink(fromNode, toNode);//        link.setName(linename);        link.setClient(‘linename‘, linename);        if (i == 0) {            link.setStyle(‘link.type‘, "orthogonal.H.V");//orthogonal\        } else if (i == 1) {            link.setStyle(‘link.type‘, "orthogonal.V.H");//orthogonal\        } else if (i == 2) {            link.setStyle(‘link.type‘, "orthogonal.V.H");//orthogonal\        }//       link.setStyle(‘link.type‘, "orthogonal");//orthogonal\        box.add(link);    }}

 

 

2. Custom Implementation of curve link with arrows
This mainly investigates the canvas rendering capability. The core code is as follows:

cx = cx - Math.cos(angle) * radius;        cy = cy - Math.sin(angle) * radius;        ctx.lineWidth = 4;        ctx.beginPath();        ctx.moveTo(p1.x, p1.y - h1 / 2);        ctx.quadraticCurveTo(cx, cy, p2.x, p2.y - h2 / 2);        ctx.stroke();        ctx.closePath();       //draw arrow        this.drawArrow(ctx, p2.x, p2.y - h2 / 5 * 3, cx, cy+20, 1, 2, Math.PI / 8, 10);

 

3. Set the link Style
Twaver links support many styles. For common types, refer to this document. This article mainly monitors the mousemove event and constantly calculates the relative positions between nodes during node dragging to determine the link type, and constantly refresh.

The core code is as follows:

function refreshLinkType() {    box.forEach(function (element) {        if (element instanceof  twaver.Link) {            var fromNode = element.getFromNode();            var toNode = element.getToNode();            var nextLinks = toNode.getLinks();            var nextNode;            nextLinks.forEach(function (element) {                if (element.getToNode() !== toNode) {                    nextNode = element.getToNode();                }            });            var fromPoint = fromNode.getCenterLocation();            var toPoint = toNode.getCenterLocation();            var nextPoint;            if (nextNode) {                nextPoint = nextNode.getCenterLocation();            }            //compute the relationship of location between fromNode and toNode            if (fromPoint.x < toPoint.x && fromPoint.y < toPoint.y) {                element.setStyle(‘link.type‘, "orthogonal.V.H");            } else if (fromPoint.x < toPoint.x && fromPoint.y > toPoint.y) {                element.setStyle(‘link.type‘, "orthogonal.V.H");            } else if (fromPoint.x > toPoint.x && fromPoint.y < toPoint.y) {                element.setStyle(‘link.type‘, "orthogonal.V.H");            } else if (fromPoint.x > toPoint.x && fromPoint.y > toPoint.y) {                element.setStyle(‘link.type‘, "orthogonal");            }            if (nextPoint) {                if (toPoint.x > fromPoint.x && toPoint.x > nextPoint.x) {                    if (toPoint.y > fromPoint.y && toPoint.y < nextPoint.y) {                        element.setStyle(‘link.type‘, "orthogonal.H.V");                    } else {                        element.setStyle(‘link.type‘, "orthogonal.V.H");                    }                }            }        }    });}

 

 

How to Create a luminous Loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.