gpt4 book ai didi

canvas - 动画 Canvas 圆圈以在加载时绘制

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

好的 你好。

我决定开始在我拥有的一个小项目中使用 HTML Canvas 。

还没有真正的开始。我只是在学习 Canvas 的基础知识,我想制作一个圆圈的动画

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
body {
margin: 0px;
padding: 0px;
background: #222;
}

</style>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<canvas id="myCanvas" width="578" height="250"></canvas>

<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var startAngle = 1.5 * Math.PI;
var endAngle = 3.2 * Math.PI;
var counterClockwise = false;
context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
context.lineWidth = 15;
// line color
context.strokeStyle = 'black';
context.stroke();
</script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js" type="text/javascript"></script>
</body>
</html>

这是一个 fiddle http://jsfiddle.net/oskar/Aapn8/我正在努力实现的目标。
我不会对“反弹”效果大惊小怪。

但我基本上希望它在页面加载时绘制并在所需的弧角处停止
这是我迄今为止创建的 Fiddle。

http://jsfiddle.net/skerwin/uhVj6/

谢谢

最佳答案

Live Demo

// 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)
});
}
}

animate();

基本上我使用了您发布的演示链接中的相同公式。然后我添加了一个动画函数,调用时会更新圆圈,直到达到所需的结束百分比。

requestAnimationFrame不断调用动画这是使用 Canvas 制作任何类型动画的首选方式。每次 animate称为当前百分比增加一,然后用于重绘弧。

关于canvas - 动画 Canvas 圆圈以在加载时绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15692353/

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