From f9828d0c1d349704d646475a0acdc5871a633745 Mon Sep 17 00:00:00 2001 From: Colin Meinke Date: Sun, 29 Jan 2017 11:10:03 +0100 Subject: [PATCH] feat(shape): add rotate manipulation property Closes #60. --- README.md | 2 ++ examples/fill-rule/client.js | 1 + src/core/shape/create.js | 6 +++++- src/core/shape/props.js | 1 + tests/core/shape/create.js | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5c446e..cedca9a 100644 --- a/README.md +++ b/README.md @@ -415,6 +415,8 @@ The functions are defined as the following properties. - `reverse` is a *boolean* that when: - `true` reverses the order of points - `false` has no effect += `rotate` is a *number* that defines the degrees to rotate the + shape clockwise - `scale` is either: - a *number* that defines the scale factor, or - an *array* that includes: diff --git a/examples/fill-rule/client.js b/examples/fill-rule/client.js index 4c55ac5..1038112 100644 --- a/examples/fill-rule/client.js +++ b/examples/fill-rule/client.js @@ -4,6 +4,7 @@ const from1 = { type: 'path', d: 'M20,20H80V80H20ZM30,30V70H70ZM40,40H60V60H40Z', fillRule: 'evenodd', + rotate: 45 } const from2 = { diff --git a/src/core/shape/create.js b/src/core/shape/create.js index 5d1ce7b..5a77438 100644 --- a/src/core/shape/create.js +++ b/src/core/shape/create.js @@ -23,6 +23,7 @@ * * @property {integer} [moveIndex] - the amount of points by which to offset a shape's index point. * @property {number[]} [offset] - the amount to offset a shape's points horizontally and vertically. + * @property {number} [rotate] - the clockwise angle to rotate the shape. * @property {boolean} [reverse] - reverses the shape's points. * @property {(number|(number|string)[])} [scale] - scales a shape. */ @@ -134,7 +135,7 @@ import { } from './props' import { filter } from '../helpers' -import { moveIndex, offset, reverse, scale } from 'points' +import { moveIndex, offset, reverse, rotate, scale } from 'points' import { toPoints } from 'svg-points' /** @@ -267,6 +268,9 @@ const manipulate = ({ manipulations, points }) => ( break + case 'rotate': + return rotate(nextPoints, args) + case 'scale': const isArray = Array.isArray(args) const scaleFactor = isArray ? args[ 0 ] : args diff --git a/src/core/shape/props.js b/src/core/shape/props.js index 20f9860..196bc40 100644 --- a/src/core/shape/props.js +++ b/src/core/shape/props.js @@ -18,6 +18,7 @@ const manipulationProps = [ 'moveIndex', 'offset', 'reverse', + 'rotate', 'scale' ] diff --git a/tests/core/shape/create.js b/tests/core/shape/create.js index 9969515..54caab8 100644 --- a/tests/core/shape/create.js +++ b/tests/core/shape/create.js @@ -375,6 +375,28 @@ describe('manipulate', () => { expect(manipulate({ manipulations, points })).toEqual(result) }) + it('should rotate points', () => { + const manipulations = { rotate: 90 } + + const points = [ + { x: 0, y: 0, moveTo: true }, + { x: 100, y: 0 }, + { x: 100, y: 100 }, + { x: 0, y: 100 }, + { x: 0, y: 0 } + ] + + const result = [ + { x: 100, y: 0, moveTo: true }, + { x: 100, y: 100 }, + { x: 0, y: 100 }, + { x: 0, y: 0 }, + { x: 100, y: 0 } + ] + + expect(manipulate({ manipulations, points })).toEqual(result) + }) + it('should scale points', () => { const manipulations = { scale: 2 }