canvas test 2

by Konstantin Cryman

HTML

<script src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script>
<canvas id="cv" width="100" height="100"></canvas>

JavaScript

const testFunc = ( knownDiameter ) => {
    const squreSide = knownDiameter / Math.SQRT2;
    const minimal_available_diameter = ( knownDiameter * 2 / Math.PI );
    const crossDiametersFactor = ( squreSide / minimal_available_diameter );
    const diameter = minimal_available_diameter * crossDiametersFactor;
    console.log( {
        squreSide,
        crossDiametersFactor,
        minimal_available_diameter,
        diameter,
    } );
    return diameter;
};


/*
Vector2 class
*/
class Vector2 {
  constructor(x = 0, y = 0) {
    this.set(x, y);
  }

  clone() {
    return new Vector2( this.x, this.y );
  }

  set(x, y) {
    this.x = x;
    this.y = y;
    return this;
  }

  copy(v2) {
    this.set(v2.x, v2.y);
    return this;
  }

  add(v2) {
    this.x += v2.x;
    this.y += v2.y;
    return this;
  }

  sub(v2) {
    this.x -= v2.x;
    this.y -= v2.y;
    return this;
  }

  divide(v2) {
    this.x /= v2.x;
    this.y /= v2.y;
    return this;
  }

  multiply(v2) {
    this.x *= v2.x;
    this.y *= v2.y;
    return this;
  }

  multiplyScalar(a) {
    this.x *= a;
    this.y *= a;
    return this;
  }

  negate(v2) {
    this.x = !this.x;
    this.y = !this.y;
    return this;
  }

  abs() {
    this.set(Math.abs(this.x), Math.abs(this.y));
    return this;
  }

  lerp(v2, alpha) {
    const vectorFrom = this.clone();
    const vectorTo = v2.clone();
    const distanceByAlpha = vectorTo.sub(vectorFrom);
    distanceByAlpha.multiply({
      x: alpha,
      y: alpha
    });
    return vectorFrom.add(distanceByAlpha);
  }

  distanceTo(v2) {
    const a = v2.x - this.x;
    const b = v2.y - this.y;
    const c = Math.sqrt(a * a + b * b);
    return Math.abs(c);
  }

  angleTo(v2) {
    return Math.atan2(v2.y - this.y, v2.x - this.x);
  }

  pointOnCircle(_angle, _radius) {
    return {
      x: _radius * Math.cos(_angle) + this.x,
      y: _radius * Math.sin(_angle) + this.y
    };
  }

  toArray() {
    return [this.x, this.y];
  }

  fromArray(arr) {
   ...