var canvas = activeDocument.groupItems.add();
var pt = 72 / 25.4;
//把需要添加的图形 放入列表
var shapes = new Array();
shapes.push(new ShapeLine(0, 0, 20, 20, 0.2, MyColor().red));
shapes.push(new ShapeRect(0, 0, 20, 20, 0.2, MyColor().red, MyColor().black));
//画出来
for (var i = 0; i < shapes.length; i++) {
shapes[i].draw();
}
/**
* 图形类 比如线条 矩形 文字 导入的pdf模板 这些都算
* 他们的共性
* 坐标 共同的方法 draw
* @constructor
*/
function Shape() {
this.canvas = canvas;
this.draw = function () {
//每个子类有自己 生产方式
alert('这个儿子没有重写爸爸的方法');
}
}
/**
* 画线段
* @param startX 起点X
* @param startY 起点Y
* @param endX 终点X
* @param endY 终点Y
* @param strokeWidth 线宽
* @param strokeColor 线色
* @constructor
*/
function ShapeLine(startX, startY, endX, endY, strokeWidth, strokeColor) {
Shape.call(this)
this.startX = startX * pt;
this.startY = startY * pt;
this.endX = endX * pt;
this.endY = endY * pt;
this.strokeWidth = strokeWidth * pt;
this.strokeColor = strokeColor;
this.draw = function () {
var line = this.canvas.pathItems.add();
line.setEntirePath([[this.startX, this.startY], [this.endX, this.endY]]);
line.strokeColor = this.strokeColor;
line.fillColor = NoColor;
line.strokeWidth = this.strokeWidth;
return line;
}
}
/**
* 矩形类
* @param startX 起点X 左上角是起点
* @param startY 起点Y 左上角是起点
* @param width 宽度
* @param height 高度
* @param strokeWidth 线宽
* @param fillColor 填充色
* @param strokeColor 线色
* @constructor
*/
function ShapeRect(startX, startY, width, height, strokeWidth, fillColor, strokeColor) {
Shape.call(this)
this.startX = startX * pt;
this.startY = startY * pt;
this.width = width * pt;
this.height = height * pt;
this.strokeWidth = strokeWidth * pt;
this.strokeColor = strokeColor;
this.fillColor = fillColor;
this.draw = function () {
var rect = this.canvas.pathItems.rectangle(this.startX, this.startY, this.width, this.height);
rect.strokeColor = this.strokeColor;
rect.fillColor = this.fillColor;
rect.strokeWidth = this.strokeWidth;
return rect;
}
}
/**
* 工具类用于存放 一些常用的方法
* @constructor
*/
function MyUtils() {
this.createCMYKColor = function (c, m, y, k) {
var color = new CMYKColor();//创建颜色变量
color.cyan = c;//设置颜色的值
color.magenta = m;//设置颜色的值
color.yellow = y;//设置颜色的值
color.black = k;//设置颜色的值
return color;
}
}
/**
* 颜色类 存储一些常用的颜色
* @returns {MyColor}
* @constructor
*/
function MyColor() {
var utils = new MyUtils();
this.black = utils.createCMYKColor(0, 0, 0, 100);
this.red = utils.createCMYKColor(0, 100, 100, 0);
return this;
}