Skip to content

Instantly share code, notes, and snippets.

@arpitnarechania
Last active February 5, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpitnarechania/0e0316936eb257b25427f52bddb03401 to your computer and use it in GitHub Desktop.
Save arpitnarechania/0e0316936eb257b25427f52bddb03401 to your computer and use it in GitHub Desktop.
Basic Shapes - Rectangle

Generating a rectangle using D3.js

<!DOCTYPE>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Basic Shapes - Rectangle</title>
<!-- JavaScript Libraries //-->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- CSS Style //-->
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="form" style="display:inline-block;float:left;">
<div class="formElement">
<span >X</span>
<input id="x" type="number">
</div>
<div class="formElement">
<span >Y</span>
<input id="y" type="number">
</div>
<div class="formElement">
<span >Fill Color</span>
<input id="fillColor" type="text">
</div>
<div class="formElement">
<span >Stroke Color</span>
<input id="strokeColor" type="text">
</div>
<div class="formElement">
<span >Stroke Width</span>
<input id="strokeWidth" type="number">
</div>
<div class="formElement">
<span >Width</span>
<input id="width" type="number">
</div>
<div class="formElement">
<span >Height</span>
<input id="height" type="number">
</div>
</div>
<div id="rectangle" style="display:inline-block;float:right;"></div>
</body>
<script type="text/javascript" src="main.js"></script>
<script>
var config = {
margin : {top: 50, right: 50, bottom: 50, left: 50},
x : 50,
y : 50,
fill_color : "red",
width : 300,
height : 150,
stroke_color : "black",
stroke_width : 2,
dom_element : "#rectangle"
};
$("#x").on("input",function(){
config.x = parseInt($(this).val());
renderRectangle(config,false);
});
$("#y").on("input",function(){
config.y = parseInt($(this).val());
renderRectangle(config,false);
});
$("#fillColor").on("input",function(){
config.fill_color = $(this).val();
renderRectangle(config,false);
});
$("#width").on("input",function(){
config.width = parseInt($(this).val());
renderRectangle(config,false);
});
$("#height").on("input",function(){
config.height = parseInt($(this).val());
renderRectangle(config,false);
});
$("#strokeColor").on("input",function(){
config.stroke_color = $(this).val();
renderRectangle(config,false);
});
$("#strokeWidth").on("input",function(){
config.stroke_width = parseInt($(this).val());
renderRectangle(config,false);
});
renderRectangle(config,true);
</script>
</html>
function renderRectangle(config,first_time){
if(first_time){
var svg = d3.select(config.dom_element).append("svg")
.style("width", config.width + config.margin.left + config.margin.right)
.style("height", config.height + config.margin.top + config.margin.bottom);
svg
.append("rect")
.style("x",config.x)
.style("y",config.y)
.style("fill",config.fill_color)
.style("stroke",config.stroke_color)
.style("stroke-width",config.stroke_width)
.style("height",config.height)
.style("width",config.width)
$("#x").val(config.x);
$("#y").val(config.y);
$("#width").val(config.width);
$("#height").val(config.height);
$("#strokeColor").val(config.stroke_color);
$("#strokeWidth").val(config.stroke_width);
$("#fillColor").val(config.fill_color);
}else{
var dom_element = d3.select("svg")
.style("width", config.width + config.margin.left + config.margin.right)
.style("height", config.height + config.margin.top + config.margin.bottom);
dom_element.select("rect")
.style("x",config.x)
.style("y",config.y)
.style("fill",config.fill_color)
.style("stroke",config.stroke_color)
.style("stroke-width",config.stroke_width)
.style("height",config.height)
.style("width",config.width);
}
}
body{
width: 700px;
}
input {
width:50px;
}
.formElement{
padding:8px;
}
span{
display: inline-block;
width: 150px;
}
svg{
border: 1px solid black;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment