Permalink
Browse files

initial checkin

  • Loading branch information...
0 parents commit 37c19826049926ec59a56b28105ad18417cc97a0 @odogono committed May 8, 2012
Showing with 987 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +18 −0 LICENSE
  3. +6 −0 README.md
  4. +203 −0 examples/colour_value.js
  5. +94 −0 examples/demo1.html
  6. +79 −0 examples/demo2.html
  7. +195 −0 examples/random_squiggle.js
  8. +39 −0 examples/request_animation_frame.js
  9. +19 −0 package.json
  10. +113 −0 pm_prng.js
  11. +220 −0 test/test.pm_prng.js
@@ -0,0 +1 @@
+node_modules
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright (c) 2009 Michael Baczynski, http://www.polygonal.de
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
+
+
+A good Pseudo-Random Number Generator (PRNG)
+
+Ported from actionscript original code at http://lab.polygonal.de/?p=162
+
@@ -0,0 +1,203 @@
+/**
+ * A utility class for converting a color value into different components.
+ *
+ * This class is licensed under Creative Commons Attribution 3.0 License:
+ * http://creativecommons.org/licenses/by/3.0/
+ * Essentially you are free to use this class in any way you want, but anywhere
+ * you list credits for a work which uses this class you need to also acknowledge
+ * this classes author.
+ *
+ * @author Noel Billig (http://www.dncompute.com)
+ * @author Alexander Veenendaal (http://opendoorgonorth.com/)
+ */
+(function (definition) {
+
+ // RequireJS/CommonJS/<script> adapter taken from https://raw.github.com/kriskowal/q/master/q.js by Kris Kowal
+ // This file will function properly as a <script> tag, or a module
+ // using CommonJS and NodeJS or RequireJS module formats. In
+ // Common/Node/RequireJS, the module exports the ColourValue API and when
+ // executed as a simple <script>, it creates a ColourValue global instead.
+
+ // RequireJS
+ if (typeof define === "function") {
+ define(definition);
+
+ // CommonJS
+ } else if (typeof exports === "object") {
+ definition(void 0, exports);
+
+ // <script>
+ } else {
+ definition(void 0, ColourValue = {});
+ }
+
+})(function (require, exports) {
+
+/**
+ * @return a generic object with the following values:
+ * h:Number - a number from 0 to 360 representing the hue of the color
+ * s:Number - a number from 0 to 1 representing the saturation
+ * v:Number - a number from 0 to 1 representing the "value" (brightness) of the color
+ */
+exports.RGBToHSV = function(r, g, b) {
+ var max = Math.max(r, g);
+ max = Math.max(max,b);
+
+ var min = Math.min(r, g);
+ min = Math.min(min,b);
+
+ var h;
+ var s = (max !== 0 ? (max - min) / max : 0);
+ var v = max / 255;
+
+ if (s == 0) {
+
+ h = null;
+
+ } else {
+
+ var delta = max - min;
+
+ if (r == max) {
+ h = (g-b) / delta;
+ } else if (g == max) {
+ h = 2 + (b-r)/delta;
+ } else if (b == max) {
+ h = 4.0 + (r-g)/delta;
+ }
+
+ h *= 60;
+
+ if (h < 0) {
+ h += 360;
+ }
+ }
+
+ return {
+ h:h,
+ s:s,
+ v:v
+ };
+}
+
+
+exports.HSVToRGB = function(h, s, v) {
+ var result = exports.create();
+
+ if (s === 0) {
+ if (!h) {
+ result.r = v;
+ result.g = v;
+ result.b = v;
+ return result;
+ } else {
+ return null; // error
+ }
+ } else {
+
+ if (h == 360) {
+ h = 0;
+ }
+ h /= 60;
+
+ var i = Math.floor(h);
+ var f = h - i;
+ var p = v * (1 - s);
+ var q = v * (1 - (s*f));
+ var t = v * (1 - (s * (1-f)));
+ var temp = [[v, t, p], [q, v, p], [p, v, t], [p, q, v], [t, p, v], [v, p, q]][i];
+
+ return {
+ r: temp[0],
+ g: temp[1],
+ b: temp[2]
+ };
+ }
+}
+
+var ColourValue = function(){
+ this.r = 0;
+ this.g = 0;
+ this.b = 0;
+ this.a = 255;
+}
+
+
+
+ColourValue.prototype.invert = function() {
+ this.r = 255-this.r;
+ this.g = 255-this.g;
+ this.b = 255-this.b;
+}
+
+
+ColourValue.prototype.setHex = function(hexStr){
+ var val = parseInt(hexStr,16);
+ this.setRGB( val );
+}
+
+ColourValue.prototype.getHex = function() {
+ return this.toInteger().toString(16);
+}
+
+
+ColourValue.prototype.setRGB = function(r,g,b,a) {
+ // TODO : complete alpha processing
+ if( typeof g === 'undefined' && typeof b === 'undefined' ){
+ this.r = (r>>16);
+ this.g = (r >> 8 ^ this.r << 8);
+ this.b = (r ^ (this.r << 16 | this.g << 8));
+ } else {
+ this.r = Math.max(0,Math.min(r,255));
+ this.g = Math.max(0,Math.min(g,255));
+ this.b = Math.max(0,Math.min(b,255));
+ }
+ if( typeof a !== 'undefined' ){
+ this.a = a;
+ }
+};
+
+ColourValue.prototype.clone = function(){
+ return exports.create( this.r, this.g, this.b );
+}
+ColourValue.prototype.toInteger = function(){
+ // TODO : complete alpha processing
+ return (this.r<<16 | this.g<<8 | this.b);
+}
+
+ColourValue.prototype.toString = function(){
+ var result = Number( this.toInteger() ).toString(16).toUpperCase();
+ while (result.length < 6) {
+ result = "0" + result;
+ }
+ return '#' + result;
+}
+
+/**
+* h:Number - A number from 0 t 360
+* s:Number - a number from 0 to 1
+* v:Number - a number from 0 to 1
+*/
+ColourValue.prototype.setHSV = function(r,g,b) {
+ var rgb = exports.HSVToRGB( h,s,b );
+ this.setRGB( rgb.r, rgb.g, rgb.b );
+}
+
+/**
+ * Returns a generic object with the following values:
+ * h:Number - a number from 0 to 360 representing the hue of the color
+ * s:Number - a number from 0 to 1 representing the saturation
+ * v:Number - a number from 0 to 1 representing the "value" (brightness) of the color
+ */
+ColourValue.prototype.toHSV = function(){
+ return exports.RGBToHSV( this.r, this.g, this.b );
+}
+
+exports.create = function(r,g,b,a){
+ var result = new ColourValue();
+ result.setRGB(r,g,b,a);
+ return result;
+}
+
+
+});
@@ -0,0 +1,94 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <title>Park Miller Pseudo Random Number Generation - Demo 1 - Random Dots</title>
+ <style type="text/css">
+ body {
+ margin: 0;
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+ color: #333333;
+ background-color: #ffffff;
+ }
+ #canvas{
+ width: 500px; height:500px; background-color: #333;
+ }
+ .instr { float: left; }
+ .seedEntry{ float: left; margin-left: 10em; }
+ </style>
+ </head>
+ <body>
+ <canvas id="canvas" width="500px" height="500px">
+ </canvas>
+ <div>
+ <div class="instr">Click to change seed</div>
+ <div class="seedEntry">Seed #<input id="seed" type="text" /></div>
+ </div>
+ <br style="clear:both;" />
+
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <script>window.jQuery || document.write('<script src="jquery-1.7.2.min.js"><\/script>')</script>
+ <script src="../pm_prng.js"></script>
+ <script src="colour_value.js"></script>
+ <script src="request_animation_frame.js"></script>
+ <script type="text/javascript">
+
+ var $canvas = $('#canvas');
+ var $seedField = $('#seed');
+ var width = $canvas.width();
+ var height = $canvas.height();
+ var context = $canvas.get(0).getContext('2d');
+ var random = PM_PRNG.create();
+
+ // clears the canvas and takes the seed either from the input field or from the generator
+ var resetCanvas = function(takeFromField){
+ var seed = takeFromField ? parseInt( $seedField.val() ) : random.nextInt();
+ random.seed = seed;
+ $seedField.val(seed);
+ context.fillStyle = '#000000';
+ context.fillRect( 0,0,width,height );
+ };
+
+ // draws a single pixel of random colour and position to the canvas
+ var draw = function(){
+ var colour = ColourValue.create(
+ random.nextIntRange(0,255),
+ random.nextIntRange(0,255),
+ random.nextIntRange(0,255) );
+
+ context.fillStyle = colour.toString();
+ var x = random.nextIntRange( 0, width-1 );
+ var y = random.nextIntRange( 0, height-1 );
+ context.fillRect( x, y, 2, 2 );
+ };
+
+ // main loop - requests next frame and draws
+ var animate = function(){
+ requestAnimationFrame( animate );
+ draw();
+ };
+
+ // when the canvas is clicked, reset with a new random seed
+ $canvas.on('click', function(){
+ resetCanvas();
+ });
+
+ // when the user enters a new seed, reset
+ $seedField.on('change', function(){
+ resetCanvas( true );
+ }).keypress( function(e){
+ if( e.which === 13 ){
+ resetCanvas( true );
+ }
+ });
+
+ // clear canvas and choose initial seed
+ resetCanvas();
+ // begin animation loop
+ animate();
+
+ </script>
+
+ </body>
+</html>
Oops, something went wrong.

0 comments on commit 37c1982

Please sign in to comment.