Advertisement
henke37

Control point snapping JSFL

Aug 7th, 2013
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var d=fl.getDocumentDOM();
  2. var s=d.selection;
  3.  
  4. if(s) {
  5.     snap(s);
  6. } else {
  7.     fl.trace("No document opened");
  8. }
  9.  
  10. function snap(s) {
  11.    
  12.     if(s.length==0) {
  13.         fl.trace("Nothing selected");
  14.         return;
  15.     }
  16.    
  17.     for(var i=0;i<s.length;i++){
  18.         var e=s[i];
  19.         //fl.trace(e);
  20.        
  21.         if(e.elementType=="shape") {
  22.             snapShape(e);
  23.         } else if(e.elementType=="instance") {
  24.             snapInst(e);
  25.         }
  26.     }
  27. }
  28.  
  29. function snapInst(inst) {
  30.     inst.x=Math.round(inst.x);
  31.     inst.y=Math.round(inst.y);
  32.     inst.width=Math.round(inst.width);
  33.     inst.height=Math.round(inst.height);
  34. }
  35.  
  36. function snapShape(shape) {
  37.    
  38.     //fl.trace("drawing object: "+shape.isDrawingObject+" group: "+shape.isGroup+" oval: "+shape.isOvalObject+" rect: "+shape.isRectangleObject );
  39.  
  40.     if(shape.isDrawingObject) return;
  41.     //if(shape.isGroup) snapGroup(shape);
  42.    
  43.     shape.beginEdit();
  44.     var vs=shape.vertices;
  45.    
  46.     for(var i=0;i<vs.length;++i) {
  47.         var v=vs[i];
  48.         var newX=Math.round(v.x);
  49.         var newY=Math.round(v.y);
  50.         v.setLocation(newX,newY);
  51.     }
  52.    
  53.     shape.endEdit();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement