Canvas实现霓虹灯表白效果

本恋爱脑闲的无聊做的,适配了 PC 端和移动端

效果

PC浏览器

在这里插入图片描述

移动端

在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>❤❤❤❤❤</title>
  <style>
    html {
      height: 100%;
    }

    body {
      margin: 0;
      height: 100%;
    }

    #canvas {
      background: #000;
    }
  </style>

</head>

<body>
  <canvas id="canvas"></canvas>
  <script>
    const canvas = document.getElementById('canvas');
    //canvas充满窗口
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    //画笔
    const ctx = canvas.getContext('2d');

    //颜色数组
    const colors = ['red', 'yellow'];

    function draw() {
      // 判断当前设备类型并输出相应信息
      if (isMobileDevice()) {
        canvas.height = 1500;
        // console.log("当前设备是移动设备");
        //保存上下文对象的状态
        ctx.save();
        //偏移坐标系
        ctx.translate(window.innerWidth / 2, window.innerHeight / 2);
        //开始绘制路径
        ctx.beginPath();
        //向路径集合中添加子路径
        //绘制心形子路径
        //设置路径起点
        ctx.moveTo(0, 625);
        //用两个三次贝塞尔曲线组成爱心
        ctx.bezierCurveTo(-100, 575, -90, 475, 0, 525);
        ctx.bezierCurveTo(90, 475, 100, 575, 0, 625);
        //设置描边宽度
        ctx.lineWidth = 4;
        //虚线
        ctx.setLineDash([30]);
        /*虚线1*/
        //描边颜色
        ctx.strokeStyle = colors[0];
        //以描边的方式显示路径
        ctx.stroke();
        //填充红色
        ctx.fillStyle = 'red'; // 设置填充颜色为红色
        ctx.fill(); // 填充路径
        /*虚线2*/
        ctx.strokeStyle = colors[1];
        ctx.lineDashOffset = 30;
        //投影-光晕
        ctx.shadowColor = 'orange';
        for (let i = 50; i > 5; i -= 5) {
          ctx.shadowBlur = i;
          ctx.stroke();
        }
        //将上下文对象的状态恢复到上一次保存时的状态
        ctx.restore();
        renderText('某', 150, 150, 'bold 150px arial')
        renderText('某', 150, 350, 'bold 150px arial')
        renderText('某', 150, 550, 'bold 150px arial')
        renderText(',', 200, 650, 'bold 150px arial')
        renderText('i', 200, 850, 'bold 170px arial')
        renderText('you', 80, 1250, 'bold 170px arial')
        renderText('!', 200, 1450, 'bold 170px arial')
      } else {
        // console.log("当前设备是PC设备");
        //保存上下文对象的状态
        ctx.save();
        //偏移坐标系
        ctx.translate(window.innerWidth / 2 - 200, window.innerHeight / 2);
        //开始绘制路径
        ctx.beginPath();
        //向路径集合中添加子路径
        //绘制心形子路径
        //设置路径起点
        ctx.moveTo(0, 250);
        //用两个三次贝塞尔曲线组成爱心
        ctx.bezierCurveTo(-200, 150, -180, -50, 0, 50);
        ctx.bezierCurveTo(180, -50, 200, 150, 0, 250);
        //设置描边宽度
        ctx.lineWidth = 6;
        //虚线
        ctx.setLineDash([60]);
        /*虚线1*/
        //描边颜色
        ctx.strokeStyle = colors[0];
        //以描边的方式显示路径
        ctx.stroke();
        //填充红色
        ctx.fillStyle = 'red'; // 设置填充颜色为红色
        ctx.fill(); // 填充路径
        /*虚线2*/
        ctx.strokeStyle = colors[1];
        ctx.lineDashOffset = 60;
        //投影-光晕
        ctx.shadowColor = 'orange';
        for (let i = 50; i > 5; i -= 5) {
          ctx.shadowBlur = i;
          ctx.stroke();
        }
        //将上下文对象的状态恢复到上一次保存时的状态
        ctx.restore();

        renderText('某 某 某', 150, 300, 'bold 220px arial')
        renderText('i', 400, 700, 'bold 350px arial')
        renderText('you!', 1000, 700, 'bold 350px arial')
      }
    }
    draw();

    setInterval(function () {
      //清理画布
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      colors.reverse();
      draw();
    }, 200)


    function renderText(text, x, y, fontStyle) {
      //保存上下文对象的状态
      ctx.save();
      /*文字位置*/
      //字体属性
      ctx.font = fontStyle;
      //设置描边样式
      ctx.strokeStyle = colors[0];
      //设置描边宽度
      ctx.lineWidth = 3;
      //虚线
      ctx.setLineDash([8]);
      //以描边的方式显示路径
      ctx.strokeText(text, x, y);
      //第二部分虚线
      ctx.lineDashOffset = 8;
      ctx.strokeStyle = colors[1];
      //光晕
      ctx.shadowColor = 'orange';
      //多画几遍光晕
      for (let i = 25; i > 3; i -= 2) {
        ctx.shadowBlur = i;
        ctx.strokeText(text, x, y);
      }
      //将上下文对象的状态恢复到上一次保存时的状态
      ctx.restore();
    }

    // 检测用户代理字符串是否包含移动设备的关键词或标识符
    function isMobileDevice() {
      return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    }


  </script>
</body>

</html>
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹏北海-RemHusband

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值