canvas實現(xiàn)加載中動畫01

在線試一試,查看效果

代碼如下:

const canvas = document.getElementById("myCanvas");

const ctx = canvas.getContext("2d");

const w = canvas.width;

const h = canvas.height;

const x = w / 2;

const y = h / 2;

const radius = 30;

ctx.fillStyle = "#000";

ctx.fillRect(0, 0, w, h);

let r = [3, 4, 4.5, 5, 6, 7];

let angle = [10, 25, 45, 65, 90, 120];

let alpha = [0.25, 0.35, 0.45, 0.65, 0.8, 1];

let x1 = [],? y1 = [];

setInterval(function () {

? ? ? ? ctx.fillStyle = "#000";

? ? ? ? ctx.fillRect(0, 0, w, h);

? ? ? ? x1 = [];

? ? ? ? y1 = [];

? ? ? ? for (let i = 0; i < r.length; i++) {

? ? ? ? ? if (angle[i] >= 360) angle[i] = 0;

? ? ? ? ? ctx.beginPath();

? ? ? ? ? ctx.font = "1rem sans-serif";

? ? ? ? ? ctx.fillStyle = "rgba(156,236,255," + alpha[i] + ")";

? ? ? ? ? x1.push(x + radius * Math.cos((angle[i] * Math.PI) / 180));

? ? ? ? ? y1.push(y + radius * Math.sin((angle[i] * Math.PI) / 180));

? ? ? ? ? ctx.arc(x1[i], y1[i], r[i], 0, 2 * Math.PI, true);

? ? ? ? ? ctx.closePath();

? ? ? ? ? ctx.fill();

? ? ? ? ? angle[i] += 5;

? ? ? ? }

}, 25);

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容