/* global define, attachPloygonShader, PolygonShader */
/*jslint bitwise: true */
(function (window) {
  'use strict';
  var coordinates2d = function (ctx, params) {
    var coordinatePosition = {
      corners: [1, -1, 0, -1, -1, 0, -1, 1, 0],
      colors: [0, 0, 0, 0, 0, 0, 0, 0, 0],
      xTicks: [],
      xTicksColor: [],
      xTickLength: 0.1,
      yTicks: [],
      yTicksColor: [],
      yTickLength: 0.1,
      xAxis: -1,
      yAxis: -1
    }
    var ticks = [];
    var ticksColor = [];
    var lineRenderer = attachPloygonShader(ctx);
    var setVertex = function (array, position) {
      for (var i = 0; i < 3; i++)
        array.push(position[i]);
    }
    if (params) {
      if (params.lineWidth !== 'undefined')
        ctx.lineWidth(params.lineWidth);
      if (params.x) {
        if (params.x.distance !== 'undefined') {
          coordinatePosition.xAxis = -1 + (params.x.distance * 0.02);
          coordinatePosition.corners[1] = coordinatePosition.xAxis;
          coordinatePosition.corners[4] = coordinatePosition.xAxis;
        }
      }
      if (params.y) {
        if (params.y.distance !== 'undefined') {
          coordinatePosition.yAxis = -1 + (params.y.distance * 0.02);
          coordinatePosition.corners[3] = coordinatePosition.yAxis;
          coordinatePosition.corners[6] = coordinatePosition.yAxis;
        }
      }
      if (params.x) {
        if (params.x.ticks) {
          if (params.x.tickLength) {
            coordinatePosition.xTickLength = params.x.tickLength;
          }
          var dist = (-coordinatePosition.yAxis + 1) / (params.x.ticks + 1);
          for (var i = 0; i < params.x.ticks; i++) {
            setVertex(coordinatePosition.xTicks, [dist * (i + 1) + coordinatePosition.yAxis, coordinatePosition.xAxis + coordinatePosition.xTickLength, 0]);
            setVertex(coordinatePosition.xTicksColor, [0, 0, 0]);
            setVertex(coordinatePosition.xTicks, [dist * (i + 1) + coordinatePosition.yAxis, coordinatePosition.xAxis - coordinatePosition.xTickLength, 0]);
            setVertex(coordinatePosition.xTicksColor, [0, 0, 0]);
            if (params.x.grid) {
              var num = Math.floor((1 - coordinatePosition.yAxis) * 10);
              for (var k = 0; k < num; k++) {
                setVertex(coordinatePosition.xTicks, [dist * (i + 1) + coordinatePosition.yAxis, coordinatePosition.xAxis + 0.025 + 0.1 * (k + 1), 0]);
                setVertex(coordinatePosition.xTicksColor, [0.5, 0.5, 0.5]);
                setVertex(coordinatePosition.xTicks, [dist * (i + 1) + coordinatePosition.yAxis, coordinatePosition.xAxis - 0.025 + 0.1 * (k + 1), 0]);
                setVertex(coordinatePosition.xTicksColor, [0.5, 0.5, 0.5]);
              }
            }
          }
        }
      }
      if (params.y) {
        if (params.y.ticks) {
          if (params.y.tickLength) {
            coordinatePosition.yTickLength = params.y.tickLength;
          }
          var dist = (-coordinatePosition.xAxis + 1) / (params.y.ticks + 1);
          for (var i = 0; i < params.y.ticks; i++) {
            setVertex(coordinatePosition.yTicks, [coordinatePosition.yAxis + coordinatePosition.yTickLength, dist * (i + 1) + coordinatePosition.xAxis, 0]);
            setVertex(coordinatePosition.yTicksColor, [0, 0, 0]);
            setVertex(coordinatePosition.yTicks, [coordinatePosition.yAxis - coordinatePosition.yTickLength, dist * (i + 1) + coordinatePosition.xAxis, 0]);
            setVertex(coordinatePosition.yTicksColor, [0, 0, 0]);
            if (params.y.grid) {
              var num = Math.floor((1 - coordinatePosition.xAxis) * 10);
              for (var k = 0; k < num; k++) {
                setVertex(coordinatePosition.yTicks, [coordinatePosition.yAxis + 0.025 + 0.1 * (k + 1), dist * (i + 1) + coordinatePosition.xAxis, 0]);
                setVertex(coordinatePosition.yTicksColor, [0, 0, 0]);
                setVertex(coordinatePosition.yTicks, [coordinatePosition.yAxis - 0.025 + 0.1 * (k + 1), dist * (i + 1) + coordinatePosition.xAxis, 0]);
                setVertex(coordinatePosition.yTicksColor, [0, 0, 0]);
              }
            }
          }
        }
      }
    }
    var self = {
      draw: function (p) {
        var width = ctx.canvas.width;
        var height = ctx.canvas.height;
        lineRenderer.draw(coordinatePosition.corners, coordinatePosition.colors, 'LINE_STRIP');
        if (params.xTicks !== 'undefined') {
          lineRenderer.draw(coordinatePosition.xTicks, coordinatePosition.xTicksColor, 'LINES');
        }
        if (params.yTicks !== 'undefined') {
          lineRenderer.draw(coordinatePosition.yTicks, coordinatePosition.yTicksColor, 'LINES');
        }
      },
      lineWidth: function (lineWidth) {
        params.lineWidth = lineWidth;
        ctx.lineWidth(lineWidth);
      }
    };
    return self;
  };
  if (typeof module === 'object' && module && typeof module.exports === 'object') {
    module.exports = coordinates2d;
  } else {
    window.coordinates2d = coordinates2d;
    if (typeof define === 'function' && define.amd) {
      define(coordinates2d);
    }
  }
})(window);
@@ -192,24 +192,51 @@
 sition = %7B%0D%0A
+      lineRenderer: null,%0D%0A
       corner
@@ -268,24 +268,24 @@
 -1, 1, 0%5D,%0D%0A
-
       colors
@@ -502,59 +502,27 @@
     
-var ticks = %5B%5D;%0D%0A    var ticksColor = %5B%5D;%0D%0A    var 
+coordinatePosition.
 line
@@ -730,32 +730,16 @@
 ineWidth
- !== 'undefined'
 )%0D%0A     
@@ -827,32 +827,16 @@
 distance
- !== 'undefined'
 ) %7B%0D%0A   
@@ -1076,32 +1076,32 @@
 f (params.y) %7B%0D%0A
+
         if (para
@@ -1113,32 +1113,16 @@
 distance
- !== 'undefined'
 ) %7B%0D%0A   
@@ -4336,32 +4336,51 @@
 eight;%0D%0A        
+coordinatePosition.
 lineRenderer.dra
@@ -4500,32 +4500,51 @@
 ') %7B%0D%0A          
+coordinatePosition.
 lineRenderer.dra
@@ -4678,24 +4678,43 @@
 %0D%0A          
+coordinatePosition.
 lineRenderer

refactor linerenderer