score:1
If you set the desired margin:
var margin = 20;
You can avoid the triangles having their center positioned within that margin:
.attr("transform", function(d)
{ return "translate(" + (margin + Math.random()*(width - margin))
+ "," + (margin + Math.random()*(height - margin)) + ")";
})
Have in mind that this doesn't take in consideration the fact that the triangles have different sizes: it sets the same margin for everyone.
Here is the fiddle: https://jsfiddle.net/7qkqtwmu/
score:3
You can easily use the element bounding box size when you set it's translate value.
It's strange, but for triangles to fully be inside svg, using bbox height does not work. Shapes width are a couple of pixels larger than their heights and using that value in computations works.
var width = 250;
var height = 250;
var number = 30;
var borderSize = 1;
var body = d3.select("body");
var triangle = d3.symbol()
.type(d3.symbolTriangle);
var svg = body.append("svg")
.attr("width", width)
.attr("height", height)
.style("border", "1px solid black");
function drawTriangles(number) {
for (var i = 0; i < number; i++) {
var dim = Math.random() * 300;
svg.append("path")
.attr("d", triangle.size(dim))
.attr("transform", function (d) {
var boundingBox = this.getBBox();
var elementWidth = Math.ceil(boundingBox.width);
var randomXOffset = Math.random() * (width - elementWidth - 2 * borderSize) + elementWidth/2 + borderSize;
var randomYOffset = Math.random() * (height - elementWidth - 2 * borderSize) + elementWidth/2 + borderSize;
return "translate(" + randomXOffset + "," + randomYOffset + ")";
})
.attr("fill", "rgb(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ")")
.attr("opacity", 2)
.attr("class", "path" + i);
}
}
drawTriangles(number);
<script src="https://d3js.org/d3.v4.min.js"></script>
<body>
</body>
Source: stackoverflow.com
Related Query
- D3.js create triangle totally inside the svg container
- mouseout/mouseleave gets fired when mouse moves INSIDE the svg path element
- How do I create a minimap of an SVG using D3 v5 and show the current viewport dimensions?
- Pushing the top most object in an SVG container to the bottom
- D3, iterate over objects to create several pie charts within the same container
- Dynamically create rect inside svg based on data
- SVG text element height/width inside of display:none container
- D3JS: help to create a SVG text element inside a circle using D3js
- get the position details of a div inside an svg
- Sometimes svg chart doesn't fit in the container but it works when screen refresh
- g group element doesn't start at 0,0 inside the svg
- How can I scale an SVG so that the content drawn in it to fit the container size?
- Draw polygon inside of the SVG defined viewport
- How to wrap the text inside the SVG polygon without cutting the text
- How to check whether a svg circle inside the svg path
- How to keep circles inside the svg when updating data in bubble chart
- Can I create an HTML list in a D3.js SVG container
- after making bar chart inside SVG using D3 , it is not fitting the entire SVG
- d3.js - Why does my SVG tags get added inside the HTML tag
- dc.js - Hide the div container till the svg is created
- Why does the text not appear inside the svg rects
- trying to create a donut chart with labels inside the curve using d3.js
- Trying to create an SVG with a grid of images. But the images don't show
- Create small polygons inside large polygons in SVG using javascript
- Apply changes to selected elements inside the svg element?
- How to access the DOM element that correlates to a D3 SVG object?
- How to add an image to an svg container using D3.js
- How do I get the width of an svg element using d3js?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- D3 Selecting an element inside an SVG
More Query from same tag
- How to find min and max value in a array of objects
- D3 js line chart with tooltip on hover with multiple line chart
- Datamaps: how to show popup on mouseover AND customize the mouseover event listener
- d3.js: How to populate cells with color for blank values?
- Update options in a drop-down menu based on another drop-down
- Why is my D3.js tooltip not working
- D3 enter/exit groups of circles for paths
- Computing array of relationships from tags
- Opacity update on all d3.svg.circles except for the class hovered
- how can i break text in 2 lines in d3.js
- How split line in D3.js when missing data
- elementClick event is not firing in angular-nvd3 chart
- angularjs-nvd3-directives updating data
- Tooltip on mouseover d3
- Creating a Line Graph in D3 with data in independent arrays?
- Real-time D3 timeseries using Backbone -- graph moves slower than axis
- How to access variables declared outside d3.json request within it
- Words go out of the borders in d3.js tag cloud
- stacking axes horizontally on top of each other
- D3 find the angle of line
- D3: Sunburst chart with root node on the outside ring
- d3.conventions not a function at script.js
- D3 brush extent disappears from chart when dragged beyond extent
- D3 offset X-axis
- D3 spacing between bars
- Can't append table to div
- Using $.getScript to load d3.js
- Where is d3.svg.diagonal()?
- Change Default d3.js colors
- Dis Alignment of bar chart in D3.js