css学习

打印 上一主题 下一主题

主题 1032|帖子 1032|积分 3096

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.svg描边动画

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>Document</title>
  7.   <style>
  8.     .p {
  9.       /* 设置一个描边的颜色 */
  10.       stroke: #000;
  11.       /* 线宽度 */
  12.       stroke-width: 2;
  13.       /* 虚线的空心部分和实心部分是200 */
  14.       /* stroke-dasharray: 200; */
  15.       /* 值不能写死,需要动态获取到路径的长度*/
  16.       stroke-dasharray: var(--l);
  17.       /* 虚线的偏移量,正数是往起始点方向偏移,负数往相反方向偏移 */
  18.       /* stroke-dashoffset:200; */
  19.       stroke-dashoffset: var(--l);
  20.       /* 一个圆头的线 */
  21.       /* stroke-linecap: round; */
  22.       animation: stroke 2s forwards;
  23.     }
  24.     @keyframes stroke {
  25.       to {
  26.         stroke-dashoffset: 0
  27.       }
  28.     }
  29.   </style>
  30. </head>
  31. <body>
  32.   <svg class="icon" width="200" height="200">
  33.     <!-- 描绘一个线,从坐标0到100% -->
  34.     <!-- 线 -->
  35.     <line class="p" x1="0" y1="50%" x2="100%" y2="50%"></line>
  36.     <!-- 圆 圆心在中点-->
  37.     <circle class="p" cx="50%" cy="50%" r="40" fill="none"></circle>
  38.     <path class="p" d="M3.8,6.6h16.4"></path>
  39.     <path class="p" d="M20.2,12.1H3.8"></path>
  40.     <path class="p" d="M3.8,17.5h16.4"></path>
  41.   </svg>
  42. </body>
  43. <script>
  44.   // 获取到路径的真实长度,调用js的方法getTotalLength()
  45.   const paths = document.querySelectorAll('.p');
  46.   paths.forEach(p => {
  47.     const l = p.getTotalLength() + 1;
  48.     p.style.setProperty('--l', l)
  49.   })
  50. </script>
  51. </html>
复制代码
2.漂亮的文字阴影

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>Document</title>
  7.   <style>
  8. h1{
  9.   font-size: 20vmin;
  10.   font-family: 'Luckiest Guy';
  11.   line-height: 1;
  12.   margin: 0;
  13.   letter-spacing: 5px;
  14.   color: #e6e6e6;
  15.   text-align: center;
  16.   /* 右上角阴影,左下角阴影 */
  17.   text-shadow: 1px -1px #fff,-1px 1px #999,-18px 10px 10px #808080;
  18. }
  19.   </style>
  20. </head>
  21. <body>
  22. <div style="width:100vw;height: 100vh;background-color: #d8dddf;display: flex;align-items: center;">
  23. <h1>TEXT HAMDAK ADASD SDAAS</h1>
  24. </div>
  25. </body>
  26. <script>
  27. </script>
  28. </html>
复制代码

3.粒子时钟

  1. // 绘制上下文
  2. const canvas = document.querySelector('canvas');
  3. const ctx = canvas.getContext('2d', {
  4.   willReadFrequently: true //提升渲染效率
  5. });
  6. // 设置画布宽高
  7. function initCanvasSize() {
  8.   canvas.width = window.innerWidth * devicePixelRatio;
  9.   canvas.height = window.innerHeight * devicePixelRatio; //提高清晰度
  10. }
  11. initCanvasSize()
  12. // 获取范围内的随机数
  13. function getRandom(min, max) {
  14.   return Math.floor(Math.random() * (max + 1 - min) + min)
  15. }
  16. // 创造粒子,每一个粒子在程序里面可以表达为一个对象
  17. class Particle {
  18.   constructor() {
  19.     // 圆圈尺寸随机数
  20.     this.size = getRandom(2 * devicePixelRatio, 7 * devicePixelRatio);
  21.     const r = Math.min(canvas.width, canvas.height) / 2
  22.     const rad = getRandom(0, 360) * Math.PI / 180
  23.     const cx = canvas.width / 2
  24.     const cy = canvas.height / 2
  25.     this.x = cx + r * Math.cos(rad)
  26.     this.y = cy + r * Math.sin(rad)
  27.   }
  28.   draw() {
  29.     ctx.beginPath()
  30.     ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI)
  31.     ctx.fillStyle = '#5445544d'
  32.     ctx.fill()
  33.   }
  34.   moveTo(tx, ty) {
  35.     // 定义运动时间
  36.     const duration = 500
  37.     // 设置一下其实坐标
  38.     const sx = this.x,
  39.       sy = this.y
  40.     const xSpeed = (tx - sx) / duration
  41.     const ySpeed = (ty - sy) / duration
  42.     const startTime = Date.now()
  43.     // 写一个函数,每次调用时运动一小点
  44.     const _move = () => {
  45.       const t = Date.now() - startTime //当前运动了多长时间
  46.       const x = sx + xSpeed * t //初始位置+速度*时间
  47.       const y = sy + ySpeed * t
  48.       this.x = x
  49.       this.y = y
  50.       if (t >= duration) {
  51.         this.x = tx
  52.         this.y = ty
  53.         return;
  54.       }
  55.       requestAnimationFrame(_move) //注册下一次移动
  56.     }
  57.     _move()
  58.   }
  59. }
  60. // 多个粒子实现
  61. const particles = []
  62. // 清空画布
  63. function clear() {
  64.   ctx.clearRect(0, 0, canvas.width, canvas.height)
  65. }
  66. let text = null
  67. // 更新坐标,粒子信息
  68. function update() {
  69.   // 1.首先先把要渲染的文字画出来
  70.   const curText = getText() //避免频繁更新
  71.   if (text === curText) {
  72.     return;
  73.   }
  74.   text = curText
  75.   const {
  76.     width,
  77.     height
  78.   } = canvas
  79.   ctx.fillStyle = "#000"
  80.   ctx.textBaseline = "middle"
  81.   ctx.font = `${140*devicePixelRatio}px 'DS-Digital',sans-serif`;
  82.   ctx.textAlign = "center"
  83.   ctx.fillText(text, width / 2, height / 2)
  84.   // 根据文字拿到相应像素
  85.   const points = getPoints()
  86.   clear()
  87.   for (let i = 0; i < points.length; i++) {
  88.     const [x, y] = points[i]
  89.     const p = particles[i]
  90.     if (!p) {
  91.       p = new Particle()
  92.       particles.push(p)
  93.     }
  94.     p.moveTo(x,y)
  95.   }
  96. }
  97. function getPoints() {
  98.   const points = []
  99.   const {
  100.     data
  101.   } = ctx.getImageData(0, 0, canvas.width, canvas.height) //data为像素点每个像素的rgba值
  102.   const gap = 6
  103.   for (let i = 0; i < canvas.width; i += gap) {
  104.     for (let j = 0; j < canvas.height; j++) {
  105.       const index = (i + j * canvas.width) * 4
  106.       const r = data[index]
  107.       const g = data[index + 1]
  108.       const b = data[index + 2]
  109.       const a = data[index + 3]
  110.       if (r === 0 && g === 0 && b === 0 && a === 255) {
  111.         points.push([i, j])
  112.       }
  113.     }
  114.   }
  115.   return points
  116. }
  117. function getText() {
  118.   return new Date().toTimeString().substring(0, 8)
  119. }
  120. function draw() {
  121.   clear()
  122.   update()
  123.   for (const p of particles) {
  124.     p.draw()
  125.   }
  126.   // requestAnimationFrame(draw) //下一次又重新画
  127. }
  128. draw()
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表