Skip to content

Commit 178d91c

Browse files
committed
Merge branch 'master' into dev
# Conflicts: # app/build/mojs-curve-editor.js # app/build/mojs-curve-editor.min.js # app/js/app.babel.jsx # package.json
2 parents d8d4c11 + a004e9e commit 178d91c

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<script>
7070
var curveEditor = new MojsCurveEditor({
7171
name: 'some name',
72+
// startPath: 'M0, 100 L100, 0'
7273
// onChange(path) { console.log(path); },
7374
// isHiddenOnMin: true
7475
});

app/js/app.babel.jsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import fallbackTo from './helpers/fallback-to';
88
import defer from './helpers/defer';
99
import {reset} from './actions/points';
1010
import addPointerDown from './helpers/add-pointer-down';
11+
import transformPathSegments from './helpers/get-points-from-path';
1112

1213
/*
1314
API wrapper above the app itself.
@@ -32,6 +33,7 @@ class API {
3233
name: 'mojs-curve-editor',
3334
isSaveState: true,
3435
isHiddenOnMin: false,
36+
startPath: '',
3537
onChange: null
3638
}
3739
}
@@ -101,8 +103,12 @@ class API {
101103

102104
_tryToRestore () {
103105
const stored = localStorage.getItem(this._localStorage);
104-
if ( stored ) { this.store.dispatch({ type: 'SET_STATE', data: JSON.parse(stored) });}
105-
else { reset(this.store); }
106+
const startPathIsSet = this._props.startPath.length > 0;
107+
if ( stored ) {
108+
this.store.dispatch({ type: 'SET_STATE', data: JSON.parse(stored) });
109+
} else if (startPathIsSet) {
110+
this._drawStartPath();
111+
} else { reset(this.store); }
106112
}
107113

108114
_subscribe () {
@@ -232,7 +238,33 @@ class API {
232238
);
233239
}
234240

241+
_getStartPathPoints() {
242+
const { startPath } = this._props;
243+
let newPoints = [];
244+
try {
245+
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
246+
path.setAttribute('d', startPath);
247+
newPoints = transformPathSegments(path);
248+
} catch (e) {
249+
console.log('Something went wrong while creating path element');
250+
}
251+
252+
return newPoints;
253+
}
235254

255+
_drawStartPath() {
256+
const newPoints = this._getStartPathPoints();
257+
if (newPoints.length) {
258+
this.store.dispatch({ type: 'POINTS_REMOVE' });
259+
260+
newPoints.forEach((point, index) => {
261+
this.store.dispatch({
262+
type: 'POINT_ADD',
263+
data: { point, index }
264+
});
265+
});
266+
}
267+
}
236268

237269
// highlight() { this.store.dispatch({ type: 'SET_HIGHLIGHT', data: true }); }
238270
// dim() { this.store.dispatch({ type: 'SET_HIGHLIGHT', data: false }); }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'path-data-polyfill';
2+
import pointToAngle from './point-to-angle';
3+
import C from '../constants';
4+
const { CURVE_PERCENT } = C;
5+
6+
export default (path) => {
7+
const points = [];
8+
const pathData = path.getPathData();
9+
10+
for (let i = 0; i < pathData.length; i++) {
11+
const dataSegment = pathData[i];
12+
const isFirstOrLastPoint = (i === 0 || i === pathData.length - 1);
13+
const {type, values} = dataSegment;
14+
15+
if (type === 'M' || type === 'L') {
16+
const [x, y] = values;
17+
points.push({
18+
x,
19+
y: y * CURVE_PERCENT,
20+
isLockedX: isFirstOrLastPoint
21+
});
22+
} else if (type === 'C') {
23+
const [x1, y1, x2, y2, xNext, yNext] = values;
24+
const prevPoint = points[i - 1];
25+
const prevHandle = pointToAngle(
26+
(x1 - prevPoint.x) * CURVE_PERCENT,
27+
(y1 * CURVE_PERCENT) - prevPoint.y
28+
);
29+
prevPoint.handle2 = prevHandle;
30+
31+
const point = {
32+
x: xNext,
33+
y: yNext * CURVE_PERCENT,
34+
type: 'disconnected',
35+
handle1: pointToAngle((x2 - xNext) * CURVE_PERCENT, (y2 - yNext) * CURVE_PERCENT),
36+
isLockedX: isFirstOrLastPoint
37+
}
38+
39+
points.push(point);
40+
}
41+
}
42+
43+
return points;
44+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"svg"
1414
],
1515
"main": "app/build/mojs-curve-editor.js",
16-
"scripts": {},
16+
"scripts": {
17+
"start": "webpack",
18+
"dev": "webpack-dev-server"
19+
},
1720
"repository": {
1821
"type": "git",
1922
"url": "git+https://github.com/legomushroom/mojs-curve-editor.git"
@@ -32,6 +35,7 @@
3235
"dependencies": {
3336
"hammerjs": "^2.0.8",
3437
"preact": "^7.1.0",
38+
"path-data-polyfill": "^1.0.0",
3539
"redux": "^3.5.2",
3640
"redux-recycle": "^1.2.0",
3741
"redux-undo": "^0.6.1"

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ const curveEditor = new MojsCurveEditor({
111111
name: 'bounce curve'
112112
// if should preserve state on page reloads
113113
isSaveState: true,
114+
// start path - will be loaded on initialization of the curve,
115+
// e.g. before any user modifications were made. Path of 'M0, 100 L100, 0' is set by default.
116+
startPath: 'M0, 100 L100, 0',
114117
// callback on path change, accepts path string
115118
onChange: function (path) {}
116119
// if should hide when minimized - useful when you try to embed the

0 commit comments

Comments
 (0)