<?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>squaredesign</title>
	<atom:link href="http://squaredesign.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://squaredesign.com</link>
	<description></description>
	<lastBuildDate>Sat, 13 Apr 2013 18:10:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>javascript, math, and color</title>
		<link>http://squaredesign.com/writes/javascript-math-and-color/</link>
		<comments>http://squaredesign.com/writes/javascript-math-and-color/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 13:07:36 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://squaredesign.com/?p=852</guid>
		<description><![CDATA[an article about using javascript to do math, and math to make colors.]]></description>
				<content:encoded><![CDATA[<p>in my previous article about the <a href="/writes/the-squares">the squares</a> i showed you how i do the mousemove() triggered, random square manipulation using JQuery and a bit of raw javascript.</p>
<p>in this entry, i&#8217;ll talk about how we can manipulate colors as numbers.</p>
<p><span id="more-852"></span></p>
<h2>step 1: draw the squares</h2>
<p>i&#8217;ve stolen my square-filled &lt;div&gt; from the <a href="/writes/the-squares">previous demo</a>, but this time the squares start out black.</p>
<pre class="brush: css; title: ; notranslate">
.demo-square
{
   float:left;
   width:28px;
   height:28px;
   background:#000;
   margin:1px;
}
</pre>
<p>the javascript that creates them:</p>
<pre class="brush: jscript; title: ; notranslate">
var cols = 10;
var rows = 6;
for(x=0; x &lt; (cols * rows); x++)
{
  $(&quot;&lt;div&gt;&quot;)
  .clone(false)
  .appendTo(&quot;#demo&quot;)
  .addClass(&quot;demo-square&quot;)
}
</pre>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;demo&quot;&gt;&lt;/div&gt;
</pre>
<div id="demo1" class="demo-squares-container"></div>
<p>pretty boring.</p>
<h2>step 2: alter the colors</h2>
<p>notice we make a loop to create the squares. loops are great for incrementally doing math ($something++).</p>
<p>also, colors can be expressed not only in the hexadecimal #AABBCC, but also numerically from 0-225, e.g. rgb(000,120,255)</p>
<p>so if we wanted the squares to fade slowly from black to white, we need to take the number of squares, divide 255 by that number, and add that amount to each of the r,g,b values on every loop. (here i&#8217;m manipulating all r,g,b values equally &#8211; if you want to play with color, you can change them separately)</p>
<p>that loop looks like this:</p>
<pre class="brush: jscript; title: ; notranslate">
var increment = 255 / ((cols * rows)-2);
var current = 0;
for(x=0; x &lt; (cols * rows); x++)
{
   rounded = Math.round(current);
   $(&quot;&lt;div&gt;&quot;)
   .clone(false)
   .appendTo(&quot;#demo&quot;)
   .addClass(&quot;demo-square&quot;)
   .css(&quot;background&quot;,&quot;rgb(&quot;+rounded+&quot;,&quot;+rounded+&quot;,&quot;+rounded+&quot;)&quot;);
   current += increment;
}
</pre>
<div id="demo2" class="demo-squares-container"></div>
<p>notes:</p>
<ul>
<li>rounding &#8211; CSS doesn&#8217;t like decimals in rgb() values</li>
<li>for the increment value, i subtract two from cols * rows, because we want the first one fully black, and the last one fully white.</li>
</ul>
<h2>step 3: random, and other uses</h2>
<p>i know, i don&#8217;t have a big sweeping gradient in the back. i have random shades. the theory is the same, to loop through every square and alter it&#8217;s rgb() value, but instead of incrementing it, we pick a (rounded) random value every time.</p>
<pre class="brush: jscript; title: ; notranslate">
var rand = 0;
for(x=0; x &lt; (cols * rows); x++)
{
  rand = Math.round(Math.random()*255);
  $(&quot;&lt;div&gt;&quot;)
  .clone(false)
  .appendTo(&quot;#demo&quot;)
  .addClass(&quot;demo-square&quot;)
  .css(&quot;background&quot;,&quot;rgb(&quot;+rand+&quot;,&quot;+rand+&quot;,&quot;+rand+&quot;)&quot;);
}
</pre>
<div id="demo3" class="demo-squares-container"></div>
<p>my <a href="/writes/the-squares/">last demo</a> showed how random things could happen with mousemove(). do you want the squares to react to user input as they mouse over them individually? we&#8217;ll tie the change to the hover() event on each square.</p>
<pre class="brush: jscript; title: ; notranslate">
for(x=0; x &lt; (cols * rows); x++)
{
   $(&quot;&lt;div&gt;&quot;)
   .clone(false)
   .appendTo(&quot;#demo&quot;)
   .addClass(&quot;demo-square&quot;)
   .css(&quot;background&quot;,&quot;#000000&quot;)
   .hover(function() {
      rand = Math.round(Math.random()*255);
   $(this).css(&quot;background&quot;,&quot;rgb(&quot;+rand+&quot;,&quot;+rand+&quot;,&quot;+rand+&quot;)&quot;)
  })
}
</pre>
<p>(mouse over the div below to see it in action)</p>
<div id="demo4" class="demo-squares-container"></div>
<p>you could do something interesting with opacity and images (for browsers that support it):</p>
<p>add an image to the background of the container with CSS:</p>
<pre class="brush: css; title: ; notranslate">
#demo
{
  background:url('/images/demo.jpg') top left no-repeat;
}
</pre>
<p>and adjust the opacity of each square to a random value (divided by two here to make it reveal more of the image)</p>
<pre class="brush: jscript; title: ; notranslate">
for(x=0; x &lt; (cols * rows); x++)
{
  $(&quot;&lt;div&gt;&quot;)
  .clone(false)
  .appendTo(&quot;#demo&quot;)
  .addClass(&quot;demo-square&quot;)
  .css(&quot;background&quot;,&quot;#000000&quot;)
  .css(&quot;opacity&quot;,Math.random()/2)
}
</pre>
<div id="demo5" class="demo-squares-container"></div>
<p>is there any practical usage for this? i think so. creating an appealing or joyful experience is practical because it makes the user happy. a happy user will read/learn/buy/share.</p>
<p>i hope you&#8217;ll consider using clever technology to make fun and functional designs for your users. if you do, please <a href="/drop-me-a-note/">drop me a note</a> and let me know how this inspired your own creations!</p>
]]></content:encoded>
			<wfw:commentRss>http://squaredesign.com/writes/javascript-math-and-color/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>the squares</title>
		<link>http://squaredesign.com/writes/the-squares/</link>
		<comments>http://squaredesign.com/writes/the-squares/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 12:59:03 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Garage]]></category>

		<guid isPermaLink="false">http://squaredesign.com/?p=847</guid>
		<description><![CDATA[here's an article explaining how i do the animation in the background of this site. it's all done with HTML, CSS, and JQuery/javascript (no images or Flash)]]></description>
				<content:encoded><![CDATA[<p>visitors to my site have asked me how i was able to make it load quickly with a big image/animation in the background (in the previous design)</p>
<p>the answer: it&#8217;s not a graphic. it&#8217;s all HTML, CSS, and Javascript.</p>
<p><span id="more-847"></span></p>
<p>as a designer / developer, i try to find ways of using the capabilities of the browser to provide advanced design and functionality, instead of pushing a large payload like a Flash movie down to the browser for every effect i want to create.</p>
<p>my inspiration came from John Resig&#8217;s port of the Processing library to <a href="http://ejohn.org/blog/processingjs/">processing.js</a>. there are some really beautiful visuals in those examples, all of which are generated in the browser.  while i didn&#8217;t use processing.js to create my design, i did leverage the <a href="http://jquery.com">JQuery</a> library to do a lot of the heavy lifting for me.</p>
<p>for this demonstration, i won&#8217;t get into the math involved to do the random colors &#8211; that would probably fill another article by itself. instead, let&#8217;s focus on the rendering and animation.</p>
<h2>step 1: pick a container</h2>
<p>for this example, let&#8217;s take this &lt;div&gt; element:</p>
<pre>&lt;div id="demo"&gt;&lt;/div&gt;</pre>
<div id="demo" class="demo-squares-container"></div>
<p>pretty boring.</p>
<h2>step 2: fill it up</h2>
<p>now let&#8217;s fill the &lt;div&gt; with smaller &lt;div&gt;s &#8211; a grid of them 10&#215;6</p>
<p>this CSS will style the small squares that we will create:</p>
<pre class="brush: css; title: ; notranslate">
.demo-square
{
	float:left;
	width:28px;
	height:28px;
	background:#dfd;
	margin:1px;
}
</pre>
<p>here&#8217;s the javascript that creates them:</p>
<pre class="brush: jscript; title: ; notranslate">
var cols = 10;
var rows = 6;
for(x=0; x&amp;lt;(cols * rows); x++)
{
	$(&quot;&amp;lt;div&amp;gt;&quot;)
	.clone(false)
	.appendTo(&quot;#demo&quot;)
	.addClass(&quot;demo-square&quot;)
}
</pre>
<p>that gives us this:</p>
<div id="demo2" class="demo-squares-container"></div>
<p>(i styled them with a bit of margin between so you could see the separate squares)</p>
<h2>step 3: make it go</h2>
<p>while there are other ways to animate the squares, most of them use a lot of CPU time, and would run even when the user wasn&#8217;t watching. i got the idea to tie the animation to the mousemove() event that JQuery provides. this would insure that it wouldn&#8217;t be eating CPU cycles while the user had another tab or window focused. also, i hope it adds a bit of fun!</p>
<p>the CSS for the alternate square style:</p>
<pre class="brush: css; title: ; notranslate">
.demo-square-dark
{
	background:#6a6;
}
</pre>
<p>the javascript that toggles the alternate class on a random square as the mouse moves:</p>
<pre class="brush: jscript; title: ; notranslate">
var demosquares = $('#demo .demo-square');
$(&quot;#demo&quot;).mousemove(function(e)
{
  pick = Math.round(demosquares.length * Math.random());
  $(demosquares[pick]).toggleClass(&quot;demo-square-dark&quot;);
});
</pre>
<p>mouse over this div to see how the mousemove() event triggers that toggle</p>
<div id="demo3" class="demo-squares-container"></div>
<p>and that&#8217;s basically how the animated background of my site works. in another post, i&#8217;ll get into the details of the math that makes the different shades. until then &#8211; check out a <a href="http://squaredesign.com/squares.html">toy i made with the background squares</a> &#8211; you can even select the two colors that it differences between!</p>
]]></content:encoded>
			<wfw:commentRss>http://squaredesign.com/writes/the-squares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>toggling CSS rules</title>
		<link>http://squaredesign.com/writes/toggling-css-rules/</link>
		<comments>http://squaredesign.com/writes/toggling-css-rules/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 12:57:33 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://squaredesign.com/?p=845</guid>
		<description><![CDATA[a brief post demonstrating a technique to quickly enable and disable CSS selectors. i use it toggle debug rules on and off - how will you apply it?]]></description>
				<content:encoded><![CDATA[<p>somewhere in my development career, i observed a quick way of enabling and disabling blocks of code using comments. i use this now in all my stylesheets, to be able to quickly toggle a CSS selector on and off.</p>
<p>if you look at a block comment:</p>
<pre class="css">/* */</pre>
<p>you realize you can stack two of these up on each other:</p>
<pre class="css">/* */ i'm not commented!  /* */</pre>
<p>and by quickly inserting a space in the closing side of the first comment, you can make the comment tag expand to encompass the whole line or block.</p>
<pre class="css">/* * / &lt;-- that space makes me commented now!  /* */</pre>
<p>the finished example:</p>
<pre class="css">/* no space */ #example{} /* enabled */
/* with space * / #example{} /* disabled */</pre>
<p>you can use this to toggle a debug class, e.g.</p>
<pre class="css">/*  */  .todo { font-weight:bold; color:red; } /* */</pre>
]]></content:encoded>
			<wfw:commentRss>http://squaredesign.com/writes/toggling-css-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
