/* Copyright (C) 2013 Gregory Schier Copyright (C) 2013 Martin Zimmermann Inspired by http://codepen.io/gschier/pen/GLvAy */ define(["app/lib/promise", "app/config"], function(Q, config) { "use strict"; // Number of squares width and height var GRID = 5; var pad = function(n, width) { return n.length >= width ? n : new Array(width - n.length + 1).join("0") + n; }; /** * Fill in a square on the canvas. */ var fill = function(svg, x, y, padding, size, color) { var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); rect.setAttribute("x", padding + x * size); rect.setAttribute("y", padding + y * size); rect.setAttribute("width", size); rect.setAttribute("height", size); rect.setAttribute("style", "fill: " + color); svg.appendChild(rect); }; /** * Pick random squares to fill in. */ var generateIdenticon = function(key, padding, size) { var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); svg.setAttribute("version", "1.1"); svg.setAttribute("viewBox", "0 0 " + size + " " + size); svg.setAttribute("preserveAspectRatio", "xMinYMin meet"); svg.setAttribute("shape-rendering", "crispEdges"); fill(svg, 0, 0, 0, size + 2*padding, config["avatar-bg"]); if (typeof key === null) { return svg; } Q.when(key, function(key) { var hash = pad((parseInt(key, 16) % Math.pow(2, 18)).toString(2), 18), index = 0; svg.setAttribute("data-hash", key); var i = parseInt(hash.substring(hash.length - 3, hash.length), 2), color = config["avatar-fg"][i % config["avatar-fg"].length]; for (var x=0; x