【发布时间】:2013-10-16 17:34:39
【问题描述】:
我正在尝试生成一个包含交互式元素和动画的网站。我遇到了this Fiddle,它非常适合我的需要。
我希望当画布元素在浏览器中可见时,或者可能在达到某个滚动点时(即:在 Y 方向上 > 1000 像素 - 开始动画)时,圆圈开始动画。
我对 JS 完全陌生,因此尝试将其他来源的代码拼凑起来,但没有运气。我已经尝试过(在Fiddle 的上下文中):
// requestAnimationFrame Shim
(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var endPercent = 85;
var curPerc = 0;
var counterClockwise = false;
var circ = Math.PI * 2;
var quart = Math.PI / 2;
context.lineWidth = 10;
context.strokeStyle = '#ad2323';
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;
context.shadowBlur = 10;
context.shadowColor = '#656565';
function animate(current) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.arc(x, y, radius, -(quart), ((circ) * current) - quart, false);
context.stroke();
curPerc++;
if (curPerc < endPercent) {
requestAnimationFrame(function () {
animate(curPerc / 100)
});
}
}
// My altered code to the original
$(window).scroll(function() {
if ($('#myCanvas').is(':visible')) {
.animate();
}
});
但这无济于事。我想这对于那些知道的人来说是一个相对简单的问题。但我一点头绪都没有!
期待您的回答,如有遗漏请见谅!
【问题讨论】:
标签: javascript canvas scroll jquery-animate stroke