js 그림 라이브러리

8315 단어 js그림을 그리다
document.write('<script type="text/javascript" src="./common.js"></script>')
Point = function(ctx){
  if (typeof(ctx) !== "undefined") {
    this.ctx = ctx
  }
  this.weight = 5;
  var now = new Date();
  this.sid = "Object" + Date.parse(now) + parseInt(Math.random()*1000);
  eval(this.sid + "=this");
  return this;
};
Point.prototype = {
  setContext: function(ctx) {
                this.ctx = ctx;
            },
  setColor: function(color) {
          this.color = color;
        },
  setAuchor: function(auchor, color) {
           this.auchor = auchor;
           this.auchorColor = color;
         },
  draw: function(point, weight) {
      this.point = point;
      if (typeof(weight) !== "undefined") {
        this.weight = weight;
      }
      this.id = setInterval(this.sid + ".doDraw()", 1);
    },
  doDraw: function() {
        this.painting(point);
        clearInterval(this.id);
      },
  painting: function(point) {
       var radgrad = this.ctx.createRadialGradient(point[0], point[1], 0, point[0], point[1], this.weight);
           radgrad.addColorStop(0, "rgb(" + this.color + ")");
           radgrad.addColorStop(0.8, "rgba(" + this.color + ", 0.1)");
           radgrad.addColorStop(1, "rgba(" + this.color + ", 0)");
           this.ctx.fillStyle = radgrad;
           this.ctx.beginPath();
           this.ctx.arc(point[0], point[1], this.weight, 0, Math.PI * 2, true);
           this.ctx.fill();
     }
}
Line = function(ctx) {
  Point.call(this, ctx);
}
Line.prototype = new Point();
Line.prototype.draw = function(points, weight) {
  this.points = points;
  if (typeof(weight) !== "undefined") {
    this.weight = weight;
  }
  this.id = setInterval(this.sid + ".doDraw()", 1);
}
Line.prototype.doDraw = function() {
  var p1 = this.points[0];
  var p2 = this.points[1];
  var forward = p1[0] < p2[0];
  this.painting(p1);
  if (Math.abs(p2[0] - p1[0]) >= Math.abs(p2[1] - p1[1])) {
    if (forward) {
      p1[1] = p1[1] + ((p2[1] - p1[1]) / (p2[0] - p1[0]) * 1);
      p1[0]++;
    } else {
      p1[1] = p1[1] + ((p2[1] - p1[1]) / (p2[0] - p1[0]) * -1);
      p1[0]--;
    }
  } else {
    if (p1[1] < p2[1]) {
      p1[0] = p1[0] + ((p2[0] - p1[0]) / (p2[1] - p1[1]) * 1);
      p1[1]++;
    } else {
      p1[0] = p1[0] + ((p2[0] - p1[0]) / (p2[1] - p1[1]) * -1);
      p1[1]--;
    }
  }
  if (p1[0] > p2[0] && forward) {
    if (this.points.length > 2) {
      this.points.shift();
    } else {
      clearInterval(this.id);
    }
  } else if (p1[0] < p2[0] && !forward) {
    if (this.points.length > 2) {
      this.points.shift();
    } else {
      clearInterval(this.id);
    }
  }
}
Curve = function(ctx) {
  Point.call(this, ctx);
}
Curve.prototype = new Point();
Curve.prototype.draw = function(points, weight, type) {
  this.points = points;
  if (typeof(weight) !== "undefined") {
    this.weight = weight;
  }
  this.begin = this.points[0][0];
  this.end = this.points[points.length - 1][0];
  this.previous = [0, 0];
  if (type === "newton") {
    this.newton = new Newton(points);
  }
  this.id = setInterval(this.sid + ".doDraw()", 1);
}
Curve.prototype.doDraw = function() {
  if (this.begin > this.end) {
    if (this.auchor) {
      this.color = this.auchorColor;
      this.weight = this.weight + 2;
      for (var i = 0; i < this.points.length; i++) {
        this.painting(this.points[i]);
      }
    }
    clearInterval(this.id);
  } else {
    if (typeof(this.newton) === "undefined") {
      var y = Lagrange(this.begin, this.points);
    } else {
      var y = this.newton.get(this.begin);
    }
    this.painting([this.begin, y]);
    var prev = [this.begin, y];
    this.begin = this.begin + Math.min(Math.abs(this.begin.subtract(this.previous[0]).divide(y.subtract(this.previous[1]))), 1);
    this.previous = prev;
  }
}

Common.js
function accAdd(arg1,arg2){
  var r1, r2, m;
  try {
    r1 = arg1.toString().split("e")[0].split(".")[1].length;
  } catch(e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split("e")[0].split(".")[1].length;
  } catch(e) {
    r2 = 0;
  }
  m=Math.pow(10, Math.max(r1, r2));
  return (arg1 * m + arg2 * m) / m;
}
Number.prototype.add = function(arg) {
    return accAdd(arg, this);
}
function accSub(arg1, arg2) {
  var r1, r2, m;
  try {
    r1 = arg1.toString().split("e")[0].split(".")[1].length;
  } catch(e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split("e")[0].split(".")[1].length;
  } catch(e) {
    r2 = 0;
  }
  m=Math.pow(10, Math.max(r1, r2));
  return (arg1 * m - arg2 * m) / m;
}
Number.prototype.subtract = function(arg) {
  return accSub(this, arg);
}
function accMul(arg1,arg2)
{
    var m=0, s1=arg1.toString(), s2=arg2.toString();
    try {
      m += s1.split("e")[0].split(".")[1].length;
    } catch(e) {}
    try {
      m += s2.split("e")[0].split(".")[1].length;
    } catch(e) {}
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}
Number.prototype.multipy = function(arg) {
  return accMul(arg, this);
}
function accDiv(arg1,arg2){
    var t1=0, t2=0, r1, r2;
    if (r2 === 0) {
      r2 = 1;
    }
    try {
      t1 = arg1.toString().split("e")[0].split(".")[1].length;
    } catch(e) {}
    try {
      t2 = arg2.toString().split("e")[0].split(".")[1].length;
    } catch(e) {}
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
    }
}
Number.prototype.divide = function(arg) {
  return accDiv(this, arg);
}
Lagrange = function(x, data) {
  var y = 0;
  for (var i = 0; i < data.length; i++) {
    var l = 1;
    for (var j = 0; j < data.length; j++) {
      if (i === j) {
    continue;
      }
      l = l.multipy(x.subtract(data[j][0]).divide(data[i][0].subtract(data[j][0])));
    }
    y = y.add(l.multipy(data[i][1]));
  }
  return y;
}
Newton = function(data) {
  var index = 0;
  var unit = [[]];
  for (var i = 0; i < data.length; i++) {
    unit[index][i] = [data[i][0], data[i][0], data[i][1]];
  }
  while (unit[index].length > 1) {
    unit[index + 1] = [];
    for (var i = 0; i < unit[index].length - 1; i++) {
      var prev = unit[index][i];
      var next = unit[index][i + 1];
      unit[index + 1][i] = [prev[0], next[1], next[2].subtract(prev[2]).divide(next[1].subtract(prev[0]))];
    }
    index++;
  }
  this.baseData = unit;
}
Newton.prototype.get = function(x) {
  var y = 0;
  for (var i = 0; i < this.baseData.length; i++) {
    var n = this.baseData[i][0][2];
    for (var j = 0; j < i; j++) {
      n = n.multipy(x.subtract(this.baseData[0][j][0]));
    }
    y = y.add(n);
  }
  return y;
}

좋은 웹페이지 즐겨찾기