Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #target illustrator
- //UI start
- function showDialog() {
- var dialog = new Window('dialog', 'Settings');
- dialog.alignChildren = 'left';
- var gradienceGroup = dialog.add('group');
- gradienceGroup.add('statictext', undefined, 'Gradience:');
- var gradienceInput = gradienceGroup.add('edittext', undefined, '50%');
- gradienceInput.characters = 5;
- var blendwidthGroup = dialog.add('group');
- blendwidthGroup.add('statictext', undefined, 'Blend Width:');
- var blendwidthInput = blendwidthGroup.add('edittext', undefined, '50%');
- blendwidthInput.characters = 5;
- var strokeWidthGroup = dialog.add('group');
- strokeWidthGroup.add('statictext', undefined, 'Stroke Width:');
- var strokeWidthInput = strokeWidthGroup.add('edittext', undefined, '1');
- strokeWidthInput.characters = 5;
- var buttonGroup = dialog.add('group');
- buttonGroup.alignment = 'center';
- buttonGroup.add('button', undefined, 'OK', { name: 'ok' });
- buttonGroup.add('button', undefined, 'Cancel', { name: 'cancel' });
- if (dialog.show() === 1) {
- return {
- gradience: parseFloat(gradienceInput.text) / 100,
- blendwidth: parseFloat(blendwidthInput.text) / 100,
- strokeWidth: parseFloat(strokeWidthInput.text),
- };
- }
- return null;
- }
- //UI end
- function main() {
- var settings = showDialog();
- if (!settings) {
- return;
- }
- var selected = app.activeDocument.selection[0];
- drawVisibleLines(selected, settings);
- }
- function drawVisibleLines(target, settings) {
- var gradience = settings.gradience;
- var blendwidth = settings.blendwidth;
- var strokeWidth = settings.strokeWidth;
- var doc = app.activeDocument;
- var originalSelection = doc.selection;
- var selection = [];
- for (var i = 0; i < originalSelection.length; i++) {
- selection.push(originalSelection[i]);
- }
- function createColor(red, green, blue) {
- var doc = app.activeDocument;
- var color = new RGBColor();
- color.red = red;
- color.green = green;
- color.blue = blue;
- if (doc.documentColorSpace == DocumentColorSpace.CMYK) {
- return color.convertTo(DocumentColorSpace.CMYK);
- } else {
- return color;
- }
- }
- for (var i = 0; i < selection.length; i++) {
- var selectedObj = selection[i];
- if (selectedObj.fillColor.typename == "GradientColor") {
- var generatedLines = [];
- // Get the gradient angle
- var gradientAngle = selectedObj.fillColor.angle;
- // Get the bounding box dimensions and coordinates
- var bounds = selectedObj.geometricBounds;
- var centerX = (bounds[0] + bounds[2]) / 2;
- var centerY = (bounds[1] + bounds[3]) / 2;
- // Calculate the new bounding box dimensions based on the gradient angle
- var angleInRadians = -gradientAngle * Math.PI / 180; // Added a negative sign here
- var width = bounds[2] - bounds[0];
- var height = bounds[1] - bounds[3];
- var newWidth = Math.abs(width * Math.cos(angleInRadians)) + Math.abs(height * Math.sin(angleInRadians));
- var newHeight = Math.abs(width * Math.sin(angleInRadians)) + Math.abs(height * Math.cos(angleInRadians));
- // Create a new rectangle for the bounding box
- var newBoundingBoxPath = doc.pathItems.rectangle(0, 0, newWidth, newHeight);
- newBoundingBoxPath.strokeColor = createColor(255, 0, 0);
- newBoundingBoxPath.fillColor = new NoColor();
- // Position the new bounding box at the center of the original bounding box
- newBoundingBoxPath.position = [centerX - newWidth / 2, centerY + newHeight / 2];
- // Rotate the new bounding box by the gradient angle to encompass the original bounding box
- newBoundingBoxPath.rotate(-gradientAngle, true, true, true, true, Transformation.CENTER);
- // Get the parallel edge of the new bounding box
- var gradBoxEdge = newBoundingBoxPath.pathPoints[0].anchor;
- // Calculate the middle point of the parallel edge and its opposite edge
- var middleOfParallelEdge = [(gradBoxEdge[0] + newBoundingBoxPath.pathPoints[1].anchor[0]) / 2, (gradBoxEdge[1] + newBoundingBoxPath.pathPoints[1].anchor[1]) / 2];
- var middleOfOppositeEdge = [(newBoundingBoxPath.pathPoints[2].anchor[0] + newBoundingBoxPath.pathPoints[3].anchor[0]) / 2, (newBoundingBoxPath.pathPoints[2].anchor[1] + newBoundingBoxPath.pathPoints[3].anchor[1]) / 2];
- // Calculate the vector between the middle points
- var vector = [middleOfOppositeEdge[0] - middleOfParallelEdge[0], middleOfOppositeEdge[1] - middleOfParallelEdge[1]];
- // Double the length of the vector
- var doubledVector = [vector[0] * 2, vector[1] * 2];
- // Calculate the line length based on the correct edges of the new bounding box
- var lineLength = newBoundingBoxPath.width;
- // Calculate the new endpoint by adding the doubled vector to the midpoint of the parallel edge
- var newEndpoint = [middleOfParallelEdge[0] + doubledVector[0], middleOfParallelEdge[1] + doubledVector[1]];
- // 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
- var visibleLine = doc.pathItems.add();
- visibleLine.fillColor = new NoColor();
- visibleLine.strokeColor = createColor(255, 0, 0);
- visibleLine.setEntirePath([middleOfOppositeEdge, middleOfParallelEdge, newEndpoint]);
- visibleLine.strokeWidth = strokeWidth;
- // ADDED: Linear interpolation algorithm
- var blendwidth = settings.blendwidth; // Define your blendwidth value between 0 and 1
- var gradience = settings.gradience; // Define your gradience value between 0 and 1
- var lineLength = Math.sqrt(Math.pow(newEndpoint[0] - middleOfOppositeEdge[0], 2) + Math.pow(newEndpoint[1] - middleOfOppositeEdge[1], 2));
- var strokeWidth = settings.strokeWidth;
- var roundedLength = Math.ceil(lineLength / strokeWidth) * strokeWidth;
- var divisions = roundedLength / strokeWidth;
- var startPoint = middleOfOppositeEdge;
- var endPoint = middleOfParallelEdge;
- var totalDivisions = divisions - 1;
- var blendStart = Math.floor(totalDivisions * (1 - blendwidth) / 2);
- var blendEnd = Math.ceil(totalDivisions * (1 + blendwidth) / 2);
- for (var div = 0; div <= totalDivisions; div++) {
- var t = div / totalDivisions;
- var blendT = (t - (1 - blendwidth) / 2) / blendwidth;
- var gradientFactor = 1 - Math.min(Math.max(blendT, 0), 1) * gradience;
- if (Math.random() < gradientFactor) {
- var x = startPoint[0] + t * (endPoint[0] - startPoint[0]);
- var y = startPoint[1] + t * (endPoint[1] - startPoint[1]);
- var perpendicularVector = [-vector[1], vector[0]];
- var lineStart = [x + perpendicularVector[0] / 2, y + perpendicularVector[1] / 2];
- var lineEnd = [x - perpendicularVector[0] / 2, y - perpendicularVector[1] / 2];
- var line = doc.pathItems.add();
- line.setEntirePath([lineStart, lineEnd]);
- line.strokeWidth = strokeWidth;
- line.strokeColor = createColor(0, 0, 0);
- generatedLines.push(line);
- }
- }
- // END: Linear interpolation algorithm
- hideLinesOutsideObjects(generatedLines, selectedObj);
- doc.selection = null;
- }
- }
- }
- function hideLinesOutsideObjects(lines, obj) {
- var doc = app.activeDocument;
- if (!obj || lines.length === 0) {
- return;
- }
- var clippingGroup = doc.groupItems.add();
- for (var i = 0; i < lines.length; i++) {
- lines[i].move(clippingGroup, ElementPlacement.PLACEATBEGINNING);
- }
- var clippingMask = obj.duplicate(clippingGroup, ElementPlacement.PLACEATBEGINNING);
- clippingMask.clipping = true;
- clippingGroup.clipped = true;
- // Expanding the Clipping Mask
- clippingGroup.selected = true;
- app.executeMenuCommand('expandStyle');
- app.redraw();
- // Ungrouping the items
- try {
- clippingGroup.noShrinkwrap = true;
- clippingGroup.ungroup();
- } catch (e) {
- // In case ungroup is not available, ignore the error
- }
- }
- function createColor(r, g, b) {
- var color = new RGBColor();
- color.red = r;
- color.green = g;
- color.blue = b;
- return color;
- }
- // Entry point
- if (app.documents.length > 0 && app.activeDocument.selection.length > 0) {
- main();
- } else {
- alert('Please select an object to draw visible lines on.');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement