Commit 218a062d authored by JetLu's avatar JetLu 🚴🏻

add: cubic bezier

parent b9bcf550
This diff is collapsed.
...@@ -19,6 +19,7 @@ npm i git+ssh://git@gitlab2.dui88.com:lufei/moto.git ...@@ -19,6 +19,7 @@ npm i git+ssh://git@gitlab2.dui88.com:lufei/moto.git
```js ```js
import {curve, tween, easing} from 'moto' import {curve, tween, easing} from 'moto'
// 二次贝塞尔曲线
curve.bezier({ curve.bezier({
p1: {x: 0, y: 0}, p1: {x: 0, y: 0},
p2: {x: 50, y: 50}, p2: {x: 50, y: 50},
...@@ -29,6 +30,18 @@ curve.bezier({ ...@@ -29,6 +30,18 @@ curve.bezier({
console.log(v) console.log(v)
}) })
// 三次贝塞尔曲线
curve.cubicBezier({
p1: {x: 0, y: 0},
p2: {x: 30, y: 30},
p3: {x: 70, y: -30},
p4: {x: 100, y: 0},
duration: 3, // 可选
ease: easing.easeInOut // 时间函数,可选
}).start(v => {
console.log(v)
})
curve.catmullRom( curve.catmullRom(
[{x: 0, y: 0}, {x: 50, y: 50}, {x: 100, y: 0}], [{x: 0, y: 0}, {x: 50, y: 50}, {x: 100, y: 0}],
8 // 运动速度,可选 8 // 运动速度,可选
......
...@@ -24,6 +24,7 @@ document.body.on('pointerdown', ev => { ...@@ -24,6 +24,7 @@ document.body.on('pointerdown', ev => {
document.body.appendChild(dot) document.body.appendChild(dot)
} else if (target.classList.contains('btn-run')) { } else if (target.classList.contains('btn-run')) {
target.classList.contains('bezier') && run('bezier') target.classList.contains('bezier') && run('bezier')
target.classList.contains('cubic-bezier') && run('cubicBezier')
target.classList.contains('catmull-rom') && run('catmullRom') target.classList.contains('catmull-rom') && run('catmullRom')
target.classList.contains('tween') && run('tween') target.classList.contains('tween') && run('tween')
} else if (target.classList.contains('btn-clear')) { } else if (target.classList.contains('btn-clear')) {
...@@ -38,7 +39,7 @@ async function run(action) { ...@@ -38,7 +39,7 @@ async function run(action) {
player.setAttribute('style', '') player.setAttribute('style', '')
if (action === 'bezier') { if (action === 'bezier') {
if (dots.length < 2) return alert('至少 2 个点!') if (dots.length < 2) return alert('至少 2 个点!')
const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position)) const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position))
curve.bezier({ curve.bezier({
p1: {x: player.offsetLeft, y: player.offsetTop}, p1: {x: player.offsetLeft, y: player.offsetTop},
...@@ -47,14 +48,25 @@ async function run(action) { ...@@ -47,14 +48,25 @@ async function run(action) {
duration: 1, duration: 1,
ease: easing.easeInOut ease: easing.easeInOut
}).start(trace) }).start(trace)
} else if (action === 'cubicBezier') {
if (dots.length < 3) return alert('至少 3 个点!')
const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position))
curve.cubicBezier({
p1: {x: player.offsetLeft, y: player.offsetTop},
p2: points[0],
p3: points[1],
p4: points[2],
duration: 1,
ease: easing.easeInOut
}).start(trace)
} else if (action === 'catmullRom') { } else if (action === 'catmullRom') {
if (dots.length < 1) return alert('至少 1 个点!') if (dots.length < 1) return alert('至少 1 个点!')
const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position)) const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position))
points.unshift({x: player.offsetLeft, y: player.offsetTop}) points.unshift({x: player.offsetLeft, y: player.offsetTop})
curve.catmullRom(points).start(trace) curve.catmullRom(points).start(trace)
} else if (action === 'tween') { } else if (action === 'tween') {
if (dots.length < 1) return alert('至少 1 个点!') if (dots.length < 1) return alert('至少 1 个点!')
const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position)) const points = Array.prototype.map.call(dots, dot => JSON.parse(dot.dataset.position))
......
import {linear} from '../easing'
export default function(option) {
const {p1, p2, p3, p4, duration = 1, ease = linear} = option
function start(option) {
let id, update, complete, t = 0
if (option instanceof Function) update = option
else ({update, complete} = option)
!function loop() {
t += 1 / 60 / duration
t > 1 ? t = 1 : null
update(bezier(p1, p2, p3, p4, ease(t)))
t === 1 ? complete && complete() : id = requestAnimationFrame(loop)
}()
return {
stop() {
cancelAnimationFrame(id)
}
}
}
return {start}
}
function bezier(p1, p2, p3, p4, t) {
return {
x: calc(p1.x, p2.x, p3.x, p4.x, t),
y: calc(p1.y, p2.y, p3.y, p4.y, t)
}
}
function calc(p1, p2, p3, p4, t) {
return (1 - t) ** 3 * p1 +
3 * (1 - t) ** 2 * t * p2 +
3 * (1 - t) * t ** 2 * p3 +
t ** 3 * p4
}
\ No newline at end of file
export bezier from './bezier' export bezier from './bezier'
export cubicBezier from './bezier.cubic'
export catmullRom from './catmullRom' export catmullRom from './catmullRom'
\ No newline at end of file
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Curve</title> <title>MOTO</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
<section class="btn-group"> <section class="btn-group">
<button class="btn-clear">Clear</button> <button class="btn-clear">Clear</button>
<button class="btn-run bezier">Bezier</button> <button class="btn-run bezier">Bezier</button>
<button class="btn-run cubic-bezier">Cubic Bezier</button>
<button class="btn-run catmull-rom">CatmullRom</button> <button class="btn-run catmull-rom">CatmullRom</button>
<button class="btn-run tween">Tween</button> <button class="btn-run tween">Tween</button>
</section> </section>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment