Skip to content

Commit

Permalink
feat: add <RenderInterval> component
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 24, 2018
1 parent e1f9b6a commit 4cf6afd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
77 changes: 77 additions & 0 deletions src/RenderInterval/index.ts
@@ -0,0 +1,77 @@
import {Component} from 'react';
import {render, createEnhancer} from 'react-universal-interface';
import {h} from '../util';

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

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

export class RenderInterval extends Component<IRenderIntervalProps, IRenderIntervalState> {
static defaultProps = {
fps: 30,
ms: 300
};

delay: number = 50;
timeout = null;

state = {
start: 0,
now: 0,
value: 0
};

componentDidMount () {
const now = Date.now();

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

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

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

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

onFrame = () => {
const now = Date.now();
const value = Math.min((now - this.state.start) / this.props.ms, 1);
let onState;

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

this.setState({
now,
value
}, onState);
};

render () {
return render(this.props, this.state);
}
}

export const withRenderInterval = createEnhancer(RenderInterval, 'renderInterval');
8 changes: 6 additions & 2 deletions src/Tween/createCubicBezierEasing.ts
Expand Up @@ -12,12 +12,16 @@ const createCubicBezierEasing = (x1, y1, x2, y2) => {
const Ay = 1 - Cy - By;

return (t) => {
let x = t, i = 0, z;
let x = t;
let i = 0;
let z;

// Newton's root finding algorithm.
for (; i < maxIterations; i++) {
z = x * (Cx + x * (Bx + x * Ax)) - t;
if (Math.abs(z) < precision) break;
if (Math.abs(z) < precision) {
break;
}
x = x - z / (Cx + x * (Bx2 + x * Ax3));
}

Expand Down

0 comments on commit 4cf6afd

Please sign in to comment.