用PIXI画线,用TweenMax移动
Draw a line with PIXI and move it with TweenMax
我看了一个使用这两个框架(PIXIjs 和 GSAP)的例子。所以我想用它
我有点卡壳了。
所以我想做的是画 3 条线来匹配我的 window 的边界。没关系,但在第二步中,我希望这条线在不离开边界的情况下移动。
这是我对这部分的小代码
// This is how I create the line
var lineArray = [];
for (var i = 0; i < 3; i++) {
var line = new PIXI.Graphics();
line.lineStyle(1, 0xf3a33f);
if(i == 0) {
line.moveTo(getRandomInt(0, window.innerWidth), window.innerHeight);
line.lineTo(getRandomInt(0, window.innerWidth), 0);
} else if(i == 1) {
line.moveTo(0, getRandomInt(0, window.innerHeight));
line.lineTo(window.innerWidth, getRandomInt(0, window.innerHeight));
} else {
line.moveTo(getRandomInt(0, window.innerWidth), window.innerHeight);
line.lineTo(window.innerWidth, getRandomInt(0, window.innerHeight));
}
line.endFill();
line.alpha = 0;
stage.addChild(line);
lineArray.push(line);
}
// And this is how I want to animate it
var timeline = new TimelineMax({ paused: true });
for (var i = lineArray.length - 1; i >= 0; i--) {
lineArray[i].beginFill(0xf3a33f, 1);
timeline.add(TweenMax.to( lineArray[i], .05, {alpha: 1}), 1.25);
}
timeline.play();
有没有办法移动图形形状的lineTo(x, y) 和moveTo(x, y)?
我以为我每次移动线都可以重新绘制,并破坏旧的,但我希望有更简单的方法来做到这一点。
干杯,
H4mm3R
如果您希望能够 动画/补间 moveTo
和 lineTo
值,那么每个 [=] 的 graphics
对象14=] 将需要更新,即 clear();
,然后使用通常的 moveTo
和 lineTo
调用重新绘制新值。所有这些都发生在 render
函数中,它会及时更新您的 canvas。
另一件事是您需要一种方法来跟踪您的 start 和 end 值。我在下面的示例中使用了名称为 currPoints
和 destPoints
的数组,其代码如下:
JavaScript:
var lineWidth = 2,
lineColor = 0xf3a33f,
length = 4,
currPoints = [],
destPoints = [],
lineArray = [],
duration = 1.4,
ease = Power4.easeInOut,
staggerFactor = .06;
function init() {
initScene();
initLines();
animateLines();
TweenLite.ticker.addEventListener('tick', render);
}
function animateLines() {
for (var i = 0; i < length; i += 1) {
TweenMax.fromTo(lineArray[i], duration, {
alpha: 0
}, {
delay: i * staggerFactor,
alpha: 1,
repeat: -1,
yoyo: true,
repeatDelay: duration * .5,
ease: ease
});
TweenMax.to(currPoints[i].moveTo, duration, {
delay: i * staggerFactor,
x: destPoints[i].moveTo.x,
y: destPoints[i].moveTo.y,
repeat: -1,
yoyo: true,
repeatDelay: duration * .5,
ease: ease
});
TweenMax.to(currPoints[i].lineTo, duration, {
delay: i * staggerFactor,
x: destPoints[i].lineTo.x,
y: destPoints[i].lineTo.y,
repeat: -1,
yoyo: true,
repeatDelay: duration * .5,
ease: ease
});
}
}
function initLines() {
var line;
for (var i = 0; i < length; i += 1) {
line = new PIXI.Graphics().lineStyle(1, 0xf3a33f);
if (i == 0) {
currPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, getRandomInt(0, window.innerWidth), 0);
destPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, getRandomInt(0, window.innerWidth), 0);
} else if (i == 1) {
currPoints[i] = getPoint(0, getRandomInt(0, window.innerHeight), window.innerWidth, getRandomInt(0, window.innerHeight));
destPoints[i] = getPoint(0, getRandomInt(0, window.innerHeight), window.innerWidth, getRandomInt(0, window.innerHeight));
} else {
currPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, window.innerWidth, getRandomInt(0, window.innerHeight));
destPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, window.innerWidth, getRandomInt(0, window.innerHeight));
}
line.moveTo(currPoints[i].moveTo.x, currPoints[i].moveTo.y);
line.lineTo(currPoints[i].lineTo.x, currPoints[i].lineTo.y);
main.addChild(line);
lineArray.push(line);
}
}
function initScene() {
renderer = PIXI.autoDetectRenderer(window.innerWidth, window.innerHeight, {
view: document.querySelector('canvas'),
antialias: true
});
main = new PIXI.Container();
}
function render() {
renderer.render(main);
for (var i = 0; i < length; i += 1) {
lineArray[i].clear();
lineArray[i].lineStyle(lineWidth, lineColor);
lineArray[i].moveTo(currPoints[i].moveTo.x, currPoints[i].moveTo.y);
lineArray[i].lineTo(currPoints[i].lineTo.x, currPoints[i].lineTo.y);
}
}
function getPoint(xMoveTo, yMoveTo, xLineTo, yLineTo) {
return {
moveTo: {
x: xMoveTo,
y: yMoveTo
},
lineTo: {
x: xLineTo,
y: yLineTo
}
};
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (1 + max - min) + min);
};
//
init();
试试 jsFiddle,如果这就是您要找的,请告诉我。
T
我看了一个使用这两个框架(PIXIjs 和 GSAP)的例子。所以我想用它
我有点卡壳了。 所以我想做的是画 3 条线来匹配我的 window 的边界。没关系,但在第二步中,我希望这条线在不离开边界的情况下移动。
这是我对这部分的小代码
// This is how I create the line
var lineArray = [];
for (var i = 0; i < 3; i++) {
var line = new PIXI.Graphics();
line.lineStyle(1, 0xf3a33f);
if(i == 0) {
line.moveTo(getRandomInt(0, window.innerWidth), window.innerHeight);
line.lineTo(getRandomInt(0, window.innerWidth), 0);
} else if(i == 1) {
line.moveTo(0, getRandomInt(0, window.innerHeight));
line.lineTo(window.innerWidth, getRandomInt(0, window.innerHeight));
} else {
line.moveTo(getRandomInt(0, window.innerWidth), window.innerHeight);
line.lineTo(window.innerWidth, getRandomInt(0, window.innerHeight));
}
line.endFill();
line.alpha = 0;
stage.addChild(line);
lineArray.push(line);
}
// And this is how I want to animate it
var timeline = new TimelineMax({ paused: true });
for (var i = lineArray.length - 1; i >= 0; i--) {
lineArray[i].beginFill(0xf3a33f, 1);
timeline.add(TweenMax.to( lineArray[i], .05, {alpha: 1}), 1.25);
}
timeline.play();
有没有办法移动图形形状的lineTo(x, y) 和moveTo(x, y)? 我以为我每次移动线都可以重新绘制,并破坏旧的,但我希望有更简单的方法来做到这一点。
干杯, H4mm3R
如果您希望能够 动画/补间 moveTo
和 lineTo
值,那么每个 [=] 的 graphics
对象14=] 将需要更新,即 clear();
,然后使用通常的 moveTo
和 lineTo
调用重新绘制新值。所有这些都发生在 render
函数中,它会及时更新您的 canvas。
另一件事是您需要一种方法来跟踪您的 start 和 end 值。我在下面的示例中使用了名称为 currPoints
和 destPoints
的数组,其代码如下:
JavaScript:
var lineWidth = 2,
lineColor = 0xf3a33f,
length = 4,
currPoints = [],
destPoints = [],
lineArray = [],
duration = 1.4,
ease = Power4.easeInOut,
staggerFactor = .06;
function init() {
initScene();
initLines();
animateLines();
TweenLite.ticker.addEventListener('tick', render);
}
function animateLines() {
for (var i = 0; i < length; i += 1) {
TweenMax.fromTo(lineArray[i], duration, {
alpha: 0
}, {
delay: i * staggerFactor,
alpha: 1,
repeat: -1,
yoyo: true,
repeatDelay: duration * .5,
ease: ease
});
TweenMax.to(currPoints[i].moveTo, duration, {
delay: i * staggerFactor,
x: destPoints[i].moveTo.x,
y: destPoints[i].moveTo.y,
repeat: -1,
yoyo: true,
repeatDelay: duration * .5,
ease: ease
});
TweenMax.to(currPoints[i].lineTo, duration, {
delay: i * staggerFactor,
x: destPoints[i].lineTo.x,
y: destPoints[i].lineTo.y,
repeat: -1,
yoyo: true,
repeatDelay: duration * .5,
ease: ease
});
}
}
function initLines() {
var line;
for (var i = 0; i < length; i += 1) {
line = new PIXI.Graphics().lineStyle(1, 0xf3a33f);
if (i == 0) {
currPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, getRandomInt(0, window.innerWidth), 0);
destPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, getRandomInt(0, window.innerWidth), 0);
} else if (i == 1) {
currPoints[i] = getPoint(0, getRandomInt(0, window.innerHeight), window.innerWidth, getRandomInt(0, window.innerHeight));
destPoints[i] = getPoint(0, getRandomInt(0, window.innerHeight), window.innerWidth, getRandomInt(0, window.innerHeight));
} else {
currPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, window.innerWidth, getRandomInt(0, window.innerHeight));
destPoints[i] = getPoint(getRandomInt(0, window.innerWidth), window.innerHeight, window.innerWidth, getRandomInt(0, window.innerHeight));
}
line.moveTo(currPoints[i].moveTo.x, currPoints[i].moveTo.y);
line.lineTo(currPoints[i].lineTo.x, currPoints[i].lineTo.y);
main.addChild(line);
lineArray.push(line);
}
}
function initScene() {
renderer = PIXI.autoDetectRenderer(window.innerWidth, window.innerHeight, {
view: document.querySelector('canvas'),
antialias: true
});
main = new PIXI.Container();
}
function render() {
renderer.render(main);
for (var i = 0; i < length; i += 1) {
lineArray[i].clear();
lineArray[i].lineStyle(lineWidth, lineColor);
lineArray[i].moveTo(currPoints[i].moveTo.x, currPoints[i].moveTo.y);
lineArray[i].lineTo(currPoints[i].lineTo.x, currPoints[i].lineTo.y);
}
}
function getPoint(xMoveTo, yMoveTo, xLineTo, yLineTo) {
return {
moveTo: {
x: xMoveTo,
y: yMoveTo
},
lineTo: {
x: xLineTo,
y: yLineTo
}
};
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (1 + max - min) + min);
};
//
init();
试试 jsFiddle,如果这就是您要找的,请告诉我。
T