<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>soledad penadés &#187; Code</title>
	<atom:link href="http://soledadpenades.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://soledadpenades.com</link>
	<description>repeat 4[fd 100 rt 90]</description>
	<lastBuildDate>Wed, 25 Apr 2012 21:10:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Simple yet Frequently Asked Questions on three.js (SFAQ)</title>
		<link>http://soledadpenades.com/2012/04/17/simple-yet-frequently-asked-questions-on-three-js-sfaq/</link>
		<comments>http://soledadpenades.com/2012/04/17/simple-yet-frequently-asked-questions-on-three-js-sfaq/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 19:43:37 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[sfaq]]></category>
		<category><![CDATA[three.js]]></category>
		<category><![CDATA[webgl]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3957</guid>
		<description><![CDATA[As promised, here&#8217;s what I learnt with my latest test with three.js :-D How do I get smooth color transitions between the vertices of a face? If you want to get color &#8220;interpolation&#8221; like the faces in the image&#8230; &#8230; you have to do a few things: Create a colors array, with as many entries [...]]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://soledadpenades.com/2012/04/16/mesh-subdivision-2012-extended-edition/">promised</a>, here&#8217;s what I learnt with my <a href="http://lab.soledadpenades.com/js/mesh_subdivision">latest test</a> with <a href="https://github.com/mrdoob/three.js">three.js</a> :-D<br />
<span id="more-3957"></span></p>
<h3>How do I get smooth color transitions between the vertices of a face?</h3>
<p>If you want to get color &#8220;interpolation&#8221; like the faces in the image&#8230;<br />
<img src="/imgs/2012/ms02.jpg" alt="mesh subdivision" /></p>
<p>&#8230; you have to do a few things:</p>
<ol>
<li>Create a colors array, with as many entries as vertices in your mesh.</li>
<li>Set the colors array as the <strong>vertexColors</strong> property of the geometry</li>
<li>Using <strong>THREE.MeshBasicMaterial</strong>, set the <strong>vertexColors</strong> property to <strong>THREE.VertexColors</strong></li>
</ol>
<p>This would create a single-triangle mesh, with red, green and blue colors in each vertex respectively:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> vertices <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> colors <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> faces <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> geometry<span style="color: #339933;">,</span> material<span style="color: #339933;">,</span> mesh<span style="color: #339933;">;</span><br />
<br />
vertices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
colors.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Color</span><span style="color: #009900;">&#40;</span>0xFF0000<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
vertices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
colors.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Color</span><span style="color: #009900;">&#40;</span>0x00FF00<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
vertices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
colors.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Color</span><span style="color: #009900;">&#40;</span>0x0000FF<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// 0, 1 and 2 are the indices of the vertices in the vertices array</span><br />
faces.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Face3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
geometry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Geometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertices</span> <span style="color: #339933;">=</span> vertices<span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">faces</span> <span style="color: #339933;">=</span> faces<span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertexColors</span> <span style="color: #339933;">=</span> colors<span style="color: #339933;">;</span><br />
<br />
material <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">MeshBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> vertexColors<span style="color: #339933;">:</span> THREE.<span style="color: #660066;">VertexColors</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
mesh <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Mesh</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #339933;">,</span> material<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<h3>How do I change the line width of an object displayed as wireframe?</h3>
<p>Using <strong>THREE.MeshBasicMaterial</strong>, set the value of the wireframeLinewidth property:</p>
<p>E.g. for a black, three units thick wireframe material:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> mat <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">MeshBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> wireframeLinewidth<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> color<span style="color: #339933;">:</span> 0x000000<span style="color: #339933;">,</span> wireframe<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<h3>How do I make an object display as double-sided?</h3>
<p>I thought this was a property that had to be set in the material, but I was wrong. It&#8217;s an Object3D property, so if you want an object to display both of its sides, you have to do:</p>
<div class="syhi_block"><code>mesh.<span style="color: #660066;">doubleSided</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></code></div>
<h3>Why aren&#8217;t the children objects hidden when the parent is <em>invisible</em>?</h3>
<p>For optimisation reasons this is not automatic. So you have to manually iterate over the parent&#8217;s children and set their visible property explicitly. But three.js has a utility function for traversing object hierarchies:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> visible <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
THREE.<span style="color: #660066;">SceneUtils</span>.<span style="color: #660066;">traverseHierarchy</span><span style="color: #009900;">&#40;</span>parent<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>child<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; child.<span style="color: #660066;">visible</span> <span style="color: #339933;">=</span> visible<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<h3>How do I get the camera position updated? It&#8217;s not moving even if I change its position!</h3>
<p>Ah, because you have to tell it where to <em>lookAt</em>. Assuming <em>camera_x</em>, <em>camera_y</em> and <em>camera_z</em> are numbers, and <em>cam_target</em> is a <strong>Vector3</strong>:</p>
<div class="syhi_block"><code>camera.<span style="color: #660066;">position</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> camera_x<span style="color: #339933;">;</span><br />
camera.<span style="color: #660066;">position</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> camera_y<span style="color: #339933;">;</span><br />
camera.<span style="color: #660066;">position</span>.<span style="color: #660066;">z</span> <span style="color: #339933;">=</span> camera_z<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
camera.<span style="color: #660066;">lookAt</span><span style="color: #009900;">&#40;</span> cam_target <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<p>Every time you change either the camera position or the target position (or both!) you need to call <strong>camera.lookAt</strong> or you won&#8217;t get any visible result.</p>
<p>This seems to be a recent change in three.js. Apparently the camera was automatically updated in older revisions.</p>
<h3>How do I get the contents of the current canvas? All I get is an empty, transparent image in Chrome!</h3>
<p>This is an <a href="http://code.google.com/p/chromium/issues/detail?id=82086">expected</a> behaviour of the WebGL implementation. The solution is to create the WebGL context with the <em>preserveDrawingBuffer</em> flag. Of course, three.js already has that in mind!</p>
<p>So you simply have to add that parameter when initialising the renderer:</p>
<div class="syhi_block"><code>renderer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">WebGLRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> preserveDrawingBuffer<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<p>Once that&#8217;s done you can happily extract screenshots of the current output with the usual <strong>canvas.toDataURL</strong> (although I <a href="http://soledadpenades.com/2012/04/16/mesh-subdivision-2012-extended-edition/">used</a> <strong>canvas.toBlob</strong> for directly downloading the image to the browser).</p>
<h3>How do I modify a geometry dynamically? (from javascript, not in a vertex shader)</h3>
<p>This is my super favourite question, so I left it for the end!</p>
<p>You can change the vertices position in a geometry once it&#8217;s been added to the scene, but you won&#8217;t notice any difference until you let three.js know that you&#8217;ve changed something in the object!</p>
<p>That&#8217;s done with:</p>
<div class="syhi_block"><code>mesh.<span style="color: #660066;">geometry</span>.__dirtyVertices <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></code></div>
<p>every time you&#8217;ve modified the geometry and need to get it updated in the screen.</p>
<p><a href="http://mrdoob.com">Mr.doob</a> told me it&#8217;s also possible to modify other geometry properties, such as the vertex colors. If you modify the vertex colors, you then need to set the __dirtyColors property to true.</p>
<p>Actually, this is not the &#8216;proper&#8217; way of doing things with WebGL. Every time a geometry is marked as &#8216;dirty&#8217;, it needs to be re-sent and re-created in the GPU, so to speak. Therefore, this flexibility comes at a cost; the larger the geometry, the slowest it&#8217;ll be. I should be doing these calculations in a vertex shader, running in the GPU, but since I&#8217;m still learning I&#8217;m allowing myself this little computational luxury ;-)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3957&amp;md5=873c8f2ce88b6b1c66096437672c5720" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2012/04/17/simple-yet-frequently-asked-questions-on-three-js-sfaq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2012%2F04%2F17%2Fsimple-yet-frequently-asked-questions-on-three-js-sfaq%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=Simple+yet+Frequently+Asked+Questions+on+three.js+%28SFAQ%29&amp;amp;description=As+promised%2C+here%27s+what+I+learnt+with+my+latest+test+with+three.js+%3A-D%0D%0A%0D%0AHow+do+I+get+smooth+color+transitions+between+the+vertices+of+a+face%3F%0D%0A%0D%0AIf+you+want+to+get+color+%22interpolation%22+like+the+faces+in+the+image...%0D%0A%0D%0A%0D%0A...+you+have+to+do+a+few+things%3A%0D%0A%0D%0A%0D%0A%09Create+a+colors+array%2C+with+as+many+entries+as+vertices+in+your+mesh.%0D%0A%09Set+the+colors+array+as+the+vertexColors+property+of+the+geometry%0D%0A%0D%0A%09Using+THREE.MeshBasicMaterial%2C+set+the+vertexColors+property+to+THREE.VertexColors%0D%0A%0D%0A%0D%0AThis+would+create+a+single-triangle+mesh%2C+with+red%2C+green+and+blue+colors+in+each+vertex+respectively%3A%0D%0A%0D%0A%0D%0Avar+vertices+%3D+%5B%5D%2C+colors+%3D+%5B%5D%2C+faces+%3D+%5B%5D%2C+geometry%2C+material%2C+mesh%3B%0D%0A%0D%0Avertices.push%28+new+THREE.Vertex%28+new+THREE.Vector3%280%2C+0%2C+0%29+%29+%29%3B%0D%0Acolors.push%28+new+THREE.Color%280xFF0000%29+%29%3B%0D%0A%0D%0Avertices.push%28+new+THREE.Vertex%28+new+THREE.Vector3%280%2C+1%2C+0%29+%29+%29%3B%0D%0Acolors.push%28+new+THREE.Color%280x00FF00%29+%29%3B%0D%0A%0D%0Avertices.push%28+new+THREE.Vertex%28+new+THREE.Vector3%281%2C+1%2C+0%29+%29+%29%3B%0D%0Acolors.push%28+new+THREE.Color%280x0000FF%29+%29%3B%0D%0A%0D%0A%2F%2F+0%2C+1+and+2+are+the+indices+of+the+vertices+in+the+vertices+array%0D%0Afaces.push%28+new+THREE.Face3%280%2C+1%2C+2%29+%29%3B%0D%0A%0D%0Ageometry+%3D+new+THREE.Geometry%28%29%3B%0D%0Ageometry.vertices+%3D+vertices%3B%0D%0Ageometry.faces+%3D+faces%3B%0D%0Ageometry.vertexColors+%3D+colors%3B%0D%0A%0D%0Amaterial+%3D+new+THREE.MeshBasicMaterial%28%7B+vertexColors%3A+THREE.VertexColors+%7D%29%3B%0D%0A%0D%0Amesh+%3D+new+THREE.Mesh%28geometry%2C+material%29%3B%0D%0A%0D%0A%0D%0AHow+do+I+change+the+line+width+of+an+object+displayed+as+wireframe%3F%0D%0A%0D%0AUsing+THREE.MeshBasicMaterial%2C+set+the+value+of+the+wireframeLinewidth+property%3A%0D%0A%0D%0AE.g.+for+a+black%2C+three+units+thick+wireframe+material%3A%0D%0A%0D%0A%0D%0Avar+mat+%3D+new+THREE.MeshBasicMaterial%28%7B+wireframeLinewidth%3A+3%2C+color%3A+0x000000%2C+wireframe%3A+true+%7D%29%3B%0D%0A%0D%0A%0D%0AHow+do+I+make+an+object+display+as+double-sided%3F%0D%0A%0D%0AI+thought+this+was+a+property+that+had+to+be+set+in+the+material%2C+but+I+was+wrong.+It%27s+an+Object3D+property%2C+so+if+you+want+an+object+to+display+both+of+its+sides%2C+you+have+to+do%3A%0D%0A%0D%0A%0D%0Amesh.doubleSided+%3D+true%3B%0D%0A%0D%0A%0D%0AWhy+aren%27t+the+children+objects+hidden+when+the+parent+is+invisible%3F%0D%0A%0D%0AFor+optimisation+reasons+this+is+not+automatic.+So+you+have+to+manually+iterate+over+the+parent%27s+children+and+set+their+visible+property+explicitly.+But+three.js+has+a+utility+function+for+traversing+object+hierarchies%3A%0D%0A%0D%0A%0D%0Avar+visible+%3D+false%3B%0D%0ATHREE.SceneUtils.traverseHierarchy%28parent%2C+function%28child%29+%7B%0D%0A%09child.visible+%3D+visible%3B%0D%0A%7D%29%3B%0D%0A%0D%0A%0D%0AHow+do+I+get+the+camera+position+updated%3F+It%27s+not+moving+even+if+I+change+its+position%21%0D%0A%0D%0AAh%2C+because+you+have+to+tell+it+where+to+lookAt.+Assuming+camera_x%2C+camera_y+and+camera_z+are+numbers%2C+and+cam_target+is+a+Vector3%3A%0D%0A%0D%0A%0D%0Acamera.position.x+%3D+camera_x%3B%0D%0Acamera.position.y+%3D+camera_y%3B%0D%0Acamera.position.z+%3D+camera_z%3B%0D%0A%09%09%0D%0Acamera.lookAt%28+cam_target+%29%3B%0D%0A%0D%0A%0D%0AEvery+time+you+change+either+the+camera+position+or+the+target+position+%28or+both%21%29+you+need+to+call+camera.lookAt+or+you+won%27t+get+any+visible+result.%0D%0A%0D%0AThis+seems+to+be+a+recent+change+in+three.js.+Apparently+the+camera+was+automatically+updated+in+older+revisions.%0D%0A%0D%0AHow+do+I+get+the+contents+of+the+current+canvas%3F+All+I+get+is+an+empty%2C+transparent+image+in+Chrome%21%0D%0A%0D%0AThis+is+an+expected+behaviour+of+the+WebGL+implementation.+The+solution+is+to+create+the+WebGL+context+with+the+preserveDrawingBuffer+flag.+Of+course%2C+three.js+already+has+that+in+mind%21%0D%0A%0D%0ASo+you+simply+have+to+add+that+parameter+when+initialising+the+renderer%3A%0D%0A%0D%0A%0D%0Arenderer+%3D+new+THREE.WebGLRenderer%28%7B+preserveDrawingBuffer%3A+true+%7D%29%3B%0D%0A%0D%0A%0D%0AOnce+that%27s+done+you+can+happily+extract+screenshots+of+the+current+output+with+the+usual+canvas.toDataURL+%28although+I+used+canvas.toBlob+for+directly+downloading+the+image+to+the+browser%29.%0D%0A%0D%0AHow+do+I+modify+a+geometry+dynamically%3F+%28from+javascript%2C+not+in+a+vertex+shader%29%0D%0A%0D%0AThis+is+my+super+favourite+question%2C+so+I+left+it+for+the+end%21%0D%0A%0D%0AYou+can+change+the+vertices+position+in+a+geometry+once+it%27s+been+added+to+the+scene%2C+but+you+won%27t+notice+any+difference+until+you+let+three.js+know+that+you%27ve+changed+something+in+the+object%21%0D%0A%0D%0AThat%27s+done+with%3A%0D%0A%0D%0A%0D%0Amesh.geometry.__dirtyVertices+%3D+true%3B%0D%0A%0D%0A%0D%0Aevery+time+you%27ve+modified+the+geometry+and+need+to+get+it+updated+in+the+screen.%0D%0A%0D%0AMr.doob+told+me+it%27s+also+possible+to+modify+other+geometry+properties%2C+such+as+the+vertex+colors.+If+you+modify+the+vertex+colors%2C+you+then+need+to+set+the+__dirtyColors+property+to+true.%0D%0A%0D%0AActually%2C+this+is+not+the+%27proper%27+way+of+doing+things+with+WebGL.+Every+time+a+geometry+is+marked+as+%27dirty%27%2C+it+needs+to+be+re-sent+and+re-created+in+the+GPU%2C+so+to+speak.+Therefore%2C+this+flexibility+comes+at+a+cost%3B+the+larger+the+geometry%2C+the+slowest+it%27ll+be.+I+should+be+doing+these+calculations+in+a+vertex+shader%2C+running+in+the+GPU%2C+but+since+I%27m+still+learning+I%27m+allowing+myself+this+little+computational+luxury+%3B-%29&amp;amp;tags=sfaq%2Cthree.js%2Cwebgl%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Mesh subdivision (2012 extended edition)</title>
		<link>http://soledadpenades.com/2012/04/16/mesh-subdivision-2012-extended-edition/</link>
		<comments>http://soledadpenades.com/2012/04/16/mesh-subdivision-2012-extended-edition/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 21:54:15 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[demoscene]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[three.js]]></category>
		<category><![CDATA[webgl]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3947</guid>
		<description><![CDATA[Back in 2004, when I was starting to experiment with OpenGL, I had the idea of creating meshes from scratch. This will come in handy for intros, since this way I won&#8217;t be needing to include the meshes&#8217; data with the executable!, I thought. I would create a simple base pyramidal mesh, and subdivide it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://lab.soledadpenades.com/js/mesh_subdivision/#eyJudW1iZXJfcGFzc2VzIjo4LCJiYWNrZ3JvdW5kX2NvbG9yIjoxMjMwMzI5MSwid2lyZWZyYW1lIjp0cnVlLCJ3aXJld2lkdGgiOjEsIndpcmVjb2xvciI6MCwiY29sb3JfMSI6MTY3MTE2ODAsImNvbG9yXzIiOjY1MjgwLCJjb2xvcl8zIjoyNTUsIndvYmJseSI6ZmFsc2UsIndvYmJseV9zcGVlZCI6MSwicm90YXRpb25fc3BlZWRfeCI6MSwicm90YXRpb25fc3BlZWRfeiI6MSwiY2FtZXJhX3giOjAsImNhbWVyYV95IjowLCJjYW1lcmFfeiI6MTAsImRyYXdfYXhpcyI6dHJ1ZX0="><img src="/imgs/2012/ms01.jpg" alt="mesh subdivision 2012" /></a></p>
<p>Back in 2004, when I was starting to experiment with OpenGL, I had the idea of creating meshes <em>from scratch</em>. <q>This will come in handy for intros, since this way I won&#8217;t be needing to include the meshes&#8217; data with the executable!</q>, I thought.</p>
<p>I would create a simple base pyramidal mesh, and subdivide it until I got something with more vertices and faces, but properly connected (not just a bunch of random points). So I when I had a couple of free days, I came up with <a href="http://soledadpenades.com/2004/04/19/improving-mesh-subdivision/">a very rough way</a> of creating and subdividing meshes. It was very raw, but I loved that I had done it myself!<br />
<span id="more-3947"></span><br />
<a href="http://soledadpenades.com/2004/04/19/improving-mesh-subdivision/"><img src="http://www.soledadpenades.com/imgs/mesh_sdv_3passes_fixed_lw.png" /></a></p>
<p>At the end, I didn&#8217;t use it for any intro at all, but I used the <em>trick</em> in a demo instead, called <a href="http://soledadpenades.com/projects/demoscene/codecolors-by-ppg/">codecolors</a> (<a href="https://github.com/sole/demoscene/tree/master/releases/ppg/ppg_05_cc/src">source code</a>, if you&#8217;re feeling adventurous). Although it&#8217;s hard to notice, since everything was generously blurred (I had recently learned how to get a cheap motion blur effect and was really eager to use it in every single frame!).</p>
<p>Fast forward to 2012. I am learning (tinkering with?) <a href="https://github.com/mrdoob/three.js">three.js</a> and I wanted to find out how to manipulate vertex data dynamically. I somehow remembered this experiment and where I left it (<q>Next step is playing with the vertices!</q>), and I decided to continue it.</p>
<p>Once I got the basics working, I thought it would be neat to have some way of interacting with the scene&#8211;so I added <a href="https://code.google.com/p/dat-gui/">dat.gui</a>, which are the controls on top right. Then I also wanted to save the images, as I once I added controls for altering the look I started getting great imagery! So I added <a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a> and <a href="https://github.com/eligrey/canvas-toBlob.js">canvas-toBlob.js</a>, since browser support for downloading images is still a bit incomplete (so to speak).</p>
<p>And finally I thought: <q>hey, storing images is ok, but what if I want to modify an existing configuration?</q>. So I added support for storing the current configuration via some simple location hash serialising. I am not sure if it&#8217;s the best way but it seems to work well enough for now!</p>
<p><a href="http://lab.soledadpenades.com/js/mesh_subdivision/#eyJudW1iZXJfcGFzc2VzIjo0LCJiYWNrZ3JvdW5kX2NvbG9yIjoxMjMwMzI5MSwid2lyZWZyYW1lIjpmYWxzZSwid2lyZXdpZHRoIjoxLCJ3aXJlY29sb3IiOjE2NzExNjgwLCJjb2xvcl8xIjoxNjcxMTY4MCwiY29sb3JfMiI6NjUyODAsImNvbG9yXzMiOjI1NSwid29iYmx5IjpmYWxzZSwid29iYmx5X3NwZWVkIjoxLCJyb3RhdGlvbl9zcGVlZF94IjoxLCJyb3RhdGlvbl9zcGVlZF96IjoxLCJjYW1lcmFfeCI6MCwiY2FtZXJhX3kiOjAsImNhbWVyYV96IjoxMCwiZHJhd19heGlzIjp0cnVlfQ=="><img src="/imgs/2012/ms02.jpg" alt="mesh subdivision 2012" /></a></p>
<p>Oh, and since I just <em>love</em> text effects, I added a silly text-manipulation thingie to dynamically alter the innerText of the page contents. Just because!</p>
<p>It&#8217;s fun to play with all the settings and seeing what happens. I have found myself mesmerised just by looking at the end result. Some images seem really organic; others look very fractal/spaceish to me. (Don&#8217;t forget to make it <a href="http://lab.soledadpenades.com/js/mesh_subdivision/#eyJudW1iZXJfcGFzc2VzIjo1LCJiYWNrZ3JvdW5kX2NvbG9yIjoxMjMwMzI5MSwid2lyZWZyYW1lIjp0cnVlLCJ3aXJld2lkdGgiOjEsIndpcmVjb2xvciI6MTY3MTE2ODAsImNvbG9yXzEiOjE2NzExNjgwLCJjb2xvcl8yIjo2NTI4MCwiY29sb3JfMyI6MjU1LCJ3b2JibHkiOnRydWUsIndvYmJseV9zcGVlZCI6OS43MDMyOTY3MDMyOTY3MDMsInJvdGF0aW9uX3NwZWVkX3giOjEsInJvdGF0aW9uX3NwZWVkX3oiOjEsImNhbWVyYV94IjoyMDAsImNhbWVyYV95IjoyMDAsImNhbWVyYV96IjoyMDAsImRyYXdfYXhpcyI6dHJ1ZX0=">wobbly</a>! &#8211;looks like someone is having a discussion inside the pyramid!).</p>
<p>I&#8217;m <em>very</em> happy with the end result, and even more about the development process. I can vividly recall the many hours I spent in 2004 dealing with arrays and memory allocation for such a simple task as this one. Quite unproductive, to be honest! But Javascript + WebGL totally ROCK! Also, after so many years of trying to explain what the demoscene and realtime is about, it&#8217;s great to be able to demonstrate it <em>in realtime</em>. Yay!</p>
<p>I&#8217;ll write about the new &#8220;Simple yet Frequently Asked Questions&#8221; about three.js that I encountered while doing this experiment&#8230; but that will be tomorrow, if you don&#8217;t mind!</p>
<p>Have fun! :)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3947&amp;md5=71ce55931be4ec0f0a529035f5efa2d9" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2012/04/16/mesh-subdivision-2012-extended-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2012%2F04%2F16%2Fmesh-subdivision-2012-extended-edition%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=Mesh+subdivision+%282012+extended+edition%29&amp;amp;description=%0D%0A%0D%0ABack+in+2004%2C+when+I+was+starting+to+experiment+with+OpenGL%2C+I+had+the+idea+of+creating+meshes+from+scratch.+This+will+come+in+handy+for+intros%2C+since+this+way+I+won%27t+be+needing+to+include+the+meshes%27+data+with+the+executable%21%2C+I+thought.%0D%0A%0D%0AI+would+create+a+simple+base+pyramidal+mesh%2C+and+subdivide+it+until+I+got+something+with+more+vertices+and+faces%2C+but+properly+connected+%28not+just+a+bunch+of+random+points%29.+So+I+when+I+had+a+couple+of+free+days%2C+I+came+up+with+a+very+rough+way+of+creating+and+subdividing+meshes.+It+was+very+raw%2C+but+I+loved+that+I+had+done+it+myself%21%0D%0A%0D%0A%0D%0A%0D%0AAt+the+end%2C+I+didn%27t+use+it+for+any+intro+at+all%2C+but+I+used+the+trick+in+a+demo+instead%2C+called+codecolors+%28source+code%2C+if+you%27re+feeling+adventurous%29.+Although+it%27s+hard+to+notice%2C+since+everything+was+generously+blurred+%28I+had+recently+learned+how+to+get+a+cheap+motion+blur+effect+and+was+really+eager+to+use+it+in+every+single+frame%21%29.%0D%0A%0D%0AFast+forward+to+2012.+I+am+learning+%28tinkering+with%3F%29+three.js+and+I+wanted+to+find+out+how+to+manipulate+vertex+data+dynamically.+I+somehow+remembered+this+experiment+and+where+I+left+it+%28Next+step+is+playing+with+the+vertices%21%29%2C+and+I+decided+to+continue+it.%0D%0A%0D%0AOnce+I+got+the+basics+working%2C+I+thought+it+would+be+neat+to+have+some+way+of+interacting+with+the+scene--so+I+added+dat.gui%2C+which+are+the+controls+on+top+right.+Then+I+also+wanted+to+save+the+images%2C+as+I+once+I+added+controls+for+altering+the+look+I+started+getting+great+imagery%21+So+I+added+FileSaver.js+and+canvas-toBlob.js%2C+since+browser+support+for+downloading+images+is+still+a+bit+incomplete+%28so+to+speak%29.%0D%0A%0D%0AAnd+finally+I+thought%3A+hey%2C+storing+images+is+ok%2C+but+what+if+I+want+to+modify+an+existing+configuration%3F.+So+I+added+support+for+storing+the+current+configuration+via+some+simple+location+hash+serialising.+I+am+not+sure+if+it%27s+the+best+way+but+it+seems+to+work+well+enough+for+now%21%0D%0A%0D%0A%0D%0A%0D%0AOh%2C+and+since+I+just+love+text+effects%2C+I+added+a+silly+text-manipulation+thingie+to+dynamically+alter+the+innerText+of+the+page+contents.+Just+because%21%0D%0A%0D%0AIt%27s+fun+to+play+with+all+the+settings+and+seeing+what+happens.+I+have+found+myself+mesmerised+just+by+looking+at+the+end+result.+Some+images+seem+really+organic%3B+others+look+very+fractal%2Fspaceish+to+me.+%28Don%27t+forget+to+make+it+wobbly%21+--looks+like+someone+is+having+a+discussion+inside+the+pyramid%21%29.%0D%0A%0D%0AI%27m+very+happy+with+the+end+result%2C+and+even+more+about+the+development+process.+I+can+vividly+recall+the+many+hours+I+spent+in+2004+dealing+with+arrays+and+memory+allocation+for+such+a+simple+task+as+this+one.+Quite+unproductive%2C+to+be+honest%21+But+Javascript+%2B+WebGL+totally+ROCK%21+Also%2C+after+so+many+years+of+trying+to+explain+what+the+demoscene+and+realtime+is+about%2C+it%27s+great+to+be+able+to+demonstrate+it+in+realtime.+Yay%21%0D%0A%0D%0AI%27ll+write+about+the+new+%22Simple+yet+Frequently+Asked+Questions%22+about+three.js+that+I+encountered+while+doing+this+experiment...+but+that+will+be+tomorrow%2C+if+you+don%27t+mind%21%0D%0A%0D%0AHave+fun%21+%3A%29&amp;amp;tags=demoscene%2Chtml5%2Cjavascript%2Copengl%2Cthree.js%2Cwebgl%2Cblog" type="text/html" />
	</item>
		<item>
		<title>WebGL VGA</title>
		<link>http://soledadpenades.com/2012/04/09/webgl-vga/</link>
		<comments>http://soledadpenades.com/2012/04/09/webgl-vga/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 00:17:53 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[retro]]></category>
		<category><![CDATA[three.js]]></category>
		<category><![CDATA[vga]]></category>
		<category><![CDATA[webgl]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3937</guid>
		<description><![CDATA[For some odd reason, I happened to remember a little demo that came with the first 16 bit computer I ever used&#8211;an Amstrad PC 2286. It was meant for showing off the capabilities of the new and shiny Paradise VGA graphics adapter that the 2000 line of computers incorporated. 256 simultaneous colours! 640&#215;350 graphics! So [...]]]></description>
			<content:encoded><![CDATA[<p>For some odd reason, I happened to remember a little demo that came with the first 16 bit computer I ever used&#8211;an Amstrad PC 2286. It was meant for showing off the capabilities of the new and shiny Paradise VGA graphics adapter that the 2000 line of computers incorporated. 256 simultaneous colours! 640&#215;350 graphics! So stunning back then! I ran this demo from time to time, admiring all those colours at the same time on the screen. Then I learnt how to make the computer output them with GW-Basic, but that is another story.</p>
<p>I wanted to watch that demo again, but it seems that the Amstrad PC line of computers doesn&#8217;t get as much love as the CPCs do. So there is hardly any software repository (or dump) for those computers, and thus I couldn&#8217;t locate that demo. It doesn&#8217;t help that I didn&#8217;t remember its filename either&#8230;</p>
<p>But I found several images depicting the demo, and somehow I decided that <em>rebuilding</em> the demo was an excellent idea &#8211;and a great excuse to learn <a href="https://github.com/mrdoob/three.js">three.js</a>!</p>
<p>This is how the original looked:</p>
<p><img src="/imgs/2012/amstrad_pc2086.jpg" alt="VGA demo" /></p>
<p>And <a href="http://lab.soledadpenades.com/js/webgl_vga/">this</a> is what I ended up with:</p>
<p><a href="http://lab.soledadpenades.com/js/webgl_vga/"><img src="/imgs/2012/amstrad_pc2086_revisited.png" alt="VGA demo + webgl 2012" /></a><br />
<span id="more-3937"></span><br />
In the process I thought I would spice things up a little. Since rotations are &#8220;cheap&#8221; with WebGL, I made things move around, mostly by rotating them. I also added some scaling here and there, and liberally used Math.sin and Math.cos (as Javascript is <em>so</em> fast nowadays too). Still, I didn&#8217;t go too far, for the sake of keeping some resemblance to the original!</p>
<h2>SFAQ</h2>
<p>Although three.js is designed to be simple and intuitive, I had several <q>how do I do this apparently simple thing with three.js?</q> moments, so I thought it would be interesting to write these down as a sort of &#8220;SFAQ&#8221; (Silly Frequently Asked Questions). There we go:</p>
<h3>How do I make a line?</h3>
<p>You first create a geometry, then add vertices to its <em>vertices</em> property. Then create a material to set the line color, and use both the geometry and the material to create a line.</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> geometry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Geometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> material <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">LineBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> color<span style="color: #339933;">:</span> 0xFF0000 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Line</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #339933;">,</span> material<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<p>Actually, you can create the line skipping the material creation. It will get a randomly assigned colour then.</p>
<h3>How do I make a line strip? (i.e. a series of connected segments)</h3>
<p>You start with a geometry, like in the line example above, but add as many points as you require. Then when creating the line you specify that the line type is THREE.LineStrip:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> geometry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Geometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// ... add as many vertices as you need</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> material <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">LineBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> color<span style="color: #339933;">:</span> 0xFF0000 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Line</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #339933;">,</span> material<span style="color: #339933;">,</span> THREE.<span style="color: #660066;">LineStrip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<h3>How do I make a line in which each segment has a different colour?</h3>
<p>The answer to this is <strong>vertex colours</strong>, which works in the same way as the homonym concept in OpenGL: you need to specify a colour per vertex, instead of just a colour for the entire line. The way to specify the colours is to add instances of THREE.Color with the desired colour to the <strong>colors</strong> property, and then tell the line material to use them, as follows:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> geometry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Geometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">colors</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Color</span><span style="color: #009900;">&#40;</span> 0xFF0000 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">colors</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Color</span><span style="color: #009900;">&#40;</span> 0x00FF00 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> material <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">LineBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> vertexColors<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Line</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #339933;">,</span> material<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></code></div>
<p>I&#8217;m using this for the odd colourful figure on top left.</p>
<h3>How do I build hierarchies of objects?</h3>
<p>You can use the generic THREE.Object3D class as container for other objects. When you add children to that object, and move it, the children will move along with the parent:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Object3D</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> geometry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">CubeGeometry</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> cube <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Mesh</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
container.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>cube<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
cube.<span style="color: #660066;">position</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Local x = 0</span><br />
container.<span style="color: #660066;">position</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The cube's 'world' X coordinate at this point is 10</span></code></div>
<p>I used this for the &#8220;moire blocks&#8221; in the middle of the screen.</p>
<h3>How do I change the colour of an object after having instantiated it?</h3>
<p>Simply access its material.color property and change it as required:</p>
<div class="syhi_block"><code><span style="color: #003366; font-weight: bold;">var</span> geometry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Geometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
geometry.<span style="color: #660066;">vertices</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vertex</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Vector3</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> material <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">LineBasicMaterial</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> color<span style="color: #339933;">:</span> 0xFF0000 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #660066;">Line</span><span style="color: #009900;">&#40;</span>geometry<span style="color: #339933;">,</span> material<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// the line is red</span><br />
<br />
<span style="color: #006600; font-style: italic;">// But if we change its green attribute to 1...</span><br />
line.<span style="color: #660066;">material</span>.<span style="color: #660066;">color</span>.<span style="color: #660066;">g</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// ...the line is yellow now!</span></code></div>
<p>This technique is used for animating the colour of the rectangles on top right.</p>
<h2>A call for help!</h2>
<p>If you know the file name, or have a copy of the original demo, please let me know! I would love to watch it again after so many years! Hopefully it will work with DOSBox. Please! :-)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3937&amp;md5=21d51722868ccc951ca3f6167f765e69" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2012/04/09/webgl-vga/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2012%2F04%2F09%2Fwebgl-vga%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=WebGL+VGA&amp;amp;description=For+some+odd+reason%2C+I+happened+to+remember+a+little+demo+that+came+with+the+first+16+bit+computer+I+ever+used--an+Amstrad+PC+2286.+It+was+meant+for+showing+off+the+capabilities+of+the+new+and+shiny+Paradise+VGA+graphics+adapter+that+the+2000+line+of+computers+incorporated.+256+simultaneous+colours%21+640x350+graphics%21+So+stunning+back+then%21+I+ran+this+demo+from+time+to+time%2C+admiring+all+those+colours+at+the+same+time+on+the+screen.+Then+I+learnt+how+to+make+the+computer+output+them+with+GW-Basic%2C+but+that+is+another+story.%0D%0A%0D%0AI+wanted+to+watch+that+demo+again%2C+but+it+seems+that+the+Amstrad+PC+line+of+computers+doesn%27t+get+as+much+love+as+the+CPCs+do.+So+there+is+hardly+any+software+repository+%28or+dump%29+for+those+computers%2C+and+thus+I+couldn%27t+locate+that+demo.+It+doesn%27t+help+that+I+didn%27t+remember+its+filename+either...%0D%0A%0D%0ABut+I+found+several+images+depicting+the+demo%2C+and+somehow+I+decided+that+rebuilding+the+demo+was+an+excellent+idea+--and+a+great+excuse+to+learn+three.js%21%0D%0A%0D%0AThis+is+how+the+original+looked%3A%0D%0A%0D%0A%0D%0A%0D%0AAnd+this+is+what+I+ended+up+with%3A%0D%0A%0D%0A%0D%0A%0D%0AIn+the+process+I+thought+I+would+spice+things+up+a+little.+Since+rotations+are+%22cheap%22+with+WebGL%2C+I+made+things+move+around%2C+mostly+by+rotating+them.+I+also+added+some+scaling+here+and+there%2C+and+liberally+used+Math.sin+and+Math.cos+%28as+Javascript+is+so+fast+nowadays+too%29.+Still%2C+I+didn%27t+go+too+far%2C+for+the+sake+of+keeping+some+resemblance+to+the+original%21%0D%0A%0D%0ASFAQ%0D%0A%0D%0AAlthough+three.js+is+designed+to+be+simple+and+intuitive%2C+I+had+several+how+do+I+do+this+apparently+simple+thing+with+three.js%3F+moments%2C+so+I+thought+it+would+be+interesting+to+write+these+down+as+a+sort+of+%22SFAQ%22+%28Silly+Frequently+Asked+Questions%29.+There+we+go%3A%0D%0A%0D%0AHow+do+I+make+a+line%3F%0D%0A%0D%0AYou+first+create+a+geometry%2C+then+add+vertices+to+its+vertices+property.+Then+create+a+material+to+set+the+line+color%2C+and+use+both+the+geometry+and+the+material+to+create+a+line.%0D%0A%0D%0A%0D%0Avar+geometry+%3D+new+THREE.Geometry%28%29%3B%0D%0A%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%280%2C+0%2C+0%29+%29+%29%3B%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%28100%2C+0%2C+0%29+%29+%29%3B%0D%0A%0D%0Avar+material+%3D+new+THREE.LineBasicMaterial%28%7B+color%3A+0xFF0000+%7D%29%3B%0D%0Avar+line+%3D+new+THREE.Line%28geometry%2C+material%29%3B%0D%0A%0D%0A%0D%0AActually%2C+you+can+create+the+line+skipping+the+material+creation.+It+will+get+a+randomly+assigned+colour+then.%0D%0A%0D%0AHow+do+I+make+a+line+strip%3F+%28i.e.+a+series+of+connected+segments%29%0D%0A%0D%0AYou+start+with+a+geometry%2C+like+in+the+line+example+above%2C+but+add+as+many+points+as+you+require.+Then+when+creating+the+line+you+specify+that+the+line+type+is+THREE.LineStrip%3A%0D%0A%0D%0A%0D%0Avar+geometry+%3D+new+THREE.Geometry%28%29%3B%0D%0A%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%280%2C+0%2C+0%29+%29+%29%3B%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%28100%2C+0%2C+0%29+%29+%29%3B%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%28100%2C+100%2C+0%29+%29+%29%3B%0D%0A%2F%2F+...+add+as+many+vertices+as+you+need%0D%0A%0D%0Avar+material+%3D+new+THREE.LineBasicMaterial%28%7B+color%3A+0xFF0000+%7D%29%3B%0D%0Avar+line+%3D+new+THREE.Line%28geometry%2C+material%2C+THREE.LineStrip%29%3B%0D%0A%0D%0A%0D%0AHow+do+I+make+a+line+in+which+each+segment+has+a+different+colour%3F%0D%0A%0D%0AThe+answer+to+this+is+vertex+colours%2C+which+works+in+the+same+way+as+the+homonym+concept+in+OpenGL%3A+you+need+to+specify+a+colour+per+vertex%2C+instead+of+just+a+colour+for+the+entire+line.+The+way+to+specify+the+colours+is+to+add+instances+of+THREE.Color+with+the+desired+colour+to+the+colors+property%2C+and+then+tell+the+line+material+to+use+them%2C+as+follows%3A%0D%0A%0D%0A%0D%0Avar+geometry+%3D+new+THREE.Geometry%28%29%3B%0D%0A%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%280%2C+0%2C+0%29+%29+%29%3B%0D%0Ageometry.colors.push%28new+THREE.Color%28+0xFF0000+%29+%29%3B%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%28100%2C+0%2C+0%29+%29+%29%3B%0D%0Ageometry.colors.push%28new+THREE.Color%28+0x00FF00+%29+%29%3B%0D%0A%0D%0Avar+material+%3D+new+THREE.LineBasicMaterial%28%7B+vertexColors%3A+true+%7D%29%3B%0D%0Avar+line+%3D+new+THREE.Line%28geometry%2C+material%29%3B%0D%0A%0D%0A%0D%0AI%27m+using+this+for+the+odd+colourful+figure+on+top+left.%0D%0A%0D%0AHow+do+I+build+hierarchies+of+objects%3F%0D%0A%0D%0AYou+can+use+the+generic+THREE.Object3D+class+as+container+for+other+objects.+When+you+add+children+to+that+object%2C+and+move+it%2C+the+children+will+move+along+with+the+parent%3A%0D%0A%0D%0A%0D%0Avar+container+%3D+new+THREE.Object3D%28%29%3B%0D%0Avar+geometry+%3D+new+THREE.CubeGeometry%2810%2C+10%2C+10%29%3B%0D%0Avar+cube+%3D+new+THREE.Mesh%28geometry%29%3B%0D%0A%0D%0Acontainer.add%28cube%29%3B%0D%0A%0D%0A%0D%0Acube.position.x+%3D+0%3B+%2F%2F+Local+x+%3D+0%0D%0Acontainer.position.x+%3D+10%3B+%2F%2F+The+cube%27s+%27world%27+X+coordinate+at+this+point+is+10%0D%0A%0D%0A%0D%0AI+used+this+for+the+%22moire+blocks%22+in+the+middle+of+the+screen.%0D%0A%0D%0AHow+do+I+change+the+colour+of+an+object+after+having+instantiated+it%3F%0D%0A%0D%0ASimply+access+its+material.color+property+and+change+it+as+required%3A%0D%0A%0D%0A%0D%0Avar+geometry+%3D+new+THREE.Geometry%28%29%3B%0D%0A%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%280%2C+0%2C+0%29+%29+%29%3B%0D%0Ageometry.vertices.push%28new+THREE.Vertex%28+new+THREE.Vector3%28100%2C+0%2C+0%29+%29+%29%3B%0D%0A%0D%0Avar+material+%3D+new+THREE.LineBasicMaterial%28%7B+color%3A+0xFF0000+%7D%29%3B%0D%0Avar+line+%3D+new+THREE.Line%28geometry%2C+material%29%3B%0D%0A%0D%0A%2F%2F+the+line+is+red%0D%0A%0D%0A%2F%2F+But+if+we+change+its+green+attribute+to+1...%0D%0Aline.material.color.g+%3D+1%3B%0D%0A%0D%0A%2F%2F+...the+line+is+yellow+now%21%0D%0A%0D%0A%0D%0AThis+technique+is+used+for+animating+the+colour+of+the+rectangles+on+top+right.%0D%0A%0D%0AA+call+for+help%21%0D%0A%0D%0AIf+you+know+the+file+name%2C+or+have+a+copy+of+the+original+demo%2C+please+let+me+know%21+I+would+love+to+watch+it+again+after+so+many+years%21+Hopefully+it+will+work+with+DOSBox.+Please%21+%3A-%29&amp;amp;tags=javascript%2Cretro%2Cthree.js%2Cvga%2Cwebgl%2Cblog" type="text/html" />
	</item>
		<item>
		<title>A first impression on Ruby&#8217;s Mechanize</title>
		<link>http://soledadpenades.com/2012/03/17/a-first-impression-on-rubys-mechanize/</link>
		<comments>http://soledadpenades.com/2012/03/17/a-first-impression-on-rubys-mechanize/#comments</comments>
		<pubDate>Sat, 17 Mar 2012 11:47:29 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[irb]]></category>
		<category><![CDATA[mechanize]]></category>
		<category><![CDATA[nokogiri]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[screen scrapping]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3927</guid>
		<description><![CDATA[TL;DR: If you need to do screen scrapping, use ruby's Mechanize.]]></description>
			<content:encoded><![CDATA[<p>I had to do some screen scrapping yesterday and, while I previously have used <a href="https://github.com/hpricot/hpricot">hpricot</a> for these kinds of things (or maybe even just plain <a href="http://curl.haxx.se/">cURL</a> or similar), this time the page required me to be logged in, and not using any HTTP &#8220;standard&#8221; way of Auth that cURL could deal with, but a custom CMS html-based login form. So I understood that I needed something more powerful; something that could pass as a &#8220;proper&#8221; browser and not just a simple crawler.</p>
<p>I had heard quite nice successful experiences with Mechanize before, so I decided I would give it a try, and it turned out to vastly exceed what I expected from it! :-)</p>
<p><a href="http://mechanize.rubyforge.org/">Mechanize</a> is a port of the original <a href="http://search.cpan.org/dist/WWW-Mechanize/">Perl  Mechanize</a> library; there is a <a href="http://wwwsearch.sourceforge.net/mechanize/">Python port</a> too, and there might be ports for other languages too but I wasn&#8217;t interested in that.</p>
<p>For some reason my mind was in the ruby-mood yesterday, so I decided to go for the ruby port. Everything was flowing nicely between ruby and me. I even dared prototyping what I wanted to do in a terminal, using <a href="http://en.wikipedia.org/wiki/Interactive_Ruby_Shell">irb</a>, before writing the final script I needed to write. My ruby muscle was getting toned again, so to speak.</p>
<p>I got some false starts, though. I first invoked just &#8216;ruby&#8217; in the terminal, only to be greeted with a waiting pipe, i.e. nothing happened as &#8211;I guess&#8211; ruby was expecting me to provide him with something to run. I remembered that the interactive ruby executable was called irb. A Control+C and four other keystrokes later, I was in immediate-mode ruby.</p>
<p>Another gotcha: not having to
<div class="syhi_block"><code><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span></code></div>
<p> when running in irb, but having to when writing a script. Thankfully that I remembered and was able to fix quickly.</p>
<p>But I&#8217;m digressing. Back to Mechanize: it simulates a real browser interacting with a website. You can even fake the user agent. But unlike the most basic screen-scrappers, which simply download a page and then do something with it and then download another one and do something else, without keeping any sort of continuity between downloads and connections, Mechanize is stateful. Which means that it keeps the state between &#8216;visited&#8217; pages. To all effects, and for the visited websites, there&#8217;s a normal browser at the other end of the line. Unless you go crazy and begin hammering a server with tons of requests, crawling websites this way might be almost invisible to servers.</p>
<p>Which is exactly what I wanted!</p>
<p>The syntax is quite nice and intuitive. Borrowing shamelessly from the manual:</p>
<div class="syhi_block"><code><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mechanize'</span><br />
<br />
agent = Mechanize.<span style="color:#9900CC;">new</span><br />
page = agent.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://google.com/'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#008000; font-style:italic;"># List links on the page</span><br />
page.<span style="color:#9900CC;">links</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>link<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> link.<span style="color:#9900CC;">text</span>, link.<span style="color:#9900CC;">href</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Click on the first link with text 'News'</span><br />
page = agent.<span style="color:#9900CC;">page</span>.<span style="color:#9900CC;">link_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'News'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">click</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Do a Google search, using the search form</span><br />
google_form = page.<span style="color:#9900CC;">form</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'f'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
google_form.<span style="color:#9900CC;">q</span> = <span style="color:#996600;">'ruby mechanize'</span><br />
page = agent.<span style="color:#9900CC;">submit</span><span style="color:#006600; font-weight:bold;">&#40;</span>google_form, google_form.<span style="color:#9900CC;">buttons</span>.<span style="color:#9900CC;">first</span><span style="color:#006600; font-weight:bold;">&#41;</span></code></div>
<p>Something that made me even more happier is that Mechanize also uses <a href="http://nokogiri.org/">Nokogiri</a> internally for parsing HTML&#8211;which means we can do the same style of nice DOM tree traversing that I used to do with Hpricot, only even better! (Nokogiri is the successor to Hpricot).</p>
<p>I didn&#8217;t need that last feature for this particular case, but it left me wondering what I could use Mechanize for&#8211;in order to use this! I will have a look at my TODO list and see where can I use my newly acquired Mechanize skills!</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3927&amp;md5=052672dc02fda8f87b6b023a6e61ab68" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2012/03/17/a-first-impression-on-rubys-mechanize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2012%2F03%2F17%2Fa-first-impression-on-rubys-mechanize%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=A+first+impression+on+Ruby%26%238217%3Bs+Mechanize&amp;amp;description=I+had+to+do+some+screen+scrapping+yesterday+and%2C+while+I+previously+have+used+hpricot+for+these+kinds+of+things+%28or+maybe+even+just+plain+cURL+or+similar%29%2C+this+time+the+page+required+me+to+be+logged+in%2C+and+not+using+any+HTTP+%22standard%22+way+of+Auth+that+cURL+could+deal+with%2C+but+a+custom+CMS+html-based+login+form.+So+I+understood+that+I+needed+something+more+powerful%3B+something+that+could+pass+as+a+%22proper%22+browser+and+not+just+a+simple+crawler.%0D%0A%0D%0AI+had+heard+quite+nice+successful+experiences+with+Mechanize+before%2C+so+I+decided+I+would+give+it+a+try%2C+and+it+turned+out+to+vastly+exceed+what+I+expected+from+it%21+%3A-%29%0D%0A%0D%0AMechanize+is+a+port+of+the+original+Perl++Mechanize+library%3B+there+is+a+Python+port+too%2C+and+there+might+be+ports+for+other+languages+too+but+I+wasn%27t+interested+in+that.%0D%0A%0D%0AFor+some+reason+my+mind+was+in+the+ruby-mood+yesterday%2C+so+I+decided+to+go+for+the+ruby+port.+Everything+was+flowing+nicely+between+ruby+and+me.+I+even+dared+prototyping+what+I+wanted+to+do+in+a+terminal%2C+using+irb%2C+before+writing+the+final+script+I+needed+to+write.+My+ruby+muscle+was+getting+toned+again%2C+so+to+speak.%0D%0A%0D%0AI+got+some+false+starts%2C+though.+I+first+invoked+just+%27ruby%27+in+the+terminal%2C+only+to+be+greeted+with+a+waiting+pipe%2C+i.e.+nothing+happened+as+--I+guess--+ruby+was+expecting+me+to+provide+him+with+something+to+run.+I+remembered+that+the+interactive+ruby+executable+was+called+irb.+A+Control%2BC+and+four+other+keystrokes+later%2C+I+was+in+immediate-mode+ruby.%0D%0A%0D%0AAnother+gotcha%3A+not+having+to+require+%27rubygems%27+when+running+in+irb%2C+but+having+to+when+writing+a+script.+Thankfully+that+I+remembered+and+was+able+to+fix+quickly.%0D%0A%0D%0ABut+I%27m+digressing.+Back+to+Mechanize%3A+it+simulates+a+real+browser+interacting+with+a+website.+You+can+even+fake+the+user+agent.+But+unlike+the+most+basic+screen-scrappers%2C+which+simply+download+a+page+and+then+do+something+with+it+and+then+download+another+one+and+do+something+else%2C+without+keeping+any+sort+of+continuity+between+downloads+and+connections%2C+Mechanize+is+stateful.+Which+means+that+it+keeps+the+state+between+%27visited%27+pages.+To+all+effects%2C+and+for+the+visited+websites%2C+there%27s+a+normal+browser+at+the+other+end+of+the+line.+Unless+you+go+crazy+and+begin+hammering+a+server+with+tons+of+requests%2C+crawling+websites+this+way+might+be+almost+invisible+to+servers.%0D%0A%0D%0AWhich+is+exactly+what+I+wanted%21%0D%0A%0D%0AThe+syntax+is+quite+nice+and+intuitive.+Borrowing+shamelessly+from+the+manual%3A%0D%0A%0D%0A%0D%0Arequire+%27rubygems%27%0D%0Arequire+%27mechanize%27%0D%0A%0D%0Aagent+%3D+Mechanize.new%0D%0Apage+%3D+agent.get%28%27http%3A%2F%2Fgoogle.com%2F%27%29%0D%0A%0D%0A%23+List+links+on+the+page%0D%0Apage.links.each+do+%7Clink%7C%0D%0A++puts+link.text%2C+link.href%0D%0Aend%0D%0A%0D%0A%23+Click+on+the+first+link+with+text+%27News%27%0D%0Apage+%3D+agent.page.link_with%28%3Atext+%3D%3E+%27News%27%29.click%0D%0A%0D%0A%23%C2%A0Do+a+Google+search%2C+using+the+search+form%0D%0Agoogle_form+%3D+page.form%28%27f%27%29%0D%0Agoogle_form.q+%3D+%27ruby+mechanize%27%0D%0Apage+%3D+agent.submit%28google_form%2C+google_form.buttons.first%29%0D%0A%0D%0A%0D%0ASomething+that+made+me+even+more+happier+is+that+Mechanize+also+uses+Nokogiri+internally+for+parsing+HTML--which+means+we+can+do+the+same+style+of+nice+DOM+tree+traversing+that+I+used+to+do+with+Hpricot%2C+only+even+better%21+%28Nokogiri+is+the+successor+to+Hpricot%29.%0D%0A%0D%0AI+didn%27t+need+that+last+feature+for+this+particular+case%2C+but+it+left+me+wondering+what+I+could+use+Mechanize+for--in+order+to+use+this%21+I+will+have+a+look+at+my+TODO+list+and+see+where+can+I+use+my+newly+acquired+Mechanize+skills%21&amp;amp;tags=hpricot%2Cirb%2Cmechanize%2Cnokogiri%2Cruby%2Cscreen+scrapping%2Cblog" type="text/html" />
	</item>
		<item>
		<title>My favourite GIMP plug-ins</title>
		<link>http://soledadpenades.com/2012/02/12/my-favourite-gimp-plug-ins/</link>
		<comments>http://soledadpenades.com/2012/02/12/my-favourite-gimp-plug-ins/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 22:11:04 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[gimp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=3896</guid>
		<description><![CDATA[When I used Ubuntu I normally installed a fancy package called gimp-plugin-registry that came with most of the plug-ins I liked, and then some more stuff that I never used. I had learnt to ignore that, but there was still the pain of not knowing where things were. Some filters went to &#8220;Colors&#8221;, others to [...]]]></description>
			<content:encoded><![CDATA[<p>When I used Ubuntu I normally installed a fancy package called gimp-plugin-registry that came with most of the plug-ins I liked, and then some more stuff that I never used. I had learnt to ignore that, but there was still the pain of not knowing where things were. Some filters went to &#8220;Colors&#8221;, others to &#8220;Light and shadow&#8221;, etc. It was extremely hard to remember where each filter was meant to be.</p>
<p>Now that I&#8217;m using Arch I couldn&#8217;t find any equivalent package but I thought that it was OK&#8211;that way I would only install what I really needed to install. In the process I&#8217;ve found a couple more plug-ins that I didn&#8217;t know about, so it&#8217;s been quite productive.</p>
<p>What I&#8217;ve done is I&#8217;ve written a script that takes care of downloading the scripts from the specified URLs, placing them in the appropriate gimp folder (~/.gimp2-6/scripts in my case), and the best of all is that it also patches them so that all are under the <em>Filters/Photo</em> menu entry. Yay! No more wandering around menus and submenus! My brain is now happy, I can just focus on playing with the pictures!</p>
<p>If you want to use this script just <a href="https://raw.github.com/sole/snippets/master/gimp/favourite_plugins/install.rb">download it</a> (it&#8217;s on my <a href="https://github.com/sole/snippets/blob/master/gimp/favourite_plugins/install.rb">snippets</a> repository). You might want to modify it as you wish. There are a couple of TO DO&#8217;s but the file is fully functional (as long as you&#8217;ve got Ruby installed).</p>
<p>Oh, and pardon my rudimentary Ruby. It&#8217;s been ages since I last wrote anything with Ruby. I still don&#8217;t know what I want to use. Ideally I&#8217;d like something as easy as php but with a syntax that is a mix of Python and Ruby, and lots of easy functional and metaprogramming.</p>
<p>I guess my problem is that I know too much, but still so little, not enough. Maybe if I just knew one language, I would be happy with that one. In any case, I&#8217;ll keep on experimenting and try to decide what I like best. Maybe my Japanese side will win over the European one? Who knows!</p>
<p>Meanwhile, be sure to enjoy the filters! Wooo!</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=3896&amp;md5=93933ba93dad096b62848ff634652238" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2012/02/12/my-favourite-gimp-plug-ins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=8399&amp;amp;url=http%3A%2F%2Fsoledadpenades.com%2F2012%2F02%2F12%2Fmy-favourite-gimp-plug-ins%2F&amp;amp;language=en_GB&amp;amp;category=text&amp;amp;title=My+favourite+GIMP+plug-ins&amp;amp;description=When+I+used+Ubuntu+I+normally+installed+a+fancy+package+called+gimp-plugin-registry+that+came+with+most+of+the+plug-ins+I+liked%2C+and+then+some+more+stuff+that+I+never+used.+I+had+learnt+to+ignore+that%2C+but+there+was+still+the+pain+of+not+knowing+where+things+were.+Some+filters+went+to+%22Colors%22%2C+others+to+%22Light+and+shadow%22%2C+etc.+It+was+extremely+hard+to+remember+where+each+filter+was+meant+to+be.%0D%0A%0D%0ANow+that+I%27m+using+Arch+I+couldn%27t+find+any+equivalent+package+but+I+thought+that+it+was+OK--that+way+I+would+only+install+what+I+really+needed+to+install.+In+the+process+I%27ve+found+a+couple+more+plug-ins+that+I+didn%27t+know+about%2C+so+it%27s+been+quite+productive.%0D%0A%0D%0AWhat+I%27ve+done+is+I%27ve+written+a+script+that+takes+care+of+downloading+the+scripts+from+the+specified+URLs%2C+placing+them+in+the+appropriate+gimp+folder+%28%7E%2F.gimp2-6%2Fscripts+in+my+case%29%2C+and+the+best+of+all+is+that+it+also+patches+them+so+that+all+are+under+the+Filters%2FPhoto+menu+entry.+Yay%21+No+more+wandering+around+menus+and+submenus%21+My+brain+is+now+happy%2C+I+can+just+focus+on+playing+with+the+pictures%21%0D%0A%0D%0AIf+you+want+to+use+this+script+just+download+it+%28it%27s+on+my+snippets+repository%29.+You+might+want+to+modify+it+as+you+wish.+There+are+a+couple+of+TO+DO%27s+but+the+file+is+fully+functional+%28as+long+as+you%27ve+got+Ruby+installed%29.%0D%0A%0D%0AOh%2C+and+pardon+my+rudimentary+Ruby.+It%27s+been+ages+since+I+last+wrote+anything+with+Ruby.+I+still+don%27t+know+what+I+want+to+use.+Ideally+I%27d+like+something+as+easy+as+php+but+with+a+syntax+that+is+a+mix+of+Python+and+Ruby%2C+and+lots+of+easy+functional+and+metaprogramming.%0D%0A%0D%0AI+guess+my+problem+is+that+I+know+too+much%2C+but+still+so+little%2C+not+enough.+Maybe+if+I+just+knew+one+language%2C+I+would+be+happy+with+that+one.+In+any+case%2C+I%27ll+keep+on+experimenting+and+try+to+decide+what+I+like+best.+Maybe+my+Japanese+side+will+win+over+the+European+one%3F+Who+knows%21%0D%0A%0D%0AMeanwhile%2C+be+sure+to+enjoy+the+filters%21+Wooo%21&amp;amp;tags=gimp%2Cphp%2Cplugins%2Cpython%2Cruby%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>

