惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
J
Java Code Geeks
博客园_首页
Scott Helme
Scott Helme
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
V
Visual Studio Blog
Cloudbric
Cloudbric
Jina AI
Jina AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
W
WeLiveSecurity
K
Kaspersky official blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
量子位
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - 【当耐特】

苹果 icloud 把我 ipad min 所有照片丢失 Wechart 饼图 - 【当耐特】 - 博客园 【开源】Skatch 正式发布 - 极速渲染抽象派草图 - 【当耐特】 【开源】小程序、小游戏和Web运动引擎 to2to 发布 - 【当耐特】 【开源】微信小程序、小游戏以及 Web 通用 Canvas 渲染引擎 - Cax - 【当耐特】 【开源】Qone 正式发布,使 javascript 支持 .NET LINQ - 【当耐特】 你不容错过的 腾讯 AlloyTeam Web 前端大会 看点完全剖析 腾讯 AlloyCrop 1.0 发布 - 【当耐特】 50行代码实现的高性能动画定时器 raf-interval - 【当耐特】 - 博客园 QQ日迹Omi实战开发,从0到1 - 【当耐特】 - 博客园 腾讯AlloyTeam正式发布omi-cli脚手架 - 创建网站无需任何配置 - 【当耐特】 Omi树组件omi-tree编写指南 - 【当耐特】 - 博客园 腾讯AlloyTeam正式发布Canvas魔幻线条 - curvejs - 【当耐特】 Omi官方插件系列 - omi-transform介绍 - 【当耐特】 Omi新成员omi-router正式发布 - 【当耐特】 - 博客园 Omi架构与React Fiber - 【当耐特】 - 博客园 Omi框架Store体系的前世今生 - 【当耐特】 - 博客园 omi-cli新版发布-升级webpack2和支持sass生成组件局部CSS - 【当耐特】 - 博客园 Omi应用md2site-0.5.0发布-支持动态markdown拉取解析 - 【当耐特】 - 博客园
腾讯AlloyTeam正式发布pasition - 制作酷炫Path过渡动画 - 【当耐特】
【当耐特】 · 2017-06-21 · via 博客园 - 【当耐特】

pv
pv

pasition

Pasition - Path Transition with little JS code, render to anywhere - 超小尺寸的Path过渡动画类库

最近和贝塞尔曲线杠上了,如curvejspasition 都是贝塞尔曲线的应用案例,未来还有一款和贝塞尔曲线相关的开源的东西,暂时保密。

安装

npm install pasition

CDN地址下载下来使用:

https://unpkg.com/pasition@1.0.0/dist/pasition.js

使用指南

pasition.lerp

你可以通过 pasition.lerp 方法拿到插值中的shapes:

var shapes  = pasition.lerp(pathA, pathB, 0.5)
//拿到shapes之后你可以在任何你想要渲染的地方绘制,如canvas、svg、webgl等
...

pasition.animate

pasition.animate({
    from : fromPath,
    to : toPath,
    time : time,
    easing : function(){ },
    begin :function(shapes){ },
    progress : function(shapes, percent){ },
    end : function(shapes){ }
})

path从哪里来?你可以从svg的path的d属性获取。

支持所有的SVG Path命令:

M/m = moveto
L/l = lineto
H/h = horizontal lineto
V/v = vertical lineto
C/c = curveto
S/s = smooth curveto
A/a = elliptical Arc
Z/z = closepath
Q/q = quadratic Belzier curve
T/t = smooth quadratic Belzier curveto

举个例子:

pasition.animate({
            from: 'M 40 40 Q 60 80 80 40T 120 40 T 160 40 z',
            to: 'M32,0C14.4,0,0,14.4,0,32s14.3,32,32,32 s32-14.3,32-32S49.7,0,32,0z',
            time: 1000,
            easing : function(){ },
            begin:function(shapes){ },
            progress : function(shapes, percent){
                //你可以在任何你想绘制的地方绘制,如canvas、svg、webgl
            },
            end : function(shapes){ }
        });

对上面传入的配置项目一一解释下:

  • from 起始的路径
  • to 终点的路径
  • time 从from到to所需要的时间
  • easing 缓动函数(不填默认是匀速运动)
  • begin 开始运动的回调函数
  • progress 运动过程中的回调函数
  • end 运动结束的回调函数

在progress里可以拿到path转变过程中的shapes和运动进度percent(范围是0-1)。下面来看看shapes的结构:

[
    [
       [],    //curve
       [],    //curve
       []    //curve   
    ],      //shape      
    [[],[],[],[],[]],     //shape      
    [[],[],[],[],[]]     //shape    
]

在开发者工具里截图:

每条curve都包含8个数字,分别代表三次贝塞尔曲线的 起点 控制点 控制点 终点。

每个shape都是闭合的,所以shape的基本规则是:

  • 每条curve的终点就是下一条curve的起点
  • 最后一条curve的终点就是第一条curve的起点

知道基本规则之后,我们可以进行渲染,这里拿canvas里渲染为例子:

Fill模式:

function renderShapes(context, curves, color){
    context.beginPath();
    context.fillStyle = color||'black';
    context.moveTo(curves[0][0], curves[0][1]);
    curves.forEach(function(points){
        context.bezierCurveTo(points[2], points[3], points[4], points[5], points[6], points[7]);
    })
    context.closePath();
    context.fill();
}

shapes.forEach(function(curves){
    renderShapes(context,curves,"#006DF0")
})

Stroke模式:

function renderCurve(context, points, color){
    context.beginPath();
    context.strokeStyle = color||'black';
    context.moveTo(points[0], points[1]);
    context.bezierCurveTo(points[2], points[3], points[4], points[5], points[6], points[7]);
    context.stroke();
}

shapes.forEach(function(curves){
    curves.forEach(function (curve) {
        renderCurve(context, curve, "#006DF0")
    })	
})

当然你也可以把shapes转成SVG的命令在SVG渲染,这应该不是什么困难的事情:

    function toSVGPath(shapes){
        //把 shapes数组转成 M....C........C........Z M....C.....C....C...Z 的字符串。
    }

这个函数可以自行尝试一下,生成出的字符串赋值给SVG的Path的d就可以了。

Github

https://github.com/AlloyTeam/pasition

License

This content is released under the MIT License.