Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Drawing a line with Javascript

Engaged ,
Oct 03, 2021 Oct 03, 2021

Hello,

I am looking for the command in Javascript for Illustrator to draw a simple line.
For a rectangle I use
    var itemRef1 = docRef.pathItems.rectangle(vTop, vLeft, vWidth, vLength);
A corresponding command
    var itemRef1 = docRef.pathItems.line(vX1, vY1, vX2, vX2);
does not seem to exist.

 

And how can you draw arcs and spirals with JavaScript?

 

Thanks for the answer in advance,
- jens.

 

TOPICS
Draw and design , Scripting
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Oct 04, 2021 Oct 04, 2021

You probably know this already but here it is.  There is no built-in function to draw a line (or an arc or spiral), at least not in the syntax of rectangle(), although of course you can define your own. 

 

To draw a line
(1) you add a pathItem to the pathItems collection;
(2) you add a first pathPoint to the pathPoints collection in the above pathItem;
(3) you add an anchor and two (right and left) handles to the above pathPoint;
(4) you add a second pathPoint to the pathPoints collection in the above

...
Translate
Adobe
Guide ,
Oct 04, 2021 Oct 04, 2021

You probably know this already but here it is.  There is no built-in function to draw a line (or an arc or spiral), at least not in the syntax of rectangle(), although of course you can define your own. 

 

To draw a line
(1) you add a pathItem to the pathItems collection;
(2) you add a first pathPoint to the pathPoints collection in the above pathItem;
(3) you add an anchor and two (right and left) handles to the above pathPoint;
(4) you add a second pathPoint to the pathPoints collection in the above pathItem;
(5) you add an anchor and two (right and left) handles to the above pathPoint.

 

When the anchor and two handles are the same for each point, the result is a straight line. This should draw a line from (0, 0) to the centre of the artboard:

 

var w = app.activeDocument.width, h = - app.activeDocument.height;
var path1 = app.activeDocument.pathItems.add();
var p1 = path1.pathPoints.add();
p1.anchor = p1.rightDirection = p1.leftDirection = [0, 0];
var p2 = path1.pathPoints.add();
p2.anchor = p2.rightDirection = p2.leftDirection = [w / 2, h / 2];

 

When the handles move, the result is a curve. You manipulate the handles the same way you do manually in Illustrator.  If you only want straight lines, a slightly faster way is to use setEntirePath(). This takes an array of the two anchors.

 

var w = app.activeDocument.width, h = - app.activeDocument.height;
var path1 = app.activeDocument.pathItems.add();
path1.setEntirePath( [ [0, 0], [w / 2, h / 2] ] );

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 05, 2021 Oct 05, 2021

Thanks for this clear infomation. I'm working on a Javascript to create squares paper. I will publish it soon.

– jens.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 05, 2021 Oct 05, 2021
LATEST

In the meantime, my Javascript to create check paper is ready:

https://www.behance.net/gallery/128714587/Javascript-SquaresPaperjsx-for-Adobe-Illustrator

 

Have fun with it.

– jens.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines