Skip to content

Commit c93909b

Browse files
committed
Simplify strokes using simplify-js
1 parent dcfed98 commit c93909b

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

blocks/sketch/src/edit.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import getStroke from 'perfect-freehand';
55
import { useCallback } from '@wordpress/element';
6+
import simplify from 'simplify-js';
67

78
/**
89
* Internal dependencies
@@ -68,7 +69,7 @@ const Edit = ( { attributes, isSelected, setAttributes } ) => {
6869
strokes: [
6970
...strokes,
7071
{
71-
stroke,
72+
stroke: simplifyPath( stroke ),
7273
color,
7374
},
7475
],
@@ -190,5 +191,10 @@ export const StrokePath = ( { stroke } ) => {
190191
return <path fill={ color } d={ getSvgPathFromStroke( stroke.stroke ) } />;
191192
};
192193

193-
// export default Edit;
194+
export const simplifyPath = ( stroke ) => {
195+
const mapped = stroke.map( item => ( { x: item[ 0 ], y: item[ 1 ] } ) );
196+
const simplified = simplify( mapped ).map( item => ( [ item.x, item.y ] ) );
197+
return simplified;
198+
};
199+
194200
export default Edit;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"classnames": "^2.3.1",
6262
"fast-average-color": "^7.0.1",
6363
"perfect-freehand": "1.2.0",
64+
"simplify-js": "1.2.4",
6465
"tinycolor2": "^1.4.2"
6566
}
6667
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14335,6 +14335,11 @@ simple-html-tokenizer@^0.5.7:
1433514335
resolved "https://registry.yarnpkg.com/simple-html-tokenizer/-/simple-html-tokenizer-0.5.11.tgz#4c5186083c164ba22a7b477b7687ac056ad6b1d9"
1433614336
integrity sha512-C2WEK/Z3HoSFbYq8tI7ni3eOo/NneSPRoPpcM7WdLjFOArFuyXEjAoCdOC3DgMfRyziZQ1hCNR4mrNdWEvD0og==
1433714337

14338+
simplify-js@1.2.4:
14339+
version "1.2.4"
14340+
resolved "https://registry.yarnpkg.com/simplify-js/-/simplify-js-1.2.4.tgz#7aab22d6df547ffd40ef0761ccd82b75287d45c7"
14341+
integrity sha512-vITfSlwt7h/oyrU42R83mtzFpwYk3+mkH9bOHqq/Qw6n8rtR7aE3NZQ5fbcyCUVVmuMJR6ynsAhOfK2qoah8Jg==
14342+
1433814343
sirv@^1.0.7:
1433914344
version "1.0.19"
1434014345
resolved "https://registry.yarnpkg.com/sirv/-/sirv-1.0.19.tgz#1d73979b38c7fe91fcba49c85280daa9c2363b49"

0 commit comments

Comments
 (0)