Git Product home page Git Product logo

reanimated-math.macro's Introduction

reanimated-math.macro

react-native-reanimated is amazing library for handling animations in react native.

Unfortunately nested function calls can get hard to read even if you are doing only simple arithmetic operations:

import { divide, multiply, sub } from 'react-native-reanimated'

function project(initialVelocity, decelerationRate) {
  return divide(
    multiply(divide(initialVelocity, 1000), decelerationRate),
    sub(1, decelerationRate),
  );
}

This babel.macro aims to simplify arithmetic over reanimated values.

Installation

yarn add reanimated-math.macro -D

If you are using older version of react-native than 0.56 you have to install and configure babel-plugin-macros as well.

Usage

import re from 'reanimated-math.macro'

function project(initialVelocity, decelerationRate) {
  return re(initialVelocity / 1000 * decelerationRate / (1 - decelerationRate))
}

or

import re from 'reanimated-math.macro'

function project(initialVelocity, decelerationRate) {
  return re`${initialVelocity} / 1000 * ${decelerationRate} / (1 - ${decelerationRate})`
}

reanimated TaggedTemplateExpression CallExpression
add(a, b, 1) re`${a} + ${b} + 1` re(a + b + 1)
sub(a, b, 1) re`${a} - ${b} - 1` re(a - b - 1)
multiply(a, b, 1) re`${a} * ${b} * 1` re(a * b * 1)
divide(a, b, 1) re`${a} / ${b} / 1` re(a / b / 1)
pow(a, b, 1) re`${a} ** ${b} ** 1` re(a ** b ** 1)
mod(a, b, 1) re`${a} % ${b} % 1` re(a % b % 1)
sqrt(a) re`sqrt(${a})` re(sqrt(a))
sin(a) re`sin(${a})` re(sin(a))
cos(a) re`cos(${a})` re(cos(a))
exp(a) re`exp(${a})` re(exp(a))
round(a) re`round(${a})` re(round(a))
floor(a) re`floor(${a})` re(floor(a))
ceil(a) re`ceil(${a})` re(ceil(a))
abs(a) re`abs(${a})` re(abs(a))
multiply(a, -1) re`-${a}` re(-a)

reanimated-math.macro's People

Contributors

futuun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.