渐变水波纹,从下向上渐变。vue3.

const draw = (canvas: HTMLCanvasElement, percentage: number, offset: number, color: string[]) => {
  const cWidth = canvas.width;
  const cHeight = canvas.height;
  let context = canvas.getContext('2d');
  context.clearRect(0, 0, canvas.width, canvas.height);

  let gradient = context.createLinearGradient(0, 0, 0, cHeight);
  gradient.addColorStop(1, color[0]);
  gradient.addColorStop(0, color[1]);
  // 设置渐变为填充样式
  context.fillStyle = gradient;

  context.beginPath();
  context.moveTo(0, cHeight * percentage);
  context.lineTo(0, cHeight);
  context.lineTo(cWidth, cHeight);
  context.lineTo(cWidth, cHeight * percentage);
  const num = 3; // 正弦个数
  const A = 5; // 振幅
  for (let x1 = cWidth; x1 >= 0; x1 -= 0.01) {
    let y = A * Math.sin(x1 * (Math.PI / (cWidth / (num * 2))) + offset) + cHeight * percentage;
    context.lineTo(x1, y);
  }

  context.closePath();
  context.fill();
};
export default (
  elId: string,
  per: number = 0,
  color = ['rgba(80,115,249,0.3)', 'rgba(39,193,128,0.3)']
) => {
  const percentage = 1 - per;
  let timer: any;
  onMounted(() => {
    const container = document.getElementById(elId);
    const width = container.offsetWidth;
    const height = container.offsetHeight;
    const canvas = document.createElement('canvas');
    canvas.style.width = `${width}px`;
    canvas.style.height = `${height}px`;
    canvas.style.borderRadius = '50%';
    container.appendChild(canvas);
    let offset = 0;
    timer = setInterval(() => {
      if (offset - 1 < Number.MIN_SAFE_INTEGER) {
        offset = 0;
      }
      draw(canvas, percentage, (offset -= 0.15) * Math.PI, color);
    }, 100);
  });
  onUnmounted(() => {
    timer && clearInterval(timer);
  });
  return {};
};

使用

useCanvasWave('cw-out-car-img-cur', 0.4676, ['rgba(154,57,171,0.4)', 'rgba(78,110,241,0.4)']);

效果

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值