Exemple #1
0
obj.extend = function(){
  var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {},
  i = 1,
  length = arguments.length,
  deep = false;
  if (typeof target === "boolean") {
    console.log('one');
    deep = target;
    target = arguments[1] || {};
    i = 2;
  }
  if (typeof target !== "object" && !is.Function(target)) {
    console.log('two');
    target = {};
  }
  if (length === i) {
    console.log('three');
    target = this;
    --i;
  }
  for (i; i < length; i++) {
    if ((options = arguments[i]) !== null) {
      for (name in options) {
        src = target[name];
        copy = options[name];
        if (target === copy) {
          continue;
        }
        if (deep && copy && (is.PlainObject(copy) || (copyIsArray = is.Array(copy)))) {
          if (copyIsArray) {
            copyIsArray = false;
            clone = src && is.Array(src) ? src : [];
          } else {
            clone = src && is.PlainObject(src) ? src : {};
          }
          target[name] = extend(deep, clone, copy);
        } else if (copy !== undefined) {
          target[name] = copy;
        }
      }
    }
  }
  return target;
};
Exemple #2
0
DOM.make = function(selector, attrs, callback) {
  var tag = extractTag(selector);
  attrs = attrs || {};

  if(!isLegalElement(tag.name)) {
    throw new Error('Illegal tag name "'+tag.name+'" passed to topdown.DOM.make');
  }

  var elements = (is.Array(attrs)) ? attrs : [attrs];
  var multipleElements = elements.length > 1;
  callback = callback || function(){};
  attrs = buildAttributes(attrs, tag.selectors, multipleElements);
  var id = attrs.id;
  var classes = attrs.classes;

  function append(array, callback) {
    var properties = array[0],
        e = document.createElement(tag.name),
        appendto;

    if (properties.appendto){
      appendto = properties.appendto;
      delete properties.appendto;
    }

    for (var att in properties) {
      if (properties.hasOwnProperty(att)) {
        e.setAttribute(att, properties[att]);
      }
    }

    e.onload = e.onreadystatechange = function(e){
      array.shift();
      if(array.length) {
        append(array, callback);
      } else {
        callback();
      }
    };

    if (id) e.id = id;
    for ( var n = 0, file; ($class = classes[n]); n++ ) {
      e.classList.add($class);
    }

    appendto = appendto || 'body';
    return DOM.get(appendto)[0].appendChild(e);
  }

  return append(elements, callback);
};
Exemple #3
0
Collision.areaContainsPoint = function(area, point){

  // check point is a Point
  if (! (point instanceof Point)) {
    throw Error('The point argument for areaContainsPoint should be a Point object.');
  }

  var x_inter;
  var points;

  // check area 'is shape' || 'is array of points'
  if (is.Array(area)) {
    area.forEach(function(e, i){
      if ( ! (e instanceof Point)) {
        throw Error('The area argument for areaContainsPoint should be either a Shape or an array of points');
      }
    });
    points = area;
  } else if (area instanceof Shape) {
    // if shape create an array of points.
    points = area.points;
  } else {
    throw Error('The area argument for areaContainsPoint should be either a Shape or an array of points');
  }

  var counter = 0;
  var p1 = points[0];

  for (var i = 1, l = points.length; i <= l; i++) {
    var p2 = points[i%l];

    if (
      point.y > Math.min(p1.y, p2.y) &&
      point.y <= Math.max(p1.y, p2.y) &&
      point.x <= Math.max(p1.x, p2.x) &&
      p1.y != p2.y
    ) {
      x_inter = (point.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
      if ( p1.x == p2.x || point.x <= x_inter) {
        counter++;
      }
    }
    p1 = p2;
  }

  return ( counter % 2 == 1 );
};
Exemple #4
0
var Constructor = function(x, y){

  if (!is.set(x)) {
    this.x = 0;
    this.y = 0;
  } else if (!is.set(y) && is.Array(x) && x.length == 2) {
    this.x = x[0];
    this.y = x[1];
  } else if (is.PlainObject(x) && x.x && x.y) {
      this.x = x.x;
      this.y = x.y;
  } else {
    this.x = is.Numeric(x) ? x : 0;
    this.y = is.Numeric(y) ? y : 0;
  }

};
Exemple #5
0
var Constructor = function(options) {

  var points;
  var position;
  var opts = {};

  function validateRawArray(points) {
    var size = 2;
    points.forEach(function(point){
      if (point.length != size) throw new Error('Array contains incorrect number of values to make a point ('+size+').');
    });
    return true;
  }

  function arrayOfPoints(points) {
    var allPoints = true;
    points.forEach(function(point){
      var isPoint = point instanceof Point;
      if (allPoints && !isPoint ) {
        allPoints = false;
      }
    });
    return allPoints;
  }

  if (is.Array(options)) {
    points = options;
  } else if (is.set(options.points)) {
    points = options.points;
  }

  if (is.Array(points)) {
    var tmp;
    if (!arrayOfPoints(points)) {
      validateRawArray(points);
      tmp = [];
      points.forEach(function(opt){
        tmp.push(Point(opt));
      });
    } else {
      tmp = points;
    }

    opts.points = tmp;
  }

  // TODO: convert options.position to Point object if array of 2 values

  options = obj.extend({
    points: [
      Point( 20, 20 ),
      Point( 20, 40 ),
      Point( 40, 40 ),
      Point( 40, 20 )
    ]
  }, options, opts);

  this.points = options.points;

  options = obj.extend({
    position: this.centroid()
  }, options, opts);

  var pos = options.position;

  if (is.Array(pos)) {
    pos = Point(pos);
  }

  this.move(this.centroid().invert());
  this.move(pos);

  this.position = this.centroid();

};