Created
August 8, 2016 09:41
-
-
Save AnnaMag/a3c0246de0d006efede42d94227b73c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var snap = require("./js/snap.svg-min.js"); | |
var under = require("./js/underscore-min.js"); | |
function getRandom (weights, values) { | |
var num = Math.random(), | |
s = 0, | |
lastIndex = weights.length - 1; | |
for (var i = 0; i < lastIndex; ++i) { | |
s += weights[i]; | |
if (num < s) { | |
return values[i]; | |
} | |
} | |
return values[lastIndex]; | |
}; | |
var text_oryg = new String("Out beyond ideas of wrongdoing and rightdoing there is a field. \ | |
I will meet you there. When the soul lies down in that grass the world is too full to talk about"); | |
var text_t = text_oryg.replace(/\./g, ""); | |
var text = text_t.replace(/\s/g, ""); | |
function charCount(str) { | |
return under._(str.split('')).countBy(function(char) { | |
//console.log(char); | |
return char.toLowerCase(); | |
}); | |
} | |
function wordCount(str) { | |
return _(str.split(' ')).countBy(function(char) { | |
return char.replace(/\./g, "").toLowerCase(); | |
}); | |
} | |
function getCharacter() { | |
return String.fromCharCode(65+Math.floor((Math.random()*26))); | |
} | |
function sum(o){ | |
for(var s = 0, i = o.length; i; s += o[--i]); | |
return s; | |
}; | |
var s = charCount(text); | |
var weights = []; | |
var letters = []; | |
for (var key in s) { | |
weights.push(s[key]); | |
letters.push(key); | |
} | |
var w_sum = sum(weights); | |
var weighted_prob = weights.map(function(x){return x / w_sum}); | |
var width = 320; | |
var height = 320; | |
var cols = 4; | |
var rows = 4; | |
var s = snap.Snap("14in","12in"); | |
snap.Snap.load("tiles.svg", function (f) { | |
var g = f.select("g"); | |
s.append(g); | |
for ( i=0; i<cols; i++) { | |
for ( j=0; j<rows; j++) { | |
if (i || j ) { // skip the first cell | |
var h = g.clone(); | |
var x = i*width+10; | |
var y = j*height+30; | |
var tstr = "translate("+x+","+y+")"; | |
h.transform(tstr); | |
$( "tspan" ).each(function( index ) { | |
//$(this).text(getCharacter()); | |
$(this).text(getRandom (weighted_prob, letters)); | |
}); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment