Browse files

Conform code to Google JavaScript standards

Provided general clean up to the source with the goal of making it more
readable and also to align its style with Google's JavaScript Style
Guide which will be used by the project going forward. Through the
process a number of inefficiencies were eliminated in the code.

See also:
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
  • Loading branch information...
1 parent f4fefc9 commit 6c06c94cdab5bde50286ee319ed074f0c0532405 @gsmith85 committed with Mar 3, 2013
Showing with 55 additions and 54 deletions.
  1. +55 −54 seurat.js
View
109 seurat.js
@@ -1,69 +1,70 @@
/*!
- * Seurat JavaScript Plugin v0.1
* http://seuratjs.com/
*
* Tested with Raphael JavaScript Library v2.1
* http://raphaeljs.com/
*
- * Copyright 2012, Greg Smith
+ * Copyright 2012, 2013 Greg Smith
* http://gregoryryansmith.com/
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
-Raphael.fn.seurat = function (settings) {
- settings = settings || {};
- var imageSource = settings.imageSource || null;
- var step = settings.step || 10;
- var shape = settings.shape || "circ";
- var attributes = settings.attributes || {stroke: "none"};
-
- var animator = settings.animator || null;
- var generator = settings.generator || null;
+Raphael.fn.seurat = function(settings) {
+ settings = settings || {};
+ var imageSource = settings.imageSource,
+ step = settings.step || 10,
+ shape = settings.shape || "circ",
+ attributes = settings.attributes || {stroke: "none"},
+ animator = settings.animator,
+ generator = settings.generator;
- var ppr = this;
- var can = document.createElement('canvas');
- var ctx = can.getContext('2d');
- var colorData;
+ var paper = this,
+ canvas = document.createElement('canvas'),
+ context = canvas.getContext('2d'),
+ colorData;
- function getColorAt(x, y) {
- var index = ((can.width * y) + x) * 4;
- return "rgb(" + colorData[index] + ", " + colorData[index + 1] + ", " + colorData[index + 2] + ")";
- }
-
- var center = step/2;
- var radius = center-1;
-
- function defaultRect(x,y,clr) {
- return ppr.rect(x, y, step, step).attr({fill: clr}).attr(attributes);
- }
+ var center = step / 2,
+ radius = center - 1;
+
+ var getColorAt = function(x, y) {
+ var index = ((canvas.width * y) + x) * 4;
+ return "rgb(" + colorData[index] + ", " + colorData[index + 1] + ", " + colorData[index + 2] + ")";
+ }
+
+ var defaultRect = function(x, y, clr) {
+ return paper.rect(x, y, step, step).attr({fill: clr}).attr(attributes);
+ }
- function defaultCirc(x,y,clr) {
- return ppr.circle(x+center, y+center, radius).attr({fill: clr}).attr(attributes);
- }
+ var defaultCirc = function(x, y, clr) {
+ return paper.circle(x+center, y+center, radius).attr({fill: clr}).attr(attributes);
+ }
- var img = new Image();
- img.onload = function(){
- can.width = img.width;
- can.height = img.height;
- ctx.drawImage(img, 0, 0, img.width, img.height);
- colorData = ctx.getImageData(0, 0, img.width, img.height).data;
+ var img = new Image();
+ img.onload = function() {
+ var x = 0,
+ y = 0,
+ element,
+ color;
+
+ canvas.width = img.width;
+ canvas.height = img.height;
+ context.drawImage(img, 0, 0, img.width, img.height);
+ colorData = context.getImageData(0, 0, img.width, img.height).data;
- for(var x = 0; x < can.width; x += step)
- for(var y = 0; y < can.height; y+= step){
- var elm;
- var color = getColorAt(x+center,y+center);
-
- if(generator != null){
- elm = generator(ppr,x,y,color,step,attributes);
- }
- else{
- if(shape == "rect") elm = defaultRect(x,y,color);
- else elm = defaultCirc(x,y,color);
- }
-
- if(animator!=null) animator(elm,x,y,step);
- }
- }
- img.crossOrigin = '';
- img.src = imageSource;
-};
+ for (; x < canvas.width; x += step) {
+ for (y = 0; y < canvas.height; y += step) {
+ color = getColorAt(x + center, y + center);
+
+ element = (generator) ? generator(paper, x, y, color, step, attributes) :
+ (shape == "rect") ? defaultRect(x, y, color) : defaultCirc(x, y, color);
+
+ if (animator) {
+ animator(element, x, y, step);
+ }
+ }
+ }
+ }
+
+ img.crossOrigin = '';
+ img.src = imageSource;
+}

0 comments on commit 6c06c94

Please sign in to comment.