javascript - Anime.js 使用画布添加相应的动画
问题描述
我正在尝试使用画布和 Anime.js 进行简单的交互,使用 Julian 示例之一的修改版本。这个想法是一个圆圈会在屏幕上放大,然后用户点击屏幕上的任意位置,圆圈会收缩。我无法播放第二个动画,不确定这是因为我的目标无效还是与时间线有关?这是示例。
//Canvas set up
var canvas = document.querySelector('.gameCanvas');
var ctx = canvas.getContext('2d');
var pointerX = 0;
var pointerY = 0;
var circle;
let basicTimeline = anime.timeline();
function setCanvasSize() {
canvas.width = window.innerWidth * 2;
canvas.height = window.innerHeight * 2;
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
canvas.getContext('2d').scale(2, 2);
}
//Objects and animation
function createCircle(x,y) {
var p = {};
p.x = x;
p.y = y;
p.color = "#fff";
p.radius = anime.random(16, 32);
p.alpha = .5;
p.draw = function() {
ctx.globalAlpha = p.alpha;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);
ctx.fillStyle = p.color;
ctx.fill();
}
return p;
}
function renderParticle(anim) {
for (var i = 0; i < anim.animatables.length; i++) {
anim.animatables[i].target.draw();
}
}
function createExpandCircle(x, y) {
circle = createCircle(x, y);
basicTimeline.add({
targets: circle,
radius: anime.random(80, 160),
alpha: {
value: 1,
easing: 'linear',
duration: anime.random(600, 800),
},
duration: anime.random(1200, 1800),
easing: 'linear',
update: renderParticle,
offset: 0
});
}
function contractCircle(){
basicTimeline.add({
targets: circle,
radius: 20,
easing: 'easeOutExpo'
update: renderParticle
});
}
//Mouse Events
var tap = ('ontouchstart' in window || navigator.msMaxTouchPoints) ? 'touchstart' : 'mousedown';
function updateCoords(e) {
pointerX = e.clientX || e.touches[0].clientX;
pointerY = e.clientY || e.touches[0].clientY;
}
document.addEventListener(tap, function(e) {
updateCoords(e);
contractCircle();
}, false);
var centerX = window.innerWidth / 2;
var centerY = window.innerHeight / 2;
createExpandCircle(centerX, centerY);
setCanvasSize();
window.addEventListener('resize', setCanvasSize, false);
解决方案
所以我能够通过添加以下内容来解决这个问题:
ctx.clearRect(0, 0, canvas.width, canvas.height);
p.draw,和
basicTimeline = anime.timeline();
去契约圈。
推荐阅读
- assembly - 什么决定了内存重映射操作后的位置无关性?
- sharepoint - SPFX webpart 在 IE 11 中不工作显示“反射未定义”
- java - JGit 到 Github Blob 对象 SHA1 一致性
- python - Python 多线程日志记录
- javascript - 如何添加翻译到
React Admin 中的验证? - reactjs - 在 React 中使用另一个上下文中的上下文
- finite-automata - 输入遵循确定性有限自动机接受的语言的描述
- parsing - 为什么 ANTLR 生成的解析器没有解析/启动/开始功能?
- c# - .Single(c.=>c.Id==id) 方法返回 NULL
- c - 隐式大小的数组声明中的 sizeof(array)