对象转换

Paper.js 的一个重要特性是它在将值传递给函数时具有灵活的参数转换能力。 在这些情况下,所有基本类型也可以描述为数组或纯JavaScript 对象。 数组只是标准序列中默认属性的列表。 一些例子:

  1. // 以 JS 对象形式创建一个矩形:
  2. var rect = new Rectangle({ x: 10, y: 20, width: 100, height: 200 });
  3. console.log(rect); // { x: 10, y: 20, width: 100, height: 200 }
  4. // 以数组形式定义尺寸 [width, height]:
  5. rect.size = [200, 300];
  6. console.log(rect); // { x: 10, y: 20, width: 200, height: 300 }
  7. // 通过 JS 对象形式改变矩形的坐标位置:
  8. rect.point = { x: 20, y: 40 };
  9. console.log(rect); // { x: 20, y: 40, width: 200, height: 300 }

注意,在需要时,点会动态转换为尺寸,反之亦然:

  1. var rect = new Rectangle();
  2. rect.point = { width: 100, height: 200 };
  3. console.log(rect); // { x: 100, y: 200, width: 0, height: 0 }
  4. rect.size = { x: 200, y: 400 };
  5. console.log(rect); // { x: 100, y: 200, width: 200, height: 400 }