• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

michaelvillar/dynamics.js: Javascript library to create physics-based animations

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

michaelvillar/dynamics.js

开源软件地址:

https://github.com/michaelvillar/dynamics.js

开源编程语言:

CoffeeScript 100.0%

开源软件介绍:

Dynamics.js

Dynamics.js is a JavaScript library to create physics-based animations

To see some demos, check out dynamicsjs.com.

Usage

Download:

Include dynamics.js into your page:

<script src="dynamics.js"></script>

You can animate CSS properties of any DOM element.

var el = document.getElementById("logo")
dynamics.animate(el, {
  translateX: 350,
  scale: 2,
  opacity: 0.5
}, {
  type: dynamics.spring,
  frequency: 200,
  friction: 200,
  duration: 1500
})

You also can animate SVG properties.

var path = document.querySelector("path")
dynamics.animate(path, {
  d: "M0,0 L0,100 L100,50 L0,0 Z",
  fill: "#FF0000",
  rotateZ: 45,
  // rotateCX and rotateCY are the center of the rotation
  rotateCX: 100,
  rotateCY: 100
}, {
  friction: 800
})

And any JavaScript object.

var o = {
  number: 10,
  color: "#FFFFFF",
  string: "10deg",
  array: [ 1, 10 ]
}
dynamics.animate(o, {
  number: 20,
  color: "#000000",
  string: "90deg",
  array: [-9, 99 ]
})

Reference

dynamics.animate(el, properties, options)

Animates an element to the properties with the animation options.

  • el is a DOM element, a JavaScript object or an Array of elements
  • properties is an object of the properties/values you want to animate
  • options is an object representing the animation
    • type is the animation type: dynamics.spring, dynamics.easeInOut,... (default: dynamics.easeInOut)
    • frequency, friction, bounciness,... are specific to the animation type you are using
    • duration is in milliseconds (default: 1000)
    • delay is in milliseconds (default: 0)
    • complete (optional) is the completion callback
    • change (optional) is called at every change. Two arguments are passed to the function. function(el, progress)
      • el is the element it's animating
      • progress is the progress of the animation between 0 and 1

dynamics.stop(el)

Stops the animation applied on the element

dynamics.css(el, properties)

This is applying the CSS properties to your element with the correct browser prefixes.

  • el is a DOM element
  • properties is an object of the CSS properties

dynamics.setTimeout(fn, delay)

Dynamics.js has its own setTimeout. The reason is that requestAnimationFrame and setTimeout have different behaviors. In most browsers, requestAnimationFrame will not run in a background tab while setTimeout will. This can cause a lot of problems while using setTimeout along your animations. I suggest you use Dynamics's setTimeout and clearTimeout to handle these scenarios.

  • fn is the callback
  • delay is in milliseconds

Returns a unique id

dynamics.clearTimeout(id)

Clears a timeout that was defined earlier

  • id is the timeout id

dynamics.toggleSlow()

Toggle a debug mode to slow down every animations and timeouts. This is useful for development mode to tweak your animation. This can be activated using Shift-Control-D in the browser.

Dynamics and properties

dynamics.spring

  • frequency default is 300
  • friction default is 200
  • anticipationSize (optional)
  • anticipationStrength (optional)

dynamics.bounce

  • frequency default is 300
  • friction default is 200

dynamics.forceWithGravity and dynamics.gravity

  • bounciness default is 400
  • elasticity default is 200

dynamics.easeInOut, dynamics.easeIn and dynamics.easeOut

  • friction default is 500

dynamics.linear

No properties

dynamics.bezier

  • points array of points and control points

The easiest way to output this kind of array is to use the curve creator. Here is an example:

[{"x":0,"y":0,"cp":[{"x":0.2,"y":0}]},
 {"x":0.5,"y":-0.4,"cp":[{"x":0.4,"y":-0.4},{"x":0.8,"y":-0.4}]},
 {"x":1,"y":1,"cp":[{"x":0.8,"y":1}]}]

Contributing

Compile: npm run build or npm run build:watch

Run tests: npm test

Browser Support

Working on

  • Safari 7+
  • Firefox 35+
  • Chrome 34+
  • IE10+

Sylvester

Some code from Sylvester.js has been used (part of Vector and Matrix).

License

The MIT License (MIT)

Copyright (c) 2015 Michael Villar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
chjj/term.js: A terminal written in javascript.发布时间:2022-07-07
下一篇:
idflood/ThreeNodes.js: vvvv &quot;clone&quot; in javascript/webgl发布时间:2022-07-07
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap