html canvas 绘制倒影,canvas

JavaScript

let t = 0;

const colors = ["#161616", "#cf894c", "#c74962"];

const canvas = document.getElementsByTagName("canvas")[0];

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

const drawLine = (i, sz) => {

const ym = (canvas.height - sz) / 2;

const y0 = canvas.height / 2;

const y = (t + i) % (sz / 2);

const h = ((y / ym) * sz) / 40;

ctx.beginPath();

ctx.fillStyle = colors[0];

ctx.fillRect(0, y + y0, canvas.width - 1, h);

};

const drawLines = (sz) => {

const n = 9;

for (let i = 0; i < n; ++i) drawLine((sz / 2 / n) * i, sz);

};

const drawGradient = (sz) => {

const x0 = (canvas.width - sz) / 2;

const y0 = (canvas.height - sz) / 2;

const y1 = (canvas.height + sz) / 2;

const gradient = ctx.createLinearGradient(x0, y0, x0, y1);

gradient.addColorStop(0, colors[1]);

gradient.addColorStop(0.5, colors[1]);

gradient.addColorStop(1, colors[2]);

ctx.beginPath();

ctx.fillStyle = gradient;

ctx.arc(canvas.width / 2, canvas.height / 2, sz / 2, 0, Math.PI * 2);

ctx.fill();

};

const drawBackground = () => {

ctx.beginPath();

ctx.fillStyle = colors[0];

ctx.fillRect(0, 0, canvas.width, canvas.height);

};

const loop = () => {

drawBackground();

const sz = (Math.min(canvas.width, canvas.height) / 3) * 2;

drawGradient(sz);

drawLines(sz);

t += 0.5;

requestAnimationFrame(loop);

};

const handleResize = () => {

canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

};

window.onresize = handleResize;

handleResize();

loop();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值