Created
November 9, 2017 16:21
-
-
Save heolin/057fc9d3fb7b1462dc0d895b13455c30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <style type="text/css"> | |
| html, | |
| body { | |
| margin: 0; | |
| overflow: hidden; | |
| height: 100%; | |
| } | |
| /* Scale canvas with resize attribute to full size */ | |
| canvas[resize] { | |
| width: 100%; | |
| height: 100%; | |
| } | |
| </style> | |
| <script type="text/javascript" src="js/paper.js"></script> | |
| <script type="text/javascript"> | |
| function manhattanDistance(point1, point2) { | |
| return Math.abs(point1.x - point2.x) + Math.abs(point1.y - point2.y); | |
| } | |
| class Line { | |
| constructor(point1, point2, hascircle1=true, hascircle2=true, reversed=false) { | |
| this.point1 = point1; | |
| this.point2 = point2; | |
| this.hascircle1 = hascircle1; | |
| this.hascircle2 = hascircle2; | |
| this.centerPoint = this.getCenterPoint(reversed); | |
| this.path = new Path(); | |
| this.updatePath(); | |
| } | |
| getCenterPoint(reversed) { | |
| var p1 = this.point1; | |
| var p2 = this.point2; | |
| var centerPoint; | |
| if (reversed) { | |
| if (p1.y < p2.y) | |
| centerPoint = new Point(p2.x, p1.y); | |
| else | |
| centerPoint = new Point(p1.x, p2.y); | |
| } else { | |
| if (p1.y > p2.y) | |
| centerPoint = new Point(p2.x, p1.y); | |
| else | |
| centerPoint = new Point(p1.x, p2.y); | |
| } | |
| return centerPoint; | |
| } | |
| updatePath() { | |
| this.path.strokeColor = 'black'; | |
| this.path.add(this.point1); | |
| this.path.add(this.centerPoint); | |
| this.path.add(this.point2); | |
| this.path.strokeWidth = 2; | |
| if (this.hascircle1) { | |
| this.circle1 = new Path.Circle(this.point1, 3); | |
| this.circle1.fillColor = 'black'; | |
| } | |
| if (this.hascircle2) { | |
| this.circle2 = new Path.Circle(this.point2, 3); | |
| this.circle2.fillColor = 'black'; | |
| } | |
| } | |
| } | |
| class Connection { | |
| constructor(point1, point2, point3, reversed1=false, reversed2=true, yOffset=10) { | |
| this.point1 = point1; | |
| this.point2 = point2; | |
| this.point3 = point3; | |
| this.reversed1 = reversed1; | |
| this.reversed2 = reversed2; | |
| this.yOffset = yOffset; | |
| this.updatePath(); | |
| } | |
| updatePath() { | |
| this.line1 = new Line(this.point1, this.point2, true, true, this.reversed1); | |
| var point4 = this.line1.centerPoint.add(new Point(0, -this.yOffset)); | |
| this.line2 = new Line(this.point3, point4, true, false, this.reversed2); | |
| } | |
| } | |
| paper.install(window); | |
| window.onload = function() { | |
| paper.setup('myCanvas'); | |
| //var line = new Line(new Point(200, 200), new Point(350, 400)); | |
| var connection = new Connection(new Point(300, 100), new Point(450, 150), new Point(100, 200), false, true); | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <canvas id="myCanvas" resize></canvas> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment