Skip to content

Commit

Permalink
feat: add delay prop to <Render> and <RenderInterv
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 24, 2018
1 parent 6ebce5e commit 8994010
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 69 deletions.
21 changes: 18 additions & 3 deletions src/Render/__story__/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Render} from '..';
import ShowDocs from '../../../.storybook/ShowDocs'

storiesOf('Animation/Render', module)
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Overlay.md')}))
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Render.md')}))
.add('Defaults', () =>
<div>
<Render>{({value}) =>
Expand All @@ -21,9 +21,24 @@ storiesOf('Animation/Render', module)
width: 100,
height: 100,
background: 'tomato',
opacity: value,
opacity: .1 + .9 * value,
position: 'relative',
top: (value * 100) - 100,
top: (value * 100),
left: 100
}}>{value}</div>
}</Render>
</div>
)
.add('Entry with delay', () =>
<div>
<Render delay={1000} ms={2000}>{({value}) =>
<div style={{
width: 100,
height: 100,
background: 'tomato',
opacity: .1 + .9 * value,
position: 'relative',
top: (value * 100),
left: 100
}}>{value}</div>
}</Render>
Expand Down
21 changes: 17 additions & 4 deletions src/Render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {render, createEnhancer} from 'react-universal-interface';
import {h} from '../util';

export interface IRenderProps {
delay?: number;
ms?: number;
}

Expand All @@ -17,6 +18,7 @@ export class Render extends Component<IRenderProps, IRenderState> {
ms: 300
};

timeout = null;
frame = null;

state = {
Expand All @@ -26,6 +28,21 @@ export class Render extends Component<IRenderProps, IRenderState> {
};

componentDidMount () {
const {delay} = this.props;

if (delay) {
this.timeout = setTimeout(this.start, delay);
} else {
this.start();
}
}

componentWillUnmount () {
cancelAnimationFrame(this.frame);
clearTimeout(this.timeout);
}

start = () => {
const now = Date.now();

this.setState({
Expand All @@ -37,10 +54,6 @@ export class Render extends Component<IRenderProps, IRenderState> {
});
}

componentWillUnmount () {
cancelAnimationFrame(this.frame);
}

onFrame = () => {
const now = Date.now();
const value = Math.min((now - this.state.start) / this.props.ms, 1);
Expand Down
74 changes: 52 additions & 22 deletions src/RenderInterval/__story__/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,56 @@ import {RenderInterval} from '..';
import ShowDocs from '../../../.storybook/ShowDocs'

storiesOf('Animation/RenderInterval', module)
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Overlay.md')}))
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/RenderInterval.md')}))
.add('Defaults', () =>
<div>
<RenderInterval>{({value}) =>
<div>{value}</div>
}</RenderInterval>
</div>
)
.add('Entry animation', () =>
<div>
<RenderInterval ms={2000}>{({value}) =>
<div style={{
width: 100,
height: 100,
background: 'tomato',
opacity: value,
position: 'relative',
top: (value * 100) - 100,
left: 100
}}>{value}</div>
}</RenderInterval>
</div>
)
<div>
<RenderInterval>{({value}) =>
<div>{value}</div>
}</RenderInterval>
</div>
)
.add('Entry animation', () =>
<div>
<RenderInterval ms={2000}>{({value}) =>
<div style={{
width: 100,
height: 100,
background: 'tomato',
opacity: .1 + .9 * value,
position: 'relative',
top: (value * 100),
left: 100
}}>{value}</div>
}</RenderInterval>
</div>
)
.add('Entry with delay', () =>
<div>
<RenderInterval delay={1000} ms={2000}>{({value}) =>
<div style={{
width: 100,
height: 100,
background: 'tomato',
opacity: .1 + .9 * value,
position: 'relative',
top: (value * 100),
left: 100
}}>{value}</div>
}</RenderInterval>
</div>
)
.add('Custom fps', () =>
<div>
<RenderInterval fps={10} ms={2000}>{({value}) =>
<div style={{
width: 100,
height: 100,
background: 'tomato',
opacity: .1 + .9 * value,
position: 'relative',
top: (value * 100),
left: 100
}}>{value}</div>
}</RenderInterval>
</div>
)
31 changes: 20 additions & 11 deletions src/RenderInterval/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {Component} from 'react';
import {render, createEnhancer} from 'react-universal-interface';
import {h} from '../util';
import {IRenderProps, IRenderState} from '../Render';

export interface IRenderIntervalProps {
export interface IRenderIntervalProps extends IRenderProps {
fps?: number;
ms?: number;
}

export interface IRenderIntervalState {
start?: number;
now?: number;
value?: number;
export interface IRenderIntervalState extends IRenderState {
}

export class RenderInterval extends Component<IRenderIntervalProps, IRenderIntervalState> {
Expand All @@ -19,8 +16,9 @@ export class RenderInterval extends Component<IRenderIntervalProps, IRenderInter
ms: 300
};

delay: number = 50;
interval: number = 50;
timeout = null;
timeoutDelay = null;

state = {
start: 0,
Expand All @@ -29,27 +27,38 @@ export class RenderInterval extends Component<IRenderIntervalProps, IRenderInter
};

componentDidMount () {
const {delay} = this.props;

if (delay) {
this.timeoutDelay = setTimeout(this.start, delay);
} else {
this.start();
}
}

start = () => {
const now = Date.now();

this.delay = 1000 / this.props.fps;
this.interval = 1000 / this.props.fps;

this.setState({
start: now,
now,
value: 0
}, () => {
this.timeout = setTimeout(this.onFrame, this.delay);
this.timeout = setTimeout(this.onFrame, this.interval);
});
}

componentDidUpdate (props) {
if (props.fps !== this.props.fps) {
this.delay = 1000 / this.props.fps;
this.interval = 1000 / this.props.fps;
}
}

componentWillUnmount () {
clearTimeout(this.timeout);
clearTimeout(this.timeoutDelay);
}

onFrame = () => {
Expand All @@ -59,7 +68,7 @@ export class RenderInterval extends Component<IRenderIntervalProps, IRenderInter

if (value < 1) {
onState = () => {
this.timeout = setTimeout(this.onFrame, this.delay);
this.timeout = setTimeout(this.onFrame, this.interval);
};
}

Expand Down
Loading

0 comments on commit 8994010

Please sign in to comment.