使用颜色和样式

下面的教程中,我们将研究在项目中给项目添加样式的不同方法。

示例路径

本教程中的示例,我们将使用以下代码创建的复选标记形状路径:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]],
  3. selected: true
  4. });

使用颜色和样式 - 图1

请注意:

要了解如何使用 Paper.js 创建路径,请阅读使用路径项教程。

笔划颜色

要在我们的路径中添加笔划,我们需要设置其 strokeColor 属性。

下面的示例演示如何使用十六进制字符串(可从 HTML CSS 样式中了解)将我们之前创建的路径的笔划颜色设置为红色。

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]]
  3. });
  4. myPath.strokeColor = '#ff0000'; // 红色

使用颜色和样式 - 图2

十六进制颜色代码会自动转换为 Color 对象。

也可以直接使用 Color 对象设置颜色。 在下面的代码中,我们设置了RGB颜色,红色为50%,绿色为0%,蓝色为50%:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]]
  3. });
  4. myPath.strokeColor = new Color(0.5, 0, 0.5);

使用颜色和样式 - 图3

请注意:

Paper.js 中,颜色分量值的范围为0到1。

填充颜色

填充颜色的工作方式与笔划颜色完全相同。 下面的示例中,我们将创建一个填充红色的路径:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]],
  3. selected: true
  4. });
  5. myPath.fillColor = '#ff0000';

使用颜色和样式 - 图4

笔划宽度

要更改路径的笔划宽度,可以更改其 strokeWidth 属性。

下面的例子中,我们给路径一个 10pt 的红色笔划:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]]
  3. });
  4. myPath.strokeColor = '#ff0000';
  5. myPath.strokeWidth = 10;

使用颜色和样式 - 图5

笔划头

要更改路径开头和结尾的形状,可以将 strokeCap 属性更改为 “round”,”square” 或 “butt”:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]],
  3. selected: true
  4. });
  5. myPath.strokeColor = '#ff0000';
  6. myPath.strokeWidth = 10;
  7. myPath.strokeCap = 'round';

使用颜色和样式 - 图6

笔划连接

要更改路径中角的形状,可以将 strokeJoin 属性更改为 “miter”,”round” 或 “bevel”:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]],
  3. selected: true
  4. });
  5. myPath.strokeColor = '#ff0000';
  6. myPath.strokeWidth = 10;
  7. myPath.strokeJoin = 'round';

使用颜色和样式 - 图7

虚线笔划

要创建虚线笔划,可以更改项目的 dashArray 属性。 以下代码生成一个带有 10pt 短划线和 12pt 间隙的虚线笔划:

  1. var myPath = new Path({
  2. segments: [[40, 115], [80, 180], [200, 20]],
  3. selected: true
  4. });
  5. myPath.strokeColor = '#ff0000';
  6. myPath.strokeWidth = 5;
  7. myPath.strokeCap = 'round';
  8. myPath.dashArray = [10, 12];

PathStyle 对象

每个项都有一个 item.style 属性,它是一个只包含样式属性的对象。

我们可以使用它来将一个项的样式属性复制给另一个项。 下面的示例首先创建一个圆形路径并为其指定一种笔划颜色。 然后创建另一个圆形路径,我们为它指定第一个路径的样式:

  1. var firstPath = new Path.Circle({
  2. center: [80, 50],
  3. radius: 35
  4. });
  5. firstPath.strokeColor = '#ff0000';
  6. firstPath.fillColor = 'blue';
  7. // secondPath 暂时没有笔划颜色
  8. var secondPath = new Path.Circle({
  9. center: [160, 50],
  10. radius: 35
  11. });
  12. // 给 secondPath 应用 firstPath 的样式:
  13. secondPath.style = firstPath.style;

使用颜色和样式 - 图8

item.style 属性也可以一次性设置多个样式属性。

下面的示例中,我们创建一个包含多个样式属性的对象,并将其赋值给 myCircle 的 style 属性,这样可以一次性设置多个样式:

  1. var myStyle = {
  2. strokeColor: '#00ffff',
  3. fillColor: '#000000',
  4. strokeWidth: 50
  5. };
  6. var myCircle = new Path.Circle({
  7. center: [100, 100],
  8. radius: 50
  9. });
  10. myCircle.style = myStyle;

使用颜色和样式 - 图9

移除样式

想要删除任意路径样式,只需将指定样式属性赋值为 null:

  1. var path = new Path.Circle({
  2. center: new Point(50, 50),
  3. radius: 50
  4. });
  5. path.fillColor = 'red';
  6. // Set the fillColor to null to remove it:
  7. path.fillColor = null;

想要删除路径的所有样式,可以将 style 属性赋值为 null:

  1. var path = new Path.Circle({
  2. center: [50, 50],
  3. radius: 50
  4. });
  5. path.style = null;

使用当前项目的风格

正如我之前提到的,所有新创建的项都会自动使用 Illustrator 接口中定义的当前活动路径样式属性。 我们也可以使用 currentStyle,通过代码来更改这些样式。

currentStyle 是项目的 PathStyle 对象,包含当前活动的样式属性,如 fillColor 和 strokeColor。

以下示例更改项目的当前样式,然后创建继承了该样式的路径。 然后更改项目样式的 strokeWidth 和 fillColor 并创建另一个路径。

  1. // 更改项目当前的样式:
  2. project.currentStyle = {
  3. strokeColor: '#000000',
  4. fillColor: '#ff0000',
  5. strokeWidth: 3
  6. };
  7. // 路径会继承刚刚设置的样式:
  8. var firstPath = new Path.Circle({
  9. center: [100, 100],
  10. radius: 50
  11. });
  12. // 改变项目的当前笔划宽度和填充颜色:
  13. project.currentStyle.strokeWidth = 8;
  14. project.currentStyle.fillColor = 'green';
  15. // 这个路径有 8pt 宽度的笔划和绿色的填充色:
  16. var secondPath = new Path.Circle({
  17. center: [250, 100],
  18. radius: 50
  19. });

使用颜色和样式 - 图10