简体   繁体   English

绘制3D对象

[英]Drawing 3D objects

I'm looking to draw a 3D cylinder with javascript by copying the layers and applying an increased margin to these elements. 我想通过复制图层并将增加的边距应用于这些元素来绘制带有JavaScript的3D圆柱体。 I have tried to set the height of the element in my input and run the copy function while total margin of the copied elements is lower than the set height of elements. 我尝试在输入中设置元素的高度并运行复制功能,同时复制的元素的总边距低于元素的设置高度。

http://jsfiddle.net/yuX7Y/3/ http://jsfiddle.net/yuX7Y/3/

<form>
        <input type="number" id="userHeight" />
        <button type="submit" onclick="circleHeight">Submit</button>
</form>

<div class="circle">
</div>
<div class="slice">
</div>

    $(document).ready(function(){

    var initMargin = 4;
    var totalMargin = 0;
    var i = 0;

    function copy(){
        $(".slice").clone().appendTo( ".content" ).css({'margin-top': totalMargin + "px"});
        console.log("cloned");
        i++;
        totalMargin = initMargin + 4;
        }
    function setH(){
        while(i < (document.getElementById("userHeight").value)){
            copy();
        }
        if(i>100){
            initMargin = 4;
            i=0;
        }
    }
});

Jump To The Result : http://jsfiddle.net/yuX7Y/15/ 跳转到结果http : //jsfiddle.net/yuX7Y/15/

Notes 笔记

This Fiddle/question intrigued me so I went ahead and looked at it for a little while. 这个小提琴/问题引起了我的兴趣,所以我继续看了一会儿。 There were actually a number of issues, some obvious and some less obvious. 实际上存在许多问题,一些很明显,有些不太明显。 Somewhat in the order I noticed them these are some of the issues: 按照某种顺序,我注意到了这些问题:

  • jQuery wasn't included in the fiddle jQuery未包含在小提琴中
  • The click event wasn't wired up correctly - it was actually trying to submit the form. click事件未正确连接-实际上是试图提交表单。 You need to use e.preventDefault to stop the form from submitting. 您需要使用e.preventDefault停止提交表单。 Since you were already using jQuery I just wired up with the jQuery click event: 由于您已经在使用jQuery,所以我只用jQuery click事件进行了连接:

     $("#recalculateHeight").click(function (e) { e.preventDefault(); setH(); }); 
  • Because of the use of "global" variables (variables not initialized within the routines), the submit would only work once. 由于使用了“全局”变量(在例程中未初始化的变量),因此提交只能工作一次。 Instead of this, I moved variable declarations to the appropriate routine. 取而代之的是,我将变量声明移至适当的例程。

  • Calling $(".slice").clone() clones ALL slice elements on the page. 调用$(".slice").clone()克隆页面上的所有切片元素。 The first time you clone, this is fine. 第一次克隆时,这很好。 But after that you are cloning two elements, then three elements, etc (as many slice elements as are on the page). 但是之后,您将克隆两个元素,然后克隆三个元素,依此类推(与页面上的切片元素一样多)。 To solve this I created a slice template like: 为了解决这个问题,我创建了一个切片模板,例如:

     <div class="slice" id="slice-template" style="display: none"></div> 

    Then you can clone to your hearts content like $("#slice-template").clone() . 然后,您可以将$("#slice-template").clone()类的内容克隆到您的内心。 Just don't forget to call the jQuery show() method or set display back to block on the cloned element. 只是别忘了调用jQuery show()方法或将显示设置回以阻止在克隆的元素上进行。

  • Finally, if you want to repeat the process many times you need to be able to clear previous elements from the page. 最后,如果要多次重复该过程,则需要能够清除页面中的先前元素。 I find this easiest to do by creating containers, then clearing the contents of the container. 我发现最简单的方法是创建容器,然后清除容器中的内容。 So I put all of the "slices" into this container: 因此,我将所有“切片”放入此容器中:

     <div class="content"> 

    Now when you want to clear the content node you can just call $(".content").empty(); 现在,当您要清除内容节点时,可以只调用$(".content").empty(); .

I also made a few style based changes in my Fiddle, but those don't make it work or not work, they just help me read the code! 我还在Fiddle中进行了一些基于样式的更改,但是这些更改使其无效或无效,它们只是帮助我阅读了代码! So, there you have it! 所以你有它! Best of luck! 祝你好运!

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

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