forked from johnwun/js4ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleurify.js
148 lines (130 loc) · 4.4 KB
/
fleurify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/////////////////////////////////////////////////////////////////
//Fleurify -- CS,CS2,CS3
//>=--------------------------------------
// Named after the "Fleur de Lis" as in "flower",
// this script is the bastard love child of a Spirograph and the "Punk & Bloat" filter.
//
// Enter a percentage to "fleurify" and the script turns basic geometry into flowery shapes.
// Warning: the fleurified objects paths will self intersect. This shouldn't matter in print media, but
// for cutable shapes you'll have to use the pathfinder palate to break and merge it.
//
// Note: The anchors are never modified, only the control handles,
// so you can always "un-fleurify" an object using my "sharpen corners" script.
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( [email protected] ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//////////////////////////////////////////////////////////////////
var doc = activeDocument;
var sel = doc.selection;
var sellen = sel.length;
//
var ignoredItemCount = 0;
//
var adjacentHandlesToo=1;
// The 'adjacentHandlesToo' variable is a toggle for zeroing the handles of adjacent points.
// I default this to true because in most cases, I assume this will
// be the expected behavior. If not, set to 0.
//var ui = confirm('Remove shared control handles from adjacent points?');
//if( ui == true){
// adjacentHandlesToo=ui;
//}
var pct=prompt("Fleurify by what percentage?","100");
//
//---------------------------------------main code:
//
for(var x=0;x<sellen;x++){
if(sel[x].typename == 'PathItem'){
// hose control points of selected path items
knotHandles(sel[x]);
}else if(sel[x].typename == 'CompoundPathItem'){
//hose control points of selected compound path items
var cpiLen = sel[x].pathItems.length;
for (var j=0;j<cpiLen;j++){
knotHandles(sel[x].pathItems[j]);
}
}else{
ignoredItemCount ++;
}
}
if(ignoredItemCount>0){
alert('Just so you know:\n'+ignoredItemCount+' Item(s) ignored. \nTry selecting objects individually or ungrouping.');
}
//------------------------------functions below:
function hoseThisAnchor(ptOb,deets){
var anch = ptOb.anchor;
var cntr_arr = Array(anch[0],anch[1]);
if(deets=='l'){
ptOb.leftDirection = cntr_arr;
} else if(deets=='r'){
ptOb.rightDirection = cntr_arr;
} else {
ptOb.leftDirection = cntr_arr;
ptOb.rightDirection = cntr_arr;
}
}
function hoseHandles(ob){
var ppl = ob.pathPoints.length;
for(var y=0;y<ppl;y++){
//This
if(ob.pathPoints[y].selected==PathPointSelection.ANCHORPOINT){
hoseThisAnchor(ob.pathPoints[y]);
}
if(adjacentHandlesToo==1){
if(ob.pathPoints[y].selected==PathPointSelection.LEFTDIRECTION){
hoseThisAnchor(ob.pathPoints[y],'l');
}
if(ob.pathPoints[y].selected==PathPointSelection.RIGHTDIRECTION){
hoseThisAnchor(ob.pathPoints[y],'r');
}
}
}
}
function knotThisAnchor(ptOb,prevOb,nextOb){
//var pct = 90;
var anch = ptOb.anchor;
var anchp = prevOb.anchor;
var anchn = nextOb.anchor;
var cntr_arr = Array(anch[0],anch[1]);
var next_arr = Array(anchp[0],anchp[1]);
var prev_arr = Array(anchn[0],anchn[1]);
var nextpos = calculatePos(cntr_arr,next_arr,pct)
var prevpos = calculatePos(cntr_arr,prev_arr,pct)
ptOb.leftDirection = prevpos;
ptOb.rightDirection = nextpos;
}
function knotHandles(ob){
var ppl = ob.pathPoints.length;
for(var y=0;y<ppl;y++){
if(ob.pathPoints[y].selected==PathPointSelection.ANCHORPOINT){
if(y==0){
knotThisAnchor(ob.pathPoints[y],ob.pathPoints[ppl-1],ob.pathPoints[y+1]);
} else if (y==ppl-1){
knotThisAnchor(ob.pathPoints[y],ob.pathPoints[y-1],ob.pathPoints[0]);
} else{
knotThisAnchor(ob.pathPoints[y],ob.pathPoints[(y-1)],ob.pathPoints[(y+1)]);
}
}
}
}
function calculatePos(pt1,pt2,pct){
var xsq = Math.pow(Math.max(pt1[0],pt2[0]) - Math.min(pt1[0],pt2[0]),2);
var ysq = Math.pow(Math.max(pt1[1],pt2[1]) - Math.min(pt1[1],pt2[1]),2);
var z = Math.sqrt(xsq+ysq);
//
var dx = (Math.max(pt1[0],pt2[0]) - Math.min(pt1[0],pt2[0]));
var dy = (Math.max(pt1[1],pt2[1]) - Math.min(pt1[1],pt2[1]));
//get ratio of width to height...
//var ratio = w/h;
//90+10*.5)
if(pt1[0] < pt2[0]){
var nux = pt1[0]+(dx*(pct/100));
} else {
var nux = pt1[0]-(dx*(pct/100));
}
//120
if(pt1[1]<pt2[1]){
var nuy = pt1[1]+(dy*(pct/100));
}else{
var nuy = pt1[1]-(dy*(pct/100));}
return([nux,nuy]);
}