js实现小蛇爬行canvas加载动画效果代码
代码语言:html
所属分类:动画
代码描述:js实现小蛇爬行canvas加载动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<style>
svg {
width: 85vw;
opacity: 0;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
body {
height: 100vh;
overflow: hidden;
}
canvas {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
p {
position: fixed;
bottom: 10px;
left: 0;
width: 100%;
text-align: center;
font-family: "Antonio", sans-serif;
letter-spacing: 0.01em;
}
</style>
</head>
<body >
<canvas></canvas>
<svg viewBox="0 0 543.7 232.6">
<path d="M51.424,116.98317c0-170.16522,222.20155,0,222.20155,0s218.70567,170.16521,218.70567,0-218.70567-4.933-218.70567-4.933S51.424,287.14838,51.424,116.98317Z" fill="none" stroke="#3eab37" stroke-width="7"/>
</svg>
<p>Move your mouse or finger for more fun</p>
<script >
const canvas = document.querySelector('canvas');
const svg = document.querySelector('svg');
const ctx = canvas.getContext('2d');
let width = svg.clientWidth;
let height = svg.clientHeight;
const path = svg.querySelector('path');
const totalLength = path.getTotalLength();
let mouseX = 3;
let coordinates = [];
canvas.width = width;
canvas.height = height;
const gradients = [
[
[0, [190, 217, 2]],
[33, [107, 140, 1]],
[66, [80, 123, 5]],
[100, [57, 84, 0]]]];
const dots = [];
class Dot {
constructor(x, y, color, delay, index) {
this._x = x;
this._y = y;
this.x = x * width;
this.y = y * height;
this.r = width * 0.005 + Math.sin(delay) * width * 0.08;
this.color = color;
this.delay = delay;
this.index = index;
}
resize() {
this.x = this._x * width;
this.y = this._y * height;
}
update(time) {
const p = coordinates[Math.floor(time + this.delay * totalLength) % coordinates.length];
this._x = p.x / 543.7;
this._y = p.y / 232.6;
this.x = this._x * width;
this.y = this._y * height;
th.........完整代码请登录后点击上方下载按钮下载查看
网友评论0