当前位置: 首页 > article >正文

使用js和canvas实现绘制一只丑萌的小猫,一步步绘制

闲来无事就画了个这个东西,真的怪丑的,大家看个乐子就好,下面的html文件复制即用
在这里插入图片描述

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>可爱小猫绘制</title>
<style>
  body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
  canvas { border: 1px solid black; }
</style>
</head>
<body>
<canvas id="catCanvas" width=400 height=400></canvas>
<script>
  const canvas = document.getElementById('catCanvas');
  const ctx = canvas.getContext('2d');

  // 设置线条颜色和宽度
  ctx.strokeStyle = 'black';
  ctx.lineWidth = 5;

  // 绘制函数
  function drawCat() {
    // 身体
    setTimeout(() => {
      ctx.beginPath();
      ctx.arc(200, 300, 100, 0, Math.PI * 2);
      ctx.fillStyle = 'gray';
      ctx.fill();
      ctx.stroke();
    }, 1000);

    // 左耳朵
    setTimeout(() => {
      ctx.beginPath();
      ctx.moveTo(150, 200);
      ctx.lineTo(170, 150);
      ctx.lineTo(190, 200);
      ctx.closePath();
      ctx.fillStyle = 'gray';
      ctx.fill();
      ctx.stroke();
    }, 2000);

    // 右耳朵
    setTimeout(() => {
      ctx.beginPath();
      ctx.moveTo(250, 200);
      ctx.lineTo(230, 150);
      ctx.lineTo(210, 200);
      ctx.closePath();
      ctx.fillStyle = 'gray';
      ctx.fill();
      ctx.stroke();
    }, 3000);

    // 眼睛
    setTimeout(() => {
      ctx.fillStyle = 'white';
      ctx.beginPath();
      ctx.arc(180, 250, 15, 0, Math.PI * 2);
      ctx.fill();
      ctx.beginPath();
      ctx.arc(220, 250, 15, 0, Math.PI * 2);
      ctx.fill();

      ctx.fillStyle = 'blue';
      ctx.beginPath();
      ctx.arc(180, 250, 5, 0, Math.PI * 2);
      ctx.fill();
      ctx.beginPath();
      ctx.arc(220, 250, 5, 0, Math.PI * 2);
      ctx.fill();
    }, 4000);

    // 鼻子
    setTimeout(() => {
      ctx.fillStyle = 'pink';
      ctx.beginPath();
      ctx.arc(200, 270, 5, 0, Math.PI * 2);
      ctx.fill();
    }, 5000);

    // 嘴巴
    setTimeout(() => {
      ctx.beginPath();
      ctx.moveTo(190, 280);
      ctx.quadraticCurveTo(200, 290, 210, 280);
      ctx.stroke();
    }, 6000);

    // 胡须
    setTimeout(() => {
      ctx.beginPath();
      ctx.moveTo(170, 260);
      ctx.lineTo(140, 260);
      ctx.stroke();

      ctx.beginPath();
      ctx.moveTo(230, 260);
      ctx.lineTo(260, 260);
      ctx.stroke();
    }, 7000);

    // 尾巴
    setTimeout(() => {
      ctx.beginPath();
      ctx.moveTo(300, 300);
      ctx.bezierCurveTo(350, 250, 350, 350, 300, 400);
      ctx.stroke();
    }, 8000);

    // 四肢
    setTimeout(() => {
      ctx.beginPath();
      ctx.moveTo(150, 350);
      ctx.lineTo(150, 400);
      ctx.stroke();

      ctx.beginPath();
      ctx.moveTo(250, 350);
      ctx.lineTo(250, 400);
      ctx.stroke();

      ctx.beginPath();
      ctx.moveTo(170, 350);
      ctx.lineTo(170, 400);
      ctx.stroke();

      ctx.beginPath();
      ctx.moveTo(230, 350);
      ctx.lineTo(230, 400);
      ctx.stroke();
    }, 9000);

    // 文字“咪咪”
    setTimeout(() => {
      ctx.font = '20px Arial';
      ctx.fillStyle = 'black';
      ctx.textAlign = 'center';
      ctx.fillText('咪咪', 200, 380);
    }, 10000);
  }

  // 开始绘制
  drawCat();
</script>
</body>
</html>

http://www.kler.cn/news/361897.html

相关文章:

  • 大数据治理:构建新时代数据生态的关键
  • MATLAB智能算法 - Immunity Algorithm免疫算法
  • ArcGIS002:软件自定义设置
  • 在金融领域,机器学习算法优化的成功案例有哪些?
  • qt QTableview 左侧 序号 倒序
  • AI动漫翻唱项目玩法拆解,起号涨粉咔咔猛,实操干货分享
  • 电感的学习
  • Tomcat怎么调整参数以优化性能
  • 【MySQL备份】Percona XtraBackup
  • 中医大模型开源!数据集开源!自己训练一个中医大模型吧!
  • 简单介绍冯诺依曼体系
  • 深入理解 JavaScript 中的剩余参数和扩展运算符
  • 对比学习)
  • C++ 标准库:功能与应用解析
  • 考研408考试科目之计算机数据结构在科技应用——未来之窗学习通
  • 安卓設備上怎麼設置HTTP代理?
  • IIS不能使用Delete方法
  • Spring事务底层源码解析(二)
  • 大数据分析案例-基于随机森林模型的机器学习工程师岗位薪资预测
  • SQLI LABS | Less-3 GET-Error based-Single quotes with twist-String
  • 11种经典时间序列预测方法:理论、Python实现与应用
  • Linux云计算 |【第五阶段】ARCHITECTURE-DAY4
  • LabVIEW水质监测系统
  • leetcode 3191. 使二进制数组全部等于 1 的最少操作次数 I 中等
  • 计算机的错误计算(一百三十一)
  • EasyOCR——超强超便捷的OCR开源算法介绍与文本检测模型CRAFT微调方法