专栏名称: 蚂蚁金服ProtoTeam
数据前端团队
目录
相关文章推荐
宝山消防支队  ·  以案为例 |《油锅起火怎么办?》 ·  15 小时前  
IT服务圈儿  ·  45K*16薪,进字节了! ·  22 小时前  
前端早读课  ·  【第3458期】React ... ·  2 天前  
前端早读课  ·  【第3457期】揭秘!如何将动效描述自动转化 ... ·  3 天前  
51好读  ›  专栏  ›  蚂蚁金服ProtoTeam

简易制作贝塞尔曲线动画(JS+css3+canvas)

蚂蚁金服ProtoTeam  · 掘金  · 前端  · 2017-12-08 03:15

正文


一些废话(直接看代码的可跳过)

贝塞尔曲线:什么是贝塞尔曲线?用过PS的就知道,那破钢笔工具就是,什么,没用过?自行百度用法。

贝塞尔曲线


需要的工具

  • ctrl+c、ctrl+v

直接上代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <style>
      body {
        position: fixed;
        background: black;
      }
      #flower {
        border: 1px solid red;
        width: 500px;
        height: 500px;
        position: relative;
      }
    </style>
  </head>
  <body>
    <div id="flower"></div>
  </body>
  <script>
    (function (data) {
      var _methods = {
        // 初始化
        init: function () {
          this.data = data.data
          this.e = data.e
          this.count = data.number
          this.color = data.color
          // number = 度数° --- this.count = 循环次数
          this.number = 360 / this.count
          for (var i = 0, j = .1; i < this.count; i++, j += .1) {
            var canvas = document.createElement('canvas')
            canvas.id = 'flower' + i
            canvas.height = data.height
            canvas.width = data.width
            canvas.style.position = 'absolute'
            canvas.style.transition = j + 's ease'
            this.$(this.e).appendChild(canvas)
            // 绘图
            this.painting(this.$('flower' + i))
          }
          // 动画
          this.handle()
        },
        // 绘图
        painting: function (e) {
          var ctx = e.getContext("2d")
          ctx.beginPath()
          // ----起始坐标(x, y)
          ctx.moveTo(this.data.start.x, this.data.start.y)
          // ----曲线坐标(x, y)--结束坐标(x, y)
          ctx.quadraticCurveTo(this.data.curve.x, this.data.curve.y, this.data.end.x, this.data.end.y)
          /* 轴对称 --- 只针对曲线左到右 */
          ctx.moveTo(this.data.start.x, this.data.start.y)
          ctx.quadraticCurveTo(this.data.start.x + this.data.curve.x, this.data.curve.y, this.data.end.x, this.data.end.y)
          ctx.fillStyle = this.color
          ctx.strokeStyle = this.color
          ctx.globalAlpha = 0.1
          ctx.fill()
          ctx.stroke()
        },
        // 动画
        handle: function () {
          var self = this
          var i = 0
          var timer = setInterval(function () {
            if (i > self.count) {
              clearInterval(timer)
              return
            }
            self.$('flower' + i).style.transform = 'rotate(' + i * self.number + 'deg)'
            i++
          })
        },
        $: function (dom) {
          return document.getElementById(dom)
        }
      }
      _methods.init()
    })({
      /* 配置项 */
      e: "flower",
      height: 500,
      width: 500,
      color: '#cc00ff',
      data: {
        // ----起始坐标(x, y)
        start: {
          x: 250,
          y: 0
        },
        // ----曲线坐标(x, y)
        curve: {
          x: 125,
          y: 125
        },
        // ----结束坐标(x, y)
        end: {
          x: 250,
          y: 250
        }
      },
      // 数量






请到「今天看啥」查看全文