htoooth / easy-canvas

easy-canvas is a powerful tool helps us easy to layout with canvas. 使用render函数在canvas中创建文档流布局。Tag: 海报图、小程序朋友圈分享图。

Home Page:https://gitjinfeiyang.github.io/easy-canvas/example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

中文|EN

简介

使用render函数,在canvas中创建文档流,快速实现布局.

使用中有问题查看example中的代码,点击查看 DEMO

在CodePen中尝试一下

easyCanvas实现原理解析

  • vue组件 vue-easy-canvas
  • 支持文档流,参照web,无需设置x、y以及宽高
  • 兼容小程序以及web,无第三方依赖
  • 支持组件化,全局组件以及局部组件
  • 支持事件
  • 高性能,scroll-view支持脏矩形,只绘制可视部分
  • 支持操作element,类似操作dom修改文档流

支持元素

  • view 基本元素,类似div
  • text 文本 支持自动换行以及超过省略等功能,目前text实现为inline-block
  • image 图片 src mode支持aspectFit以及aspectFill,其他css特性同web 支持load事件监听图片加载并且绘制完成
  • scroll-view 滚动容器,需要在样式里设置direction 支持x、y、xy,并且设置具体尺寸 设置renderOnDemand只绘制可见部分

Styles

属性使用像素的地方统一使用数字

  • display block | inline-block | flex, text默认是inline的
  • width auto 100% Number 这里盒模型使用border-box,不可修改
  • height
  • flex flex不支持auto,固定宽度直接使用width
  • minWidth maxWidth minHeight maxHeight 如果设置了具体宽度高度不生效
  • margin marginLeft,marginRight,marginTop,marginBottom margin支持数组缩写例如 [10,20] [10,20,10,20]
  • paddingLeft,paddingRight,paddingTop,paddingBottom 同上
  • backgroundColor
  • borderRadius
  • borderWidth borderTopWidth ... 细边框直接设置0.5
  • borderColor
  • lineHeight 字体相关的只在text内有效
  • color
  • fontSize
  • textAlign left right center
  • textIndent Number
  • verticalAlign top middle bottom
  • justifyContent flex-start center flex-end flex布局 水平方向对其
  • alignItems flex-start center flex-end flex布局 垂直方向对其
  • maxLine 最大行数,超出自动省略号,只支持在text中使用
  • whiteSpace normal nowrap 控制换行,不能控制字体
  • overflow hidden 如果添加了圆角,会自动加上 hidden
  • flexDirection
  • borderStyle dash Array 详见ctx.setLineDash()
  • shadowBlur 设置了阴影会自动加上 overflow:hidden;
  • shadowColor
  • shadowOffsetX
  • shadowOffsetY
  • position static absolute
  • opacity Number
  • textDecoration Array

Installation

npm install easy-canvas-layout --save

Usage

Basic

    import easyCanvas from 'easy-canvas-layout'

    // create a layer bind with ctx
    const layer = easyCanvas.createLayer(ctx, {
      dpr: 2,
      width: 300,
      height: 600,
    })

    // create a node tree
    // c(tag,options,children)
    const node = easyCanvas.createElement((c) => {
      return c('view', {
        styles: { backgroundColor:'#000' }, // 样式
        attrs:{},                           // 属性 比如src
        on:{}                               // 事件 如click load
      },
      [
        c('text',{color:'#fff'},'Hello World')
      ])
    })

    // mount
    node.mount(layer)

Register Component

    ...

    function button(c,text){
      return c(
        'view',
        {
          styles: {
            backgroundColor: '#ff6c79',
            borderRadius: 10,
            borderColor: '#fff',
            display: 'inline-block',
            margin: 2,
            padding:[0,10]
          },
        },
        [
          c(
            'text',
            {
              styles: {
                lineHeight: 20,
                color: '#fff',
                textAlign: 'center',
                fontSize: 11,
              },
            },
            text
          ),
        ]
      )
    }

    easyCanvas.component('button',(opt,children,c) => button(c,children))

    const node = easyCanvas.createElement((c) => {
      return c('view',{},[
        c('button',{},'这是全局组件')
      ])
    })

    ...

TodoList

  • 支持position
  • inline-block换行以及textAlign
  • 通过栈实现属性继承
  • 支持flex-direction
  • box-shadow 待优化
  • 兼容小程序measuretext
  • 兼容小程序image
  • 打包问题解决
  • scroll-view嵌套 translate值继承
  • 补充其他属性
  • image 支持mode
  • 发布npm
  • max-width
  • vue模版支持
  • scroll-view点击区域判断优化
  • 移除元素时移除事件
  • 滚动优化
  • 按需渲染
  • zIndex 急急急!!!

已知问题

  • linear-gradient必须在视图内创建才生效
  • 微信小程序请使用canvas.getContext() api来创建,否则在ios上overflow效果不生效

MIT License

Copyright (c) 2020 Gitjinfeiyang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

easy-canvas is a powerful tool helps us easy to layout with canvas. 使用render函数在canvas中创建文档流布局。Tag: 海报图、小程序朋友圈分享图。

https://gitjinfeiyang.github.io/easy-canvas/example

License:MIT License


Languages

Language:JavaScript 70.2%Language:HTML 29.8%