0 Replies Latest reply on Apr 13, 2014 5:48 AM by TimHill83

    Improving Precision when scaling to exact pixel size

    TimHill83 Level 1

      Hi All,

       

      I'm very new to javascript and illustrator scripting, so I'm pretty sure my problem might be easily fixed

       

      I'm trying to write a script for creating PNGs with a specified pixel size.  I have created the following functions, which first works out the scaling % to use based on the artboard size (borrowed the idea from this post -thanks MuppetMark), and then export the image to PNG.

       

      It works, but, I am finding that when scaling to larger sizes, the resultant output is often a few pixels different from the value I have specified.  I'm sure this is due to precision/rounding issues in javascript, but have no idea how to fix.

       

      I'd be really grateful of any suggestions

       

      Many thanks

       

      Tim

       

      function getScaleFactor(artboard, width, height)
      {
      var scaleFactor;
      if (width > 0)
          {   
              var scaleFactor = ( width / ( artboard.artboardRect[2] - artboard.artboardRect[0] ) ) * 100;
          }
      else if (height > 0)
          {
              scaleFactor = ( height / ( artboard.artboardRect[1] - artboard.artboardRect[3] ) ) * 100; 
          }
      else
          {
          scaleFactor = 100.0;
          }
      return scaleFactor;
      }
      
      function exportFileToPNG24 (dest, scaleFactor) {
          if ( app.documents.length > 0 ) {
              var exportOptions = new ExportOptionsPNG24();
              var type = ExportType.PNG24;
              var fileSpec = new File(dest);
              exportOptions.antiAliasing = true;
              exportOptions.transparency = true;
              exportOptions.saveAsHTML = false;
              exportOptions.artBoardClipping = true;
              exportOptions.horizontalScale = scaleFactor;
              exportOptions.verticalScale = scaleFactor;
              app.activeDocument.exportFile( fileSpec, type, exportOptions );
          }
      }