简体   繁体   English

HTML / CSS / JS中的几何图案

[英]geometric patterns in HTML/CSS/JS

I have been challenge to recreate the image below using HTML/CSS/JS without using any img files. 我一直面临使用HTML / CSS / JS重新创建下面的图像而不使用任何img文件的挑战。 I was thinking that I could make the shapes with and CSS but I know this is probably the most stupid way to do it. 我当时以为可以用CSS制作形状,但是我知道这可能是最愚蠢的方法。

Do any of you have suggestions how to approach this? 你们有没有建议如何解决这个问题? Maybe a HTML5 canvas? 也许是HTML5画布? I see the pattern is made of two layers, one with triangles and the top layer with circles. 我看到图案由两层组成,一层是三角形,顶层是圆形。 How would I approach this if I wanted to have the triangles and the circles randomly generated? 如果我想随机生成三角形和圆形,该如何处理?

Thanks 谢谢 在此处输入图片说明

You could definitely achieve this by using the canvas element, but have you thought about using d3.js ? 您绝对可以通过使用canvas元素来实现这一点,但是您是否考虑过使用d3.js

D3.js is a library which is capable of manipulating documents based on data. D3.js是一个能够基于数据处理文档的库。 Since all the elements within your picture have an exact position within a cartesian coordinate system you would just have to provide the data and then it would be fairly simple to append the elements to your document. 由于图片中的所有元素在笛卡尔坐标系中都有确切的位置,因此您只需要提供数据,然后将元素附加到文档中就很简单了。

You could be very precise by using the exact coordinates of each element. 通过使用每个元素的确切坐标,您可以非常精确。 Take a look at the snippet and i am sure you will get the idea. 看一下摘要,我相信您会明白的。 The D3 way of selecting an manipulating elements is very similar to what jQuery does, so if you are familiar with jQuery you will like D3js. D3选择操作元素的方式与jQuery非常相似,因此,如果您熟悉jQuery,您会喜欢D3js。

Hope this helps you out. 希望这可以帮助你。

 var margin = { top: 50, right: 50, bottom: 50, left: 50}, w = 300 - margin.left - margin.right, h = 600 - margin.top - margin.bottom, circleRadii = 15, triData = [{x: 20, y: 30}, {x: 50, y: 120}, {x: 140, y: 160}], circleData = [{x: 10, y: 10}, {x: 40, y: 80}, {x: 160, y: 70}]; var svg = d3.select("body") .append("svg") .attr("width", w + margin.left + margin.right) .attr("height", h + margin.left + margin.right) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var tri = svg.selectAll(".point") .data(triData) .enter().append("path") .attr("class", "point") .attr("stroke", "none") .attr("fill", "rgba(30,110,160,.5)") .attr("d", d3.svg.symbol().type("triangle-up").size(1024*2)) .attr("transform", function(d) { return "translate(" + dx + "," + dy + ")"; }); var circles = svg.selectAll("circle") .data(circleData) .enter() .append("circle"); var circleAttr = circles .attr("cx", function (d) { return dx; }) .attr("cy", function (d) { return dy; }) .attr("r", circleRadii) .style("fill", "rgba(10,100,0,.5)"); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM