dojox/drawing/util/common.js

  • Provides:

    • dojox.drawing.util.common
  • Requires:

    • dojox.math.round in common
  • dojox.drawing.util.common

    • type
      Object
    • summary
      A collection of common methods used for DojoX Drawing.
      This singleton is accessible in most Drawing classes
      as this.util
      
      NOTE:
      A lot of functions use a EventObject
      as an argument. An attempt was made to accept
      either that object or a list of numbers. That wasn't
      finished (it didn't work well in all cases) but is
      likely to happen in the future.
      In cases where you are not sending a Mouse object,
      form your argument like so:
      var obj = {
      start:{
      x:Number,  	// start x
      y:Number	// start y
      },
      x: Number,		// end x
      y:Number		// end y
      }
  • dojox.drawing.util.common.radToDeg

    • type
      Function
    • parameters:
      • n: (typeof Numer)
    • source: [view]
         return (n*180)/Math.PI; // Number
    • summary
      Convert the passed number to degrees.
    • returns
      Number
  • dojox.drawing.util.common.degToRad

    • type
      Function
    • parameters:
      • n: (typeof Numer)
    • source: [view]
         return (n*Math.PI)/180; // Number
    • summary
      Convert the passed number to radians.
    • returns
      Number
  • dojox.drawing.util.common.angle

    • type
      Function
    • parameters:
      • obj: (typeof EventObject)
        Manager.Mouse event.
      • snap: (typeof ? Float)
        Float
        Returns nearest angle within snap limits
        
        obj = this.argsToObj.apply(this, arguments);
    • source: [view]
         if(snap){
          snap = snap/180;
          var radians = this.radians(obj),
           seg = Math.PI * snap,
           rnd = dojox.math.round(radians/seg),
           new_radian = rnd*seg;
          return dojox.math.round(this.radToDeg(new_radian)); // Whole Number

         
         }else{
          return this.radToDeg(this.radians(obj)); // Float
         }
    • summary
      Return angle based on mouse object
      arguments:
    • returns
      Whole Number|Float
  • dojox.drawing.util.common.oppAngle

    • type
      Function
    • parameters:
      • ang: (typeof Angle)
    • source: [view]
         (ang+=180) > 360 ? ang = ang - 360 : ang;
         return ang;
    • summary
  • dojox.drawing.util.common.radians

    • type
      Function
    • parameters:
      • o: (typeof EventObject)
    • source: [view]
         return Math.atan2(o.start.y-o.y,o.x-o.start.x);
    • summary
      Return the radians derived from the coordinates
      in the Mouse object.
      
      var o = this.argsToObj.apply(this, arguments);
  • dojox.drawing.util.common.length

    • type
      Function
    • parameters:
      • o: (typeof EventObject)
    • source: [view]
         return Math.sqrt(Math.pow(o.start.x-o.x, 2)+Math.pow(o.start.y-o.y, 2));
    • summary
      Return the length derived from the coordinates
      in the Mouse object.
  • dojox.drawing.util.common.lineSub

    • type
      Function
    • parameters:
      • x1: (typeof Number)
      • y1: (typeof Number)
      • x2: (typeof Number)
      • y2: (typeof Number)
      • amt: (typeof Number)
    • source: [view]
         var len = this.distance(this.argsToObj.apply(this, arguments));
         len = len < amt ? amt : len;
         var pc = (len-amt)/len;
         var x = x1 - (x1-x2) * pc;
         var y = y1 - (y1-y2) * pc;
         return {x:x, y:y}; // Object
    • summary
      Subtract an amount from a line
    • description
      x1,y1,x2,y2 represents the Line. 'amt' represents the amount
      to subtract from it.
    • returns
      Object
    • chains:
      • this.argsToObj: (call)
      • this.argsToObj: (call)
  • dojox.drawing.util.common.argsToObj

    • type
      Function
    • source: [view]
         var a = arguments;
         if(a.length < 4){ return a[0]; }
         return {
          start:{
           x:a[0],
           y:a[1]
          },
          x:a[2],
          y:a[3]//,
          //snap:a[4]
         }; // Object
    • summary
      Attempts to determine in a Mouse Object
      was passed or indiviual numbers. Returns
      an object.
  • dojox.drawing.util.common.distance

    • type
      Function
    • source: [view]
         var o = this.argsToObj.apply(this, arguments);
         return Math.abs(Math.sqrt(Math.pow(o.start.x-o.x, 2)+Math.pow(o.start.y-o.y, 2))); // Number
    • summary
      Return the length derived from the coordinates
      in the Mouse object. Different from util.length
      in that this always returns an absolute value.
    • returns
      Number
    • chains:
      • this.argsToObj: (call)
      • this.argsToObj: (call)
  • dojox.drawing.util.common.slope

    • summary
  • dojox.drawing.util.common.pointOnCircle

    • type
      Function
    • parameters:
      • cx: (typeof Number)
      • cy: (typeof Number)
      • radius: (typeof Number)
      • angle: (typeof Number)
    • source: [view]
         var radians = angle * Math.PI / 180.0;
         var x = radius * Math.cos(radians);
         var y = radius * Math.sin(radians);
         return {
          x:cx+x,
          y:cy-y
         }; // Object
    • summary
      A *very* helpful method. If you know the center
      (or starting) point, length and angle, find the
      x,y point at the end of that line.
  • dojox.drawing.util.common.constrainAngle

    • type
      Function
    • parameters:
      • obj: (typeof EventObject)
      • min: (typeof Number)
      • max: (typeof Number)
    • source: [view]
         var angle = this.angle(obj);
         if(angle >= min && angle <= max){
          return obj;  // Object
         }
         var radius = this.length(obj);
         var new_angle = angle > max ? max : min - angle < 100 ? min : max;
         return this.pointOnCircle(obj.start.x,obj.start.y,radius, new_angle); // Object
    • summary
      Ensures the angle in the Mouse Object is within the
      min and max limits. If not one of those limits is used.
      Returns an x,y point for the angle used.
    • returns
      Object
  • dojox.drawing.util.common.snapAngle

    • type
      Function
    • parameters:
      • obj: (typeof EventObject)
        Mouse object (see dojox.drawing.Mouse)
      • ca: (typeof Float)
        Fractional amount to snap to
        A decimal number fraction of a half circle
        .5 would snap to 90 degrees
        .25  would snap to 45 degrees
        .125 would snap to 22.5 degrees, etc.
    • source: [view]
         var radians = this.radians(obj),
          radius = this.length(obj),
          seg = Math.PI * ca,
          rnd = Math.round(radians/seg),
          new_radian = rnd*seg,
          new_angle = this.radToDeg(new_radian),
          pt = this.pointOnCircle(obj.start.x,obj.start.y,radius,new_angle);
         return pt; // Object
    • summary
      Snaps a line to the nearest angle
    • returns
      Object
  • dojox.drawing.util.common.idSetStart

    • type
      Function
    • parameters:
      • num: (typeof )
    • source: [view]
         start=num;
    • summary
  • dojox.drawing.util.common.uid

    • type
      Function
    • parameters:
      • str: (typeof ? String)
        String
        If provided, kept in a map, incremented
        and used in the id. Otherwise 'shape' is used.
    • source: [view]
         str = str || "shape";
         uidMap[str] = uidMap[str]===undefined ? start : uidMap[str] + 1;
         return str + uidMap[str]; // String
    • summary
      Creates a unique ID.
      arguments:
    • returns
      String
  • dojox.drawing.util.common.abbr

    • type
      Function
    • parameters:
      • type: (typeof )
    • source: [view]
         return type.substring(type.lastIndexOf(".")+1).charAt(0).toLowerCase()
          + type.substring(type.lastIndexOf(".")+2);
    • summary
      Converts a namespace (typically a tool or a stencil) into
      an abbreviation
  • dojox.drawing.util.common.mixin

    • type
      Function
    • parameters:
      • o1: (typeof )
      • o2: (typeof )
    • source: [view]
      dojo.provide("dojox.drawing.util.common");
      dojo.require("dojox.math.round");


      (function(){

       
       var uidMap = {};
       var start = 0;
       dojox.drawing.util.common = {
        // summary:
        //  A collection of common methods used for DojoX Drawing.
        //  This singleton is accessible in most Drawing classes
        //  as this.util
        //
        // NOTE:
        //  A lot of functions use a EventObject
        //  as an argument. An attempt was made to accept
        //  either that object or a list of numbers. That wasn't
        //  finished (it didn't work well in all cases) but is
        //  likely to happen in the future.
        //  In cases where you are not sending a Mouse object,
        //  form your argument like so:
        //  var obj = {
        //   start:{
        //     x:Number,  // start x
        //     y:Number // start y
        //    },
        //    x: Number,  // end x
        //    y:Number  // end y
        //   }
        //
        //
        radToDeg: function(/*Numer*/n){
         // summary:
         //  Convert the passed number to degrees.
         return (n*180)/Math.PI; // Number
        },

        
        degToRad: function(/*Numer*/n){
         // summary:
         //  Convert the passed number to radians.
         return (n*Math.PI)/180; // Number
        },

        
        angle: function(/*EventObject*/obj, /* ? Float */snap){
         // summary:
         //  Return angle based on mouse object
         // arguments:
         //  obj: EventObject
         //   Manager.Mouse event.
         //   snap: Float
         //   Returns nearest angle within snap limits
         //
         //obj = this.argsToObj.apply(this, arguments);
         if(snap){
          snap = snap/180;
          var radians = this.radians(obj),
           seg = Math.PI * snap,
           rnd = dojox.math.round(radians/seg),
           new_radian = rnd*seg;
          return dojox.math.round(this.radToDeg(new_radian)); // Whole Number

         
         }else{
          return this.radToDeg(this.radians(obj)); // Float
         }
        },

        
        oppAngle: function(/*Angle*/ang){
         (ang+=180) > 360 ? ang = ang - 360 : ang;
         return ang;
        },

        
        radians: function(/*EventObject*/o){
         // summary:
         //  Return the radians derived from the coordinates
         //  in the Mouse object.
         //
         //var o = this.argsToObj.apply(this, arguments);
         return Math.atan2(o.start.y-o.y,o.x-o.start.x);
        },

        
        length: function(/*EventObject*/o){
         // summary:
         //  Return the length derived from the coordinates
         //  in the Mouse object.
         //
         return Math.sqrt(Math.pow(o.start.x-o.x, 2)+Math.pow(o.start.y-o.y, 2));
        },

        
        lineSub: function(/*Number*/x1, /*Number*/y1, /*Number*/x2, /*Number*/y2, /*Number*/amt){
         // summary:
         //  Subtract an amount from a line
         // description:
         //  x1,y1,x2,y2 represents the Line. 'amt' represents the amount
         //  to subtract from it.
         //
         var len = this.distance(this.argsToObj.apply(this, arguments));
         len = len < amt ? amt : len;
         var pc = (len-amt)/len;
         var x = x1 - (x1-x2) * pc;
         var y = y1 - (y1-y2) * pc;
         return {x:x, y:y}; // Object
        },

        
        argsToObj: function(){
         // summary:
         //  Attempts to determine in a Mouse Object
         //  was passed or indiviual numbers. Returns
         //  an object.
         //
         var a = arguments;
         if(a.length < 4){ return a[0]; }
         return {
          start:{
           x:a[0],
           y:a[1]
          },
          x:a[2],
          y:a[3]//,
          //snap:a[4]
         }; // Object
        },

        
        distance: function(/*EventObject or x1,y1,x2,y2*/){
         // summary:
         //  Return the length derived from the coordinates
         //  in the Mouse object. Different from util.length
         //  in that this always returns an absolute value.
         //
         var o = this.argsToObj.apply(this, arguments);
         return Math.abs(Math.sqrt(Math.pow(o.start.x-o.x, 2)+Math.pow(o.start.y-o.y, 2))); // Number
        },

        
        slope:function(/*Object*/p1, /*Object*/p2){
         // summary:
         //  Given two poits of a line, returns the slope.
         if(!(p1.x-p2.x)){ return 0; }
         return ((p1.y-p2.y)/(p1.x-p2.x)); // Number
        },

        
        pointOnCircle: function(/*Number*/cx, /*Number*/cy, /*Number*/radius, /*Number*/angle){
         // summary:
         //  A *very* helpful method. If you know the center
         //  (or starting) point, length and angle, find the
         //  x,y point at the end of that line.
         //
         var radians = angle * Math.PI / 180.0;
         var x = radius * Math.cos(radians);
         var y = radius * Math.sin(radians);
         return {
          x:cx+x,
          y:cy-y
         }; // Object
        },

        
        constrainAngle: function(/*EventObject*/obj, /*Number*/min, /*Number*/max){
         // summary:
         //  Ensures the angle in the Mouse Object is within the
         //  min and max limits. If not one of those limits is used.
         //  Returns an x,y point for the angle used.
         //
         var angle = this.angle(obj);
         if(angle >= min && angle <= max){
          return obj;  // Object
         }
         var radius = this.length(obj);
         var new_angle = angle > max ? max : min - angle < 100 ? min : max;
         return this.pointOnCircle(obj.start.x,obj.start.y,radius, new_angle); // Object
        },

        
        snapAngle: function(/*EventObject*/obj, /*Float*/ca){
         // summary:
         //  Snaps a line to the nearest angle
         //   obj: Mouse object (see dojox.drawing.Mouse)
         //   ca: Fractional amount to snap to
         //    A decimal number fraction of a half circle
         //    .5 would snap to 90 degrees
         //    .25 would snap to 45 degrees
         //    .125 would snap to 22.5 degrees, etc.
         //
         var radians = this.radians(obj),
          radius = this.length(obj),
          seg = Math.PI * ca,
          rnd = Math.round(radians/seg),
          new_radian = rnd*seg,
          new_angle = this.radToDeg(new_radian),
          pt = this.pointOnCircle(obj.start.x,obj.start.y,radius,new_angle);
         return pt; // Object
        },

        
        // helpers
        idSetStart: function(num){
         start=num;
        },

        
        uid: function(/* ? String */str){
         // summary:
         //  Creates a unique ID.
         // arguments:
         //  str: String
         //   If provided, kept in a map, incremented
         //   and used in the id. Otherwise 'shape' is used.
         //
         str = str || "shape";
         uidMap[str] = uidMap[str]===undefined ? start : uidMap[str] + 1;
         return str + uidMap[str]; // String
        },

        
        abbr: function(type){
         // summary:
         //  Converts a namespace (typically a tool or a stencil) into
         //  an abbreviation
         return type.substring(type.lastIndexOf(".")+1).charAt(0).toLowerCase()
          + type.substring(type.lastIndexOf(".")+2);
        },
        mixin: function(o1, o2){
         // TODO: make faster
         //return dojo.mixin(dojo.clone(o1), dojo.clone(o2));
    • returns
      Number|Whole Number|Float|Object|String
    • summary
  • dojox.drawing.util.common.objects

    • summary
  • dojox.drawing.util

    • type
      Object
    • summary
  • dojox.drawing

    • type
      Object
    • summary
  • dojox

    • type
      Object
    • summary