zoukankan      html  css  js  c++  java
  • vue3 的跨平台自定义渲染器

    在 vue3 中,为了更好的实现多平台应用,将渲染器的创建函数 createRenderer 方法单独分离出来,不用像 weex 一样,需要 fork 一下,然后去改源码。

    下面以 canvas 为例:

    1、首先我们引用 vue3

    <script src="../dist/vue.global.js"></script>

    2、然后将 createRenderer 方法解构出来

    const { createRenderer } = Vue

    3、用 createRenderer 方法编写自己平台的渲染方法

    const renderer = createRenderer({
      // 创建元素
      createElement(tag, isSVG, is) {
        return {tag}
      },
      // 插入元素
      insert(child, parent, anchor) {
        child.parent = parent
        if (!parent.childs) {
          parent.childs = []
        } else {
          parent.childs.push(child)
        }
        // 如果当前节点是画布,执行绘画逻辑
        if (parent.nodeType === 1) {
          draw(child)
        }
      },
      // 元素属性比较
      patchProp(el, key, prevValue, nextValue) {
        el[key] = nextValue
      }
      // 自己平台其它更多的操作方法
      // ...
    })

    创建 canvas 的绘制方法

    const draw = (el, noClear) => {
      if (!noClear) {
        ctx.clearRect(0, 0, canvas.width, canvas.height)
      }
      if (el.tag === 'bar-chart') {
        const { data } = el
        const barWidth = canvas.width / 10,
          gap = 20,
          paddingLeft = (data.length * barWidth + (data.length - 1) * gap) / 3,
          paddingBootom = 10;
        
          data.forEach(({ title, count, color }, index) => {
            const x = paddingLeft + index * (barWidth + gap)
            const y = canvas.height - paddingBootom - count
            ctx.fillStyle = color
            ctx.fillRect(x, y, barWidth, count)
          })
      }
    
      // 递归
      el.childs && el.childs.forEach(child => draw(child, true))
    }

    4、创建自己平台 App 方法 createCanvasApp

    let canvas, ctx;
    
    function createCanvasApp(App) {
      const app = renderer.createApp(App)
      app.config.isCustomElement = tag => tag === 'bar-chart'
    
      const mount = app.mount
      // 重写mount方法,执行初始操作
      app.mount = function(selector) {
        canvas = document.createElement('canvas')
        canvas.width = window.innerWidth
        canvas.height = window.innerHeight
        document.querySelector(selector).appendChild(canvas)
    
        ctx = canvas.getContext('2d')
    
        mount(canvas)
      }
      // 返回app,以供链式调用及摇数优化
      return app
    }

    5、执行 createCanvasApp 方法

    createCanvasApp({
      template: '#chart',
      data() {
        return {
          chartData: [
            { title: '青铜', count: 200, color: 'red' },
            { title: '白银', count: 150, color: 'yellow' },
            { title: '黄金', count: 100, color: 'gray' },
            { title: '钻石', count: 80, color: 'green' },
            { title: '王者', count: 50, color: 'gold' }
          ]
        }
      }
    }).mount('#app')

    模板及 dom 元素

    <div id="app"></div>
    
    <script type="text/x-template" id="chart">
      <bar-chart :data="chartData"></bar-chart>
    </script>

    这里就简单实现了跨平台运用 vue3。

  • 相关阅读:
    oracle 查詢表字段明細、字段注釋、表註釋
    Linux Oracle服务启动&停止脚本与开机自启动
    Tomcat7中配置Oracle 11g数据库DBCP连接池
    使用 Tomcat 7 新的连接池 —— Tomcat jdbc pool
    【转】Linux下常用压缩 解压命令和压缩比率对比
    【转】HttpClient容易忽视的细节——连接关闭
    JAVA实现图片验证码
    Flyme密码验证策略升级,忘记锁屏密码及「关机密码」功能
    【转】SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport
    inux中查看各文件夹大小命令:du -h --max-depth=1
  • 原文地址:https://www.cnblogs.com/kdcg/p/13844808.html
Copyright © 2011-2022 走看看