Advertisement
pad00000

illustrator script

May 3rd, 2023
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 8.39 KB | Software | 0 0
  1. #target illustrator
  2.  
  3. //UI start
  4. function showDialog() {
  5.   var dialog = new Window('dialog', 'Settings');
  6.   dialog.alignChildren = 'left';
  7.  
  8.   var gradienceGroup = dialog.add('group');
  9.   gradienceGroup.add('statictext', undefined, 'Gradience:');
  10.   var gradienceInput = gradienceGroup.add('edittext', undefined, '50%');
  11.   gradienceInput.characters = 5;
  12.  
  13.   var blendwidthGroup = dialog.add('group');
  14.   blendwidthGroup.add('statictext', undefined, 'Blend Width:');
  15.   var blendwidthInput = blendwidthGroup.add('edittext', undefined, '50%');
  16.   blendwidthInput.characters = 5;
  17.  
  18.   var strokeWidthGroup = dialog.add('group');
  19.   strokeWidthGroup.add('statictext', undefined, 'Stroke Width:');
  20.   var strokeWidthInput = strokeWidthGroup.add('edittext', undefined, '1');
  21.   strokeWidthInput.characters = 5;
  22.  
  23.   var buttonGroup = dialog.add('group');
  24.   buttonGroup.alignment = 'center';
  25.   buttonGroup.add('button', undefined, 'OK', { name: 'ok' });
  26.   buttonGroup.add('button', undefined, 'Cancel', { name: 'cancel' });
  27.  
  28.   if (dialog.show() === 1) {
  29.     return {
  30.       gradience: parseFloat(gradienceInput.text) / 100,
  31.       blendwidth: parseFloat(blendwidthInput.text) / 100,
  32.       strokeWidth: parseFloat(strokeWidthInput.text),
  33.     };
  34.   }
  35.  
  36.   return null;
  37. }
  38.  
  39. //UI end
  40. function main() {
  41.  
  42.     var settings = showDialog();
  43. if (!settings) {
  44.   return;
  45.   }
  46.  
  47.   var selected = app.activeDocument.selection[0];
  48.   drawVisibleLines(selected, settings);
  49. }
  50.  
  51. function drawVisibleLines(target, settings) {
  52. var gradience = settings.gradience;
  53. var blendwidth = settings.blendwidth;
  54. var strokeWidth = settings.strokeWidth;
  55.  
  56.   var doc = app.activeDocument;
  57.   var originalSelection = doc.selection;
  58.   var selection = [];
  59.   for (var i = 0; i < originalSelection.length; i++) {
  60.     selection.push(originalSelection[i]);
  61.   }
  62.  
  63.   function createColor(red, green, blue) {
  64.     var doc = app.activeDocument;
  65.     var color = new RGBColor();
  66.     color.red = red;
  67.     color.green = green;
  68.     color.blue = blue;
  69.  
  70.     if (doc.documentColorSpace == DocumentColorSpace.CMYK) {
  71.       return color.convertTo(DocumentColorSpace.CMYK);
  72.     } else {
  73.       return color;
  74.     }
  75.   }
  76.  
  77.   for (var i = 0; i < selection.length; i++) {
  78.     var selectedObj = selection[i];
  79.  
  80.     if (selectedObj.fillColor.typename == "GradientColor") {
  81.       var generatedLines = [];
  82.    
  83.       // Get the gradient angle
  84.       var gradientAngle = selectedObj.fillColor.angle;
  85.  
  86.       // Get the bounding box dimensions and coordinates
  87.       var bounds = selectedObj.geometricBounds;
  88.       var centerX = (bounds[0] + bounds[2]) / 2;
  89.       var centerY = (bounds[1] + bounds[3]) / 2;
  90.  
  91.       // Calculate the new bounding box dimensions based on the gradient angle
  92.     var angleInRadians = -gradientAngle * Math.PI / 180; // Added a negative sign here
  93.       var width = bounds[2] - bounds[0];
  94.       var height = bounds[1] - bounds[3];
  95.       var newWidth = Math.abs(width * Math.cos(angleInRadians)) + Math.abs(height * Math.sin(angleInRadians));
  96.       var newHeight = Math.abs(width * Math.sin(angleInRadians)) + Math.abs(height * Math.cos(angleInRadians));
  97.  
  98.       // Create a new rectangle for the bounding box
  99.       var newBoundingBoxPath = doc.pathItems.rectangle(0, 0, newWidth, newHeight);
  100.       newBoundingBoxPath.strokeColor =  createColor(255, 0, 0);
  101.       newBoundingBoxPath.fillColor = new NoColor();
  102.  
  103.       // Position the new bounding box at the center of the original bounding box
  104.       newBoundingBoxPath.position = [centerX - newWidth / 2, centerY + newHeight / 2];
  105.  
  106.   // Rotate the new bounding box by the gradient angle to encompass the original bounding box
  107.   newBoundingBoxPath.rotate(-gradientAngle, true, true, true, true, Transformation.CENTER);
  108.  
  109.  
  110.       // Get the parallel edge of the new bounding box
  111.       var gradBoxEdge = newBoundingBoxPath.pathPoints[0].anchor;
  112.  
  113.       // Calculate the middle point of the parallel edge and its opposite edge
  114.       var middleOfParallelEdge = [(gradBoxEdge[0] + newBoundingBoxPath.pathPoints[1].anchor[0]) / 2, (gradBoxEdge[1] + newBoundingBoxPath.pathPoints[1].anchor[1]) / 2];
  115.       var middleOfOppositeEdge = [(newBoundingBoxPath.pathPoints[2].anchor[0] + newBoundingBoxPath.pathPoints[3].anchor[0]) / 2, (newBoundingBoxPath.pathPoints[2].anchor[1] + newBoundingBoxPath.pathPoints[3].anchor[1]) / 2];
  116.  
  117.       // Calculate the vector between the middle points
  118.       var vector = [middleOfOppositeEdge[0] - middleOfParallelEdge[0], middleOfOppositeEdge[1] - middleOfParallelEdge[1]];
  119.      
  120.  
  121.       // Double the length of the vector
  122.       var doubledVector = [vector[0] * 2, vector[1] * 2];
  123.  
  124.     // Calculate the line length based on the correct edges of the new bounding box
  125.     var lineLength = newBoundingBoxPath.width;
  126.  
  127.       // Calculate the new endpoint by adding the doubled vector to the midpoint of the parallel edge
  128.       var newEndpoint = [middleOfParallelEdge[0] + doubledVector[0], middleOfParallelEdge[1] + doubledVector[1]];
  129.  
  130.       // Create a visible line between the middle points of the parallel edges, with a new endpoint that is double the distance from the original endpoint
  131.       var visibleLine = doc.pathItems.add();
  132.       visibleLine.fillColor = new NoColor();
  133.       visibleLine.strokeColor = createColor(255, 0, 0);
  134.       visibleLine.setEntirePath([middleOfOppositeEdge, middleOfParallelEdge, newEndpoint]);
  135.       visibleLine.strokeWidth = strokeWidth;      
  136.      
  137.       // ADDED: Linear interpolation algorithm
  138.       var blendwidth = settings.blendwidth; // Define your blendwidth value between 0 and 1
  139.      var gradience = settings.gradience; // Define your gradience value between 0 and 1
  140.      
  141.       var lineLength = Math.sqrt(Math.pow(newEndpoint[0] - middleOfOppositeEdge[0], 2) + Math.pow(newEndpoint[1] - middleOfOppositeEdge[1], 2));
  142.      
  143.       var strokeWidth = settings.strokeWidth;
  144.       var roundedLength = Math.ceil(lineLength / strokeWidth) * strokeWidth;
  145.       var divisions = roundedLength / strokeWidth;
  146.      
  147.       var startPoint = middleOfOppositeEdge;
  148.       var endPoint = middleOfParallelEdge;
  149.       var totalDivisions = divisions - 1;
  150.       var blendStart = Math.floor(totalDivisions * (1 - blendwidth) / 2);
  151.       var blendEnd = Math.ceil(totalDivisions * (1 + blendwidth) / 2);
  152.      
  153.       for (var div = 0; div <= totalDivisions; div++) {
  154.         var t = div / totalDivisions;
  155.         var blendT = (t - (1 - blendwidth) / 2) / blendwidth;
  156.         var gradientFactor = 1 - Math.min(Math.max(blendT, 0), 1) * gradience;
  157.        
  158.         if (Math.random() < gradientFactor) {
  159.           var x = startPoint[0] + t * (endPoint[0] - startPoint[0]);
  160.           var y = startPoint[1] + t * (endPoint[1] - startPoint[1]);
  161.           var perpendicularVector = [-vector[1], vector[0]];
  162.           var lineStart = [x + perpendicularVector[0] / 2, y + perpendicularVector[1] / 2];
  163.           var lineEnd = [x - perpendicularVector[0] / 2, y - perpendicularVector[1] / 2];
  164.          
  165.           var line = doc.pathItems.add();
  166.           line.setEntirePath([lineStart, lineEnd]);
  167.           line.strokeWidth = strokeWidth;
  168.           line.strokeColor = createColor(0, 0, 0);
  169.           generatedLines.push(line);
  170.         }
  171.       }
  172.       // END: Linear interpolation algorithm
  173.  
  174.       hideLinesOutsideObjects(generatedLines, selectedObj);
  175.  
  176.       doc.selection = null;
  177.     }
  178.   }
  179. }
  180.  
  181. function hideLinesOutsideObjects(lines, obj) {
  182.   var doc = app.activeDocument;
  183.  
  184.   if (!obj || lines.length === 0) {
  185.     return;
  186.   }
  187.  
  188.   var clippingGroup = doc.groupItems.add();
  189.   for (var i = 0; i < lines.length; i++) {
  190.     lines[i].move(clippingGroup, ElementPlacement.PLACEATBEGINNING);
  191.   }
  192.  
  193.   var clippingMask = obj.duplicate(clippingGroup, ElementPlacement.PLACEATBEGINNING);
  194.   clippingMask.clipping = true;
  195.   clippingGroup.clipped = true;
  196.  
  197.   // Expanding the Clipping Mask
  198.   clippingGroup.selected = true;
  199.   app.executeMenuCommand('expandStyle');
  200.   app.redraw();
  201.  
  202.   // Ungrouping the items
  203.   try {
  204.     clippingGroup.noShrinkwrap = true;
  205.     clippingGroup.ungroup();
  206.   } catch (e) {
  207.     // In case ungroup is not available, ignore the error
  208.   }
  209. }
  210.  
  211.  
  212.   function createColor(r, g, b) {
  213.     var color = new RGBColor();
  214.     color.red = r;
  215.     color.green = g;
  216.     color.blue = b;
  217.     return color;
  218.   }
  219.  
  220. // Entry point
  221. if (app.documents.length > 0 && app.activeDocument.selection.length > 0) {
  222.   main();
  223. } else {
  224.   alert('Please select an object to draw visible lines on.');
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement