gpt4 book ai didi

animation - 您可以在动画时围绕贝塞尔曲线路径 "bend"SVG 吗?

转载 作者:行者123 更新时间:2023-12-02 02:47:04 31 4
gpt4 key购买 nike

当 SVG 对象沿着贝塞尔曲线进行动画处理时,我有什么方法可以“弯曲”它?我主要使用 GSAP 来制作动画。效果看起来像这样:https://www.behance.net/gallery/49401667/Twist ed-letters-2(带蓝色铅笔的那个)。我设法让红色箭头沿路径设置动画,但形状始终保持不变。我希望它沿着绿色路径前进并在它绕曲线移​​动时弯曲,以便在动画结束时它具有紫色箭头的形状。这是codepen .

GSAP代码:

var motionPath = MorphSVGPlugin.pathDataToBezier("#motionPath", {align:"#arrow1"});
var tl1 = new TimelineMax({paused:true, reversed:true});
tl1.set("#arrow1", {xPercent:-50, yPercent:-50});
tl1.to("#arrow1", 4, {bezier:{values:motionPath, type:"cubic"}});


$("#createAnimation").click(function(){
tl1.reversed() ? tl1.play() : tl1.reverse();
});

有没有办法只用 GSAP 来做到这一点?或者我需要像 Pixi 这样的东西?

最佳答案

这就是我将如何做到的:

首先,我需要一组点来绘制箭头和轨迹。我想在轨道上移动箭头,箭头应该跟随轨道弯曲。为了在动画的每一帧中实现这种效果,我正在计算箭头点的新位置。

另外:轨道是接缝长度的两倍。

请阅读代码中的注释

let track = document.getElementById("track");
let trackLength = track.getTotalLength();
let t = 0.1;// the position on the path. Can take values from 0 to .5
// an array of points used to draw the arrow
let points = [
[0, 0],[6.207, -2.447],[10.84, -4.997],[16.076, -7.878],[20.023, -10.05],[21.096, -4.809],[25.681, -4.468],[31.033, -4.069],[36.068, -3.695],[40.81, -3.343],[45.971, -2.96],[51.04, -2.584],[56.075, -2.21],[60.838, -1.856],[65.715, -1.49],[71.077, -1.095],[75.956, -0.733],[80, 0],[75.956, 0.733],[71.077, 1.095],[65.715, 1.49],[60.838, 1.856],[56.075, 2.21],[51.04, 2.584],[45.971, 2.96],[40.81, 3.343],[36.068, 3.695],[31.033, 4.069],[25.681, 4.468],[21.096, 4.809],[20.023, 10.05],[16.076, 7.878],[10.84, 4.997],[6.207, 2.447],[0, 0]
];

function move() {
requestAnimationFrame(move);
if (t > 0) {
t -= 0.001;
} else {
t = 0.5;
}
let ry = newPoints(track, t);
drawArrow(ry);
}

move();



function newPoints(track, t) {
// a function to change the value of every point on the points array
let ry = [];
points.map(p => {
ry.push(getPos(track, t, p[0], p[1]));
});
return ry;
}

function getPos(track, t, d, r) {
// a function to get the position of every point of the arrow on the track
let distance = d + trackLength * t;
// a point on the track
let p = track.getPointAtLength(distance);
// a point near p used to calculate the angle of rotation
let _p = track.getPointAtLength((distance + 1) % trackLength);
// the angle of rotation on the path
let a = Math.atan2(p.y - _p.y, p.x - _p.x) + Math.PI / 2;
// returns an array of coordinates: the first is the x, the second is the y
return [p.x + r * Math.cos(a), p.y + r * Math.sin(a)];
}

function drawArrow(points) {
// a function to draw the arrow in base of the points array
let d = `M${points[0][0]},${points[0][1]}L`;
points.shift();
points.map(p => {
d += `${p[0]}, ${p[1]} `;
});
d += "Z";
arrow.setAttributeNS(null, "d", d);
}
svg {
display: block;
margin: 2em auto;
border: 1px solid;
overflow: visible;
width:140vh;
}
#track {
stroke: #d9d9d9;
vector-effect: non-scaling-stroke;
}
<svg viewBox="-20 -10 440 180">

<path id="track" fill="none"
d="M200,80
C-50,280 -50,-120 200,80
C450,280 450,-120 200,80
C-50,280 -50,-120 200,80
C450,280 450,-120 200,80Z" />

<path id="arrow" d="" />
</svg>

关于animation - 您可以在动画时围绕贝塞尔曲线路径 "bend"SVG 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54155535/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com