Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="test"></div>
              
            
!

CSS

              
                	div.tooltip {	
    position: absolute;			
    text-align: center;							
    padding: 2px;				
    font: 12px sans-serif;		
    background: lightsteelblue;	
    border: 0px;		
    border-radius: 8px;
	}
text.circleid{
	fill: lightsteelblue;
	text-size: small;
	}
              
            
!

JS

              
                var margin = {"left": 50, "right": 0, "top": 50, "bottom": 0};
var w = 500 - margin.left - margin.right;
var h = 500 - margin.top - margin.bottom;

var values = [{"x":10, "y":10}, {"x":10, "y":200}, {"x":200, "y":10} ];

var x = d3.scale.linear()
  .range([0, w])
  .domain([d3.min(values, function(d) { return d.x; }), d3.max(values, function(d) { return d.x + 10; })]);

var y = d3.scale.linear()
  .range([0, h])
  .domain([d3.min(values, function(d) { return d.y; }), d3.max(values, function(d) { return d.y + 10; })]);

var svg = d3.select('#test').append('svg')
  .attr("width", w + margin.left + margin.right)
  .attr("height", h + margin.top + margin.bottom)
  .call(d3.behavior.zoom().x(x).y(y).on("zoom", zoom)).on("dblclick.zoom", null);

var div = d3.select("body").append("div")	
    .attr("class", "tooltip")				
    .style("opacity", 0);
	div.append('text')

	var numinput = div.append('input')
			.attr('id', 'lengthValue')
			.attr('type', 'number');

var canvas = svg.append('g')
  .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  

var line = d3.svg.line()
    .x(function(d) { return x(d.x); })
    .y(function(d) { return y(d.y); })
    .interpolate("linear");
	
var drag = d3.behavior.drag()
.on("drag", function(){
  values[parseInt(d3.select(this).attr("id"))].x = x.invert(d3.event.x);
  values[parseInt(d3.select(this).attr("id"))].y = y.invert(d3.event.y);
  
  createTri(values)
    })
.on("dragstart", function(){ 
	d3.event.sourceEvent.stopPropagation();
    d3.select(this).transition().duration(200)
    .attr("fill", "red")})
.on("dragend", function(){ 
    d3.select(this).transition().duration(200)
    .attr("fill", "black")});

function createTri(data){
  div.transition().duration(100).style("opacity", 0);
  
  var triangle = canvas.selectAll("path").data([data]);
    
  triangle.enter().append("path")
    .attr("stroke", "steelblue")
    .attr("stroke-width", 2)
    .attr("fill", "lightsteelblue")
	.attr("id", "svgpath");
  triangle //update
    .attr("d", line(data) + "Z" );
    
  
  var circle = canvas.selectAll('circle').data(data);
  
  circle.enter().append('circle')
  .attr("id", function(d, i){return i})
  .attr("r", 10)
  .call(drag);
  
  circle //update
  .attr("cx", function(d){ return x(d.x)})
  .attr("cy", function(d){ return y(d.y)});
  
  var circleID = canvas.selectAll("text.circleid").data(data);
  
  circleID.enter().append('text')
  .attr('text-anchor', 'middle')
  .attr("class", "circleid")
  .attr('dy', '.35em')
  .style('pointer-events', 'none')
  .text(function(d, i) { return i + 1});
  
  circleID //update
   .attr('x', function(d, i) { return x(d.x)})
   .attr('y', function(d, i) { return y(d.y)}); 
  
  
  var length = canvas.selectAll("text.length").data(data);
  
  length.enter().append('text')
  .attr('text-anchor', 'middle')
  .attr("class", "length")
   
  length //update
   .text(function(d, i) { return lineDistance(data[i], data[loopI(i, data, 1)]);})
   .attr('x', function(d, i) { return x((data[i].x + data[loopI(i, data, 1)].x)/2)})
   .attr('y', function(d, i) { return y((data[i].y + data[loopI(i, data, 1)].y)/2)})
   .on("dblclick", function(d, i) {		
								div.transition()		
									.duration(200)		
									.style("opacity", .9)			
									.style("left", (d3.event.pageX + 15) + "px")		
									.style("top", (d3.event.pageY - 30) + "px");
									numinput
									.attr('onChange', 'changeLength(' + i + ', ' + loopI(i, data, 1) + ')');
									//.attr('value', lineDistance(data[i], data[loopI(i, data, 1)]));
									div.select('text')
										.text( lineDistance(data[i], data[loopI(i, data, 1)]));
									document.getElementById("lengthValue").value = lineDistance(data[i], data[loopI(i, data, 1)]);
									document.getElementById("lengthValue").focus();
								});;
   
  var angle = canvas.selectAll("text.angle").data(data);
  
  angle.enter().append('text')
  .attr('text-anchor', 'middle')
  .attr("class", "angle");
   
  angle //update
   .text(function(d, i) { return getAngle(i, data) + "\u00B0" ;})
   .attr("x", function(d){ return x(d.x)})
   .attr("y", function(d){ return y(d.y) - 15})
   .on("dblclick", function(d, i) {		
								div.transition()		
									.duration(200)		
									.style("opacity", .9)			
									.style("left", (d3.event.pageX + 15) + "px")		
									.style("top", (d3.event.pageY - 30) + "px");
									numinput
									.attr('onChange', 'changeAngle(' + i + ', ' + loopI(i, data, 1) + ', ' + getAngle(i, data) + ')');
									//.attr('value', lineDistance(data[i], data[loopI(i, data, 1)]));
									div.select('text')
										.text( getAngle(i, data) + "\u00B0");
									document.getElementById("lengthValue").value = getAngle(i, data);
									document.getElementById("lengthValue").focus();
								});;
   var area = canvas.selectAll("text.area").data([data]);
  
  area.enter().append('text')
  .attr('text-anchor', 'middle')
  .attr("class", "area");
   
  area //update
   .text("A: " + polygonArea(data))
   .attr("x", getCentroid(d3.select("#svgpath"))[0] - 20)
   .attr("y", getCentroid(d3.select("#svgpath"))[1] - 20);
};

    function lineDistance( point1, point2 ) //distance formula
    {
    var xs = 0;
    var ys = 0;
     
    xs = point2.x - point1.x;
    xs = xs * xs;
     
    ys = point2.y - point1.y;
    ys = ys * ys;
     
    return Math.sqrt( xs + ys ).toFixed(2);
    };

    function loopI(index, dataArray, direction){ //find adjacent nodes, looping on start and end of array
      var nextI = index == dataArray.length - 1? 0 : index + 1;
      var prevI = index == 0? dataArray.length -1: index - 1;
      var result = direction == 1? nextI: prevI;
      return result
    };
    function getAngle(index, dataArray){ //use cosine rule to determine internal angle
      var a = lineDistance( dataArray[index], dataArray[loopI(index, dataArray, 1)] );
      var b = lineDistance( dataArray[index], dataArray[loopI(index, dataArray, 0)] );
      var c = lineDistance( dataArray[loopI(index, dataArray, 0)], dataArray[loopI(index, dataArray, 1)] );
      
      var cosFormula = ((a * a) + (b * b) - (c * c))/(2 * a * b);
      
      var angle = Math.acos(cosFormula);
      
      return (angle * (180/Math.PI)).toFixed(2)
    };
	
	function polygonArea(points) { //find area of polygon using shoelace algorithm
  var sum = 0.0;
  var length = points.length;
  if (length < 3) {
    return sum;
  }
  points.forEach(function(d1, i1) {
    i2 = (i1 + 1) % length;
    d2 = points[i2];
    sum += (d2.y * d1.x) - (d1.y * d2.x);
  });
  return Math.abs(sum / 2).toFixed(2);
}
function getCentroid(selection) {
    // get the DOM element from a D3 selection
    // you could also use "this" inside .each()
    var element = selection.node(),
        // use the native SVG interface to get the bounding box
        bbox = element.getBBox();
    // return the center of the bounding box
    return [bbox.x + bbox.width/2, bbox.y + bbox.height/2];
}

function changeLength(index1, index2){

var vect = [values[index2].x - values[index1].x, values[index2].y - values[index1].y];

var length = lineDistance(values[index1], values[index2]);
var uvect = [vect[0]/length, vect[1]/length];
var newlength = parseFloat(document.getElementById('lengthValue').value);

values[index2].x = values[index1].x + uvect[0] * newlength;
values[index2].y = values[index1].y + uvect[1] * newlength;
//document.getElementById('lengthValue').value = 0
createTri(values)

};
function changeAngle(index1, index2, currentAngle){
var newAngle = parseFloat(document.getElementById('lengthValue').value);
console.log(index1 + ", " + index2 + ", " + newAngle);
degreeRotate = newAngle - currentAngle
console.log(degreeRotate)
var degRad = degreeRotate * (Math.PI/180);
console.log(degRad)

var origin = values[index1]
var toRotate = values[index2]

var xRot = origin.x + Math.cos(degRad) * (toRotate.x - origin.x) - Math.sin(degRad) * (toRotate.y - origin.y);
var yRot = origin.y + Math.sin(degRad) * (toRotate.x - origin.x) + Math.cos(degRad) * (toRotate.y - origin.y);

values[index2].x = xRot;
values[index2].y = yRot;

createTri(values);
};
	function zoom() {
  createTri(values);
};
createTri(values)
              
            
!
999px

Console