【情人节福利】JS实现玫瑰花
-
本文详细来自实验楼,如需转载请注明出处。
前言
不得不看的提示
先放压缩过的html代码,把下段代码复制到你的新建的html文件用浏览器打开就能看到一个玫瑰。我们管它叫生产版好了,三步就能完成。
在文档的最后附上一个简单的开发版本,供大家自行定制 :-)
情人节这么浪漫的告白,快去试试吧,送她或者他一朵与众不同的玫瑰花
代码(生产版)
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-doctype" style="box-sizing: border-box; color: rgb(136, 136, 255);"><!DOCTYPE HTML></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">html</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">head</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">title</span>></span>Rose<span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">title</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">meta</span> <span class="hljs-attribute" style="box-sizing: border-box;">http-equiv</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"Content-Type"</span> <span class="hljs-attribute" style="box-sizing: border-box;">content</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"text/html; charset=UTF-8"</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">head</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">body</span> <span class="hljs-attribute" style="box-sizing: border-box;">style</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"margin-left:350px"</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box;">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"c"</span>></span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">canvas</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> b = document.body; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> c = document.getElementsByTagName(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">'canvas'</span>)[<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>]; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> a = c.getContext(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">'2d'</span>); document.body.clientWidth; </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">with</span>(m=<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>)C=cos,S=sin,P=pow,R=random;c.width=c.height=f=<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">600</span>;h=-<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">250</span>;<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">p</span><span class="hljs-params" style="box-sizing: border-box;">(a,b,c)</span>{</span><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span>(c><span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">60</span>)<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span>[S(a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>)*(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">13</span>+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">5</span>/(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.2</span>+P(b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>)))-S(b)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>,b*f+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">625</span>+C(a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>)*(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">13</span>+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">5</span>/(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.2</span>+P(b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>)))+b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">400</span>,a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-b/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>,a];A=a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>-<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>;B=b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>-<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>;<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span>(A*A+B*B<<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>){<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span>(c><span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">37</span>){n=(j=c&<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>)?<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">6</span>:<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>;o=<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.5</span>/(a+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.01</span>)+C(b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">125</span>)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">3</span>-a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">300</span>;w=b*h;<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span>[o*C(n)+w*S(n)+j*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">610</span>-<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">390</span>,o*S(n)-w*C(n)+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">550</span>-j*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">350</span>,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1180</span>+C(B+A)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span>-j*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">300</span>,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.4</span>-a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span>+P(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-B*B,-h*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">6</span>)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.15</span>-a*b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.4</span>+C(a+b)/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">5</span>+P(C((o*(a+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>)+(B><span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>?w:-w))/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">25</span>),<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">30</span>)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span>*(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-B*B),o/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1e3</span>+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.7</span>-o*w*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">3e-6</span>]}<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span>(c><span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">32</span>){c=c*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1.16</span>-<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.15</span>;o=a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">45</span>-<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">20</span>;w=b*b*h;z=o*S(c)+w*C(c)+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">620</span>;<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span>[o*C(c)-w*S(c),<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">28</span>+C(B*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.5</span>)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span>-b*b*b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">60</span>-z/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>-h,z,(b*b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.3</span>+P((<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-(A*A)),<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.15</span>+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.3</span>)*b,b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.7</span>]}o=A*(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>-b)*(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">80</span>-c*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>);w=<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span>-C(A)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">120</span>-C(b)*(-h-c*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4.9</span>)+C(P(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-b,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>))*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>+c*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>;z=o*S(c)+w*C(c)+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">700</span>;<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span>[o*C(c)-w*S(c),B*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span>-C(P(b, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>))*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>-c/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">3</span>-z/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1.35</span>+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">450</span>,z,(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-b/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1.2</span>)*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.9</span>+a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span>, P((<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>-b),<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">20</span>)/<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.05</span>]}}setInterval(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">'for(i=0;i<1e4;i++)if(s=p(R(),R(),i%46/.74)){z=s[2];x=~~(s[0]*f/z-h);y=~~(s[1]*f/z-h);if(!m[q=y*f+x]|m[q]>z)m[q]=z,a.fillStyle="rgb("+~(s[3]*h)+","+~(s[4]*h)+","+~(s[3]*s[3]*-80)+")",a.fillRect(x,y,1,1)}'</span>,<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>) </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">body</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">html</span>></span> </code>
详细步骤
使用了多个不同的形状图来组成这朵代码玫瑰。共使用了31个形状:24个花瓣,4个萼片,2个叶子和1根花茎,其中每一个形状图都用代码进行描绘。
首先,来定义一个采样范围:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b)</span> {</span> <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 使用a和b作为采样范围的参数</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: a*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, y: b*<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span> }; <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 该表面是一个50*50单元区域</span> } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
然后,编写形状描绘代码:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> canvas = document.body.appendChild(document.createElement(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"canvas"</span>)), context = canvas.getContext(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"2d"</span>), a, b, position; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (a = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; a < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; a += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (b = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; b < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; b += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span>) { position = surface(a, b); context.fillRect(position.x, position.y, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
这时,看到的效果是这样的:
现在,尝试一下更密集的采样间隔:
正如现在所看到的,因为采样间隔越来越密集,点越来越接近,到最高密度时,相邻点之间的距离小于一个像素,肉眼就看不到间隔(见0.01)。为了不造成太大的视觉差,再进一步缩小采样间隔,此时,绘制区已经填满(比较结果为0.01和0.001)。
接下来,我用这个公式来绘制一个圆形:(X-X0)^ 2 +(Y-Y0)^ 2 <半径^ 2,其中(X0,Y0)为圆心:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b)</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> x = a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, y = b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, radius = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, x0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, y0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> ((x - x0) * (x - x0) + (y - y0) * (y - y0) < radius * radius) { <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 圆内</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: x, y: y }; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">else</span> { <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 圆外</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: rgb(0, 136, 0);">null</span>; } } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
为了防止溢出,还要加上一个采样条件:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (position = surface(a, b)) { context.fillRect(position.x, position.y, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
结果如下:
有不同的方法来定义一个圆,其中一些并不需要拒绝采样。我并无一定要使用哪一种来定义圆圈的意思,所以下面用另一种方法来定义一个圆:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b)</span> {</span> <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 使用极坐标,快回去翻微积分吧!</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> angle = a * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.PI * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, radius = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, x0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, y0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(angle) * radius * b + x0, y: <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(angle) * radius * b + y0 }; } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
(此方法相比前一个方法需要密集采样以进行填充。)
好了,现在让圆变形,以使它看起来更像是一个花瓣:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b)</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> x = a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, y = b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, radius = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, x0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, y0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> ((x - x0) * (x - x0) + (y - y0) * (y - y0) < radius * radius) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: x, y: y * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> + b) / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span> <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 变形</span> }; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">else</span> { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: rgb(0, 136, 0);">null</span>; } }</span></code>
这看起来已经很像一个玫瑰花瓣的形状了。在这里也可以试试通过修改一些函数数值,将会出现很多有趣的形状。
接下来应该给它添加色彩了:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b)</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> x = a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, y = b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, radius = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, x0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, y0 = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> ((x - x0) * (x - x0) + (y - y0) * (y - y0) < radius * radius) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: x, y: y * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> + b) / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, r: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor((<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - b) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">155</span>), <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 添加梯度</span> g: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, b: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span> }; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">else</span> { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: rgb(0, 136, 0);">null</span>; } } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (a = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; a < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; a += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.01</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (b = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; b < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; b += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.001</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (point = surface(a, b)) { context.fillStyle = <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"rgb("</span> + point.r + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.g + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.b + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">")"</span>; context.fillRect(point.x, point.y, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } } }</span></code>
一片带色的花瓣就出现了。
3D曲面和透视投影
定义三维表面很简单,比如,来定义一个管状物体:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b)</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> angle = a * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.PI * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, radius = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, length = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">400</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(angle) * radius, y: <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(angle) * radius, z: b * length - length / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 减去一般的长度,使得焦点在三维坐标中心点(0,0,0)</span> r: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>, g: <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor(b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">255</span>), b: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span> }; } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
接着添加投影透视图,首先需要我们定义一个摄像头:
如上图,将摄像头放置在(0,0,Z)位置,画布在X / Y平面。投影到画布上的采样点为:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> pX, pY, <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 画布X和Y轴的坐标</span> perspective = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">350</span>, halfHeight = canvas.height / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, halfWidth = canvas.width / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, cameraZ = -<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">700</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (a = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; a < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; a += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.001</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (b = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; b < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; b += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.01</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (point = surface(a, b)) { pX = (point.x * perspective) / (point.z - cameraZ) + halfWidth; pY = (point.y * perspective) / (point.z - cameraZ) + halfHeight; context.fillStyle = <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"rgb("</span> + point.r + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.g + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.b + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">")"</span>; context.fillRect(pX, pY, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } } } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
z-buffer
z-buffer在计算机图形学中是一个相当普遍的技术,在为物件进行着色时,执行“隐藏面消除”工作,使隐藏物件背后的部分就不会被显示出来。
上图是用z-buffer技术处理后的玫瑰。(可以看到已经具有立体感了)
talk is cheap, show the code!
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> zBuffer = [], zBufferIndex; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (a = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; a < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; a += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.001</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (b = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; b < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; b += <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.01</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (point = surface(a, b)) { pX = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor((point.x * perspective) / (point.z - cameraZ) + halfWidth); pY = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor((point.y * perspective) / (point.z - cameraZ) + halfHeight); zBufferIndex = pY * canvas.width + pX; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> ((<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">typeof</span> zBuffer[zBufferIndex] === <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"undefined"</span>) || (point.z < zBuffer[zBufferIndex])) { zBuffer[zBufferIndex] = point.z; context.fillStyle = <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"rgb("</span> + point.r + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.g + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.b + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">")"</span>; context.fillRect(pX, pY, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } } } } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
旋转
你可以使用任何矢量旋转的方法。在代码玫瑰的创建中,我使用的是欧拉旋转。现在将之前编写的管状物进行旋转,实现绕Y轴旋转:
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> angle = a * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.PI * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, radius = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">100</span>, length = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">400</span>, x = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(angle) * radius, y = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(angle) * radius, z = b * length - length / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, yAxisRotationAngle = -<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.4</span>, <span class="hljs-comment" style="box-sizing: border-box; color: rgb(136, 136, 136);">// 弧度</span> rotatedX = x * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(yAxisRotationAngle) + z * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(yAxisRotationAngle), rotatedZ = x * -<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(yAxisRotationAngle) + z * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(yAxisRotationAngle); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x: rotatedX, y: y, z: rotatedZ, r: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>, g: <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor(b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">255</span>), b: <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span> }; } </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
蒙特卡罗方法
关于采样时间,间隔过大过小都会引起极差的视觉感受,所以,需要设置合理的采样间隔,这里使用蒙特卡罗方法。
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> i; window.setInterval(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-params" style="box-sizing: border-box;">()</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> (i = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; i < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">10000</span>; i++) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (point = surface(<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.random(), <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.random())) { pX = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor((point.x * perspective) / (point.z - cameraZ) + halfWidth); pY = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.floor((point.y * perspective) / (point.z - cameraZ) + halfHeight); zBufferIndex = pY * canvas.width + pX; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> ((<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">typeof</span> zBuffer[zBufferIndex] === <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"undefined"</span>) || (point.z < zBuffer[zBufferIndex])) { zBuffer[zBufferIndex] = point.z; context.fillStyle = <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"rgb("</span> + point.r + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.g + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + point.b + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">")"</span>; context.fillRect(pX, pY, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } } } }, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>); </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span></code>
设置a和b为随机参数,用足够的采样完成表面填充。我每次绘制10000点,然后静待屏幕完成更新。
另外需要注意的是,如果随机数发生错误时,表面填充效果会出错。有些浏览器中,Math.random的执行是线性的,这就有可能导致表面填充效果出错。这时,就得使用类似Mersenne Twister(一种随机数算法)这样的东西去进行高质量的PRNG采样,从而避免错误的发生。
完成源码(开发版)
<code class="language-html hljs " style="box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; padding: 0.5em; color: rgb(0, 0, 0); border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; display: block; background-color: transparent !important;"><span class="hljs-doctype" style="box-sizing: border-box; color: rgb(136, 136, 255);"><!DOCTYPE HTML></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">html</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">head</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">title</span>></span>Rose<span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">title</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">meta</span> <span class="hljs-attribute" style="box-sizing: border-box;">http-equiv</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"Content-Type"</span> <span class="hljs-attribute" style="box-sizing: border-box;">content</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"text/html; charset=UTF-8"</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">head</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">body</span> <span class="hljs-attribute" style="box-sizing: border-box;">style</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"margin-left:200px"</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">div</span> <span class="hljs-attribute" style="box-sizing: border-box;">style</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"text-align: center"</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box;">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"c"</span>></span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">canvas</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">div</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span> <span class="hljs-attribute" style="box-sizing: border-box;">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(136, 0, 0);">"text/javascript"</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> canvas = document.getElementsByTagName(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">'canvas'</span>)[<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>]; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> context = canvas.getContext(<span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">'2d'</span>); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> a = context; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> b = document.body; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> c = canvas; document.body.clientWidth; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> zBuffer = []; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> SIZE = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">777</span>; canvas.width = canvas.height = SIZE; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> h = -<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">350</span>; <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(136, 0, 0); font-weight: bold;">surface</span><span class="hljs-params" style="box-sizing: border-box;">(a, b, c)</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (c > <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">60</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x : <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>) * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">13</span> + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">5</span> / (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.2</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow(b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>))) - <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(b) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, y : b * SIZE + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span>, z : <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">625</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>) * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">13</span> + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">5</span> / (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.2</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow(b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>))) + b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">400</span>, r : a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - b / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>, g : a }; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> A = a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span> - <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> B = b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span> - <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (A * A + B * B < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (c > <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">37</span>) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> j = c & <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> n = j ? <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">6</span> : <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> o = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.5</span> / (a + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.01</span>) + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">125</span>) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">3</span> - a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">300</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> w = b * h; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x : o * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(n) + w * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(n) + j * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">610</span> - <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">390</span>, y : o * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(n) - w * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(n) + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">550</span> - j * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">350</span>, z : <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1180</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(B + A) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span> - j * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">300</span>, r : <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.4</span> - a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - B * B, -h * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">6</span>) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.15</span> - a * b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.4</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(a + b) / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">5</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow(<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos((o * (a + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>) + (B > <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span> ? w : -w)) / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">25</span>), <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">30</span>) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span> * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - B * B), g : o / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1e3</span> + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.7</span> - o * w * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">3e-6</span> }; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (c > <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">32</span>) { c = c * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1.16</span> - <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.15</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> o = a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">45</span> - <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">20</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> w = b * b * h; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> z = o * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(c) + w * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(c) + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">620</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x : o * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(c) - w * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(c), y : <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">28</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(B * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.5</span>) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span> - b * b * b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">60</span> - z / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span> - h, z : z, r : (b * b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.3</span> + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow((<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - (A * A)), <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.15</span> + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.3</span>) * b, g : b * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.7</span> }; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> o = A * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span> - b) * (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">80</span> - c * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> w = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span> - <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(A) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">120</span> - <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(b) * (-h - c * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4.9</span>) + <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow(<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - b, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>)) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span> + c * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">2</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> z = o * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(c) + w * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(c) + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">700</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> { x : o * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(c) - w * <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.sin(c), y : B * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">99</span> - <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.cos(<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow(b, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">7</span>)) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">50</span> - c / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">3</span> - z / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1.35</span> + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">450</span>, z : z, r : (<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - b / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1.2</span>) * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.9</span> + a * <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.1</span>, g : <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.pow((<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span> - b), <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">20</span>) / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">4</span> + <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.05</span> }; } } setInterval(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">function</span><span class="hljs-params" style="box-sizing: border-box;">()</span> {</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span> ( <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> i = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>; i < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">10000</span>; i++) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> part = i % <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">46</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> c = part / <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">.74</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> point = surface(<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.random(), <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">Math</span>.random(), c); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> (point) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> z = point.z; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> x = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">parseInt</span>(point.x * SIZE / z - h); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> y = <span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">parseInt</span>(point.y * SIZE / z - h); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> zBufferIndex = y * SIZE + x; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span> ((<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">typeof</span> zBuffer[zBufferIndex] === <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"undefined"</span>) || (zBuffer[zBufferIndex] > z)) { zBuffer[zBufferIndex] = z; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> r = -<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">parseInt</span>(point.r * h); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> g = -<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">parseInt</span>(point.g * h); <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">var</span> b = -<span class="hljs-built_in" style="box-sizing: border-box; font-weight: bold;">parseInt</span>(point.r * point.r * -<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">80</span>); context.fillStyle = <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">"rgb("</span> + r + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + g + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">","</span> + b + <span class="hljs-string" style="box-sizing: border-box; color: rgb(136, 0, 0);">")"</span>; context.fillRect(x, y, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">1</span>); } } } }, <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 136, 0);">0</span>); </span><span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">script</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">body</span>></span> <span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; font-weight: bold;">html</span>></span></code>
为了使玫瑰的每个部分在同一时间完成并呈现,还需要添加一个功能,为每部分设置一个参数以返回值来进行同步。并用一个分段函数代表玫瑰的各个部分。比如在花瓣部分,可以使用旋转和变形来创建它们。
虽然表面采样方法是创建三维图形非常著名的、最古老的方法之一,但这种把蒙特卡罗、z-buffer加入到表面采样中的方法并不常见。对于现实生活场景的制作,这也许算不上很有创意,但它简易的代码实现和很小的体积仍令人满意。
快去做一朵与众不同的玫瑰送给你的那个她/他吧!
Happy Valentine’s Day
- 本文详细来自实验楼,如需转载请注明出处。