Skip to content

Commit

Permalink
feat: shape morphing with non-matching points
Browse files Browse the repository at this point in the history
Closes #5.
  • Loading branch information
colinmeinke committed Jun 28, 2016
1 parent 9f02b64 commit 1e39925
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/morph/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
client.dist.js
node_modules/
npm-debug.log
10 changes: 10 additions & 0 deletions examples/morph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Wilderness morph example

## Usage

```
npm install
npm run build
```

Then open `index.html` in your browser.
26 changes: 26 additions & 0 deletions examples/morph/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { shape, render, play } from '../../src';

const from = {
type: 'circle',
cx: 25,
cy: 25,
r: 25,
fill: '#E54',
};

const to = {
type: 'path',
d: 'M75,75l10-5l15,20v10h-10l-5-5l-10,5z',
moveIndex: 1,
reverse: true,
};

const animation = shape( from, to );

render({ selector: '.svg' }, animation );

play( animation, {
alternate: true,
duration: 2000,
iterations: Infinity,
});
32 changes: 32 additions & 0 deletions examples/morph/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>Wilderness morph example</title>
<style>
html,
body {
height: 100%;
}

body {
margin: 0;
}

.svg {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<svg
class="svg"
height="100"
preserveAspectRatio="xMidYMid meet"
viewBox="0 0 100 100"
width="100"
>
</svg>
<script src="./client.dist.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/morph/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"author": {
"name": "Colin Meinke",
"email": "hello@colinmeinke.com",
"url": "www.colinmeinke.com"
},
"babel": {
"plugins": [
"transform-object-rest-spread"
],
"presets": [
"es2015"
]
},
"bugs": {
"url": "https://github.com/colinmeinke/wilderness/issues"
},
"description": "Wilderness morph example",
"devDependencies": {
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babelify": "^7.3.0",
"browserify": "^13.0.1"
},
"license": "ISC",
"repository": {
"type": "git",
"url": "https://github.com/colinmeinke/wilderness.git"
},
"scripts": {
"build": "browserify ./client.js -o ./client.dist.js -t babelify"
},
"version": "0.0.0-semantically-released"
}
2 changes: 1 addition & 1 deletion examples/playback/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Wilderness basic example
# Wilderness playback example

## Usage

Expand Down
2 changes: 1 addition & 1 deletion examples/playback/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Wilderness playback controls example</title>
<title>Wilderness playback example</title>
<style>
html,
body {
Expand Down
2 changes: 1 addition & 1 deletion examples/playback/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"bugs": {
"url": "https://github.com/colinmeinke/wilderness/issues"
},
"description": "Wilderness basic example",
"description": "Wilderness playback example",
"devDependencies": {
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
Expand Down
2 changes: 2 additions & 0 deletions src/core/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

import { filter } from './misc';
import match from './match';
import normalise from './points';

export {
currentIteration,
Expand All @@ -21,6 +22,7 @@ export {
finished,
iterationsComplete,
match,
normalise,
paused,
previousIteration,
};
43 changes: 43 additions & 0 deletions src/core/helpers/points.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { add, cubify, remove } from 'points';

const addCurves = ( a, b ) => {
const c = [];

for ( let i = 0, l = a.length; i < l; i++ ) {
if ( !a[ i ].curve && b[ i ].curve ) {
c.push({ ...a[ i ], curve: {
type: 'cubic',
x1: a[ i - 1 ].x,
y1: a[ i - 1 ].y,
x2: a[ i ].x,
y2: a[ i ].y,
}});
} else {
c.push( a[ i ]);
}
}

return c;
}

const normaliseCurves = ( a, b ) => ([ addCurves( a, b ), addCurves( b, a )]);

const normalisePoints = ( a, b ) => {
let c = cubify( remove( a ));
let d = cubify( remove( b ));

if ( d.length > c.length ) {
c = add( c, d.length );
} else if ( c.length > d.length ) {
d = add( d, c.length );
}

return normaliseCurves( c, d );
};

const normalise = ( a, b ) => {
const [ c, d ] = normalisePoints( a.points, b.points );
return [{ ...a, points: c }, { ...b, points: d }];
};

export default normalise;
6 changes: 4 additions & 2 deletions src/core/shape/update.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { currentState, easingFunc, match } from '../helpers';
import { currentState, easingFunc, match, normalise } from '../helpers';
import { stylePropAttrMap } from './props';
import { toPath } from 'svg-points';

Expand Down Expand Up @@ -39,7 +39,9 @@ const currentShape = ({ shapes, state, timeline }) => {
const t = d * offset / scale;
const ease = typeof easing === 'function' ? easing : easingFunc( easing );

return match( shape1, shape2, ( b, e ) => b === e ? b : ease( t, b, e, d ));
const [ s1, s2 ] = normalise( shape1, shape2 );

return match( s1, s2, ( b, e ) => b === e ? b : ease( t, b, e, d ));
};

const update = shape => {
Expand Down

0 comments on commit 1e39925

Please sign in to comment.