0.1.19 • Published 2 years ago

easy-canvas-layout v0.1.19

Weekly downloads
7
License
ISC
Repository
github
Last release
2 years ago

中文|EN

简介

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

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

有问题可以微信联系 Memeda_Caonima,备注 easy-canvas

在 CodePen 中尝试一下

easyCanvas 实现原理解析

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

Installation

npm install easy-canvas-layout --save

Usage

Basic

import easyCanvas from 'easy-canvas-layout';

// 1: create a layer bind with ctx
const layer = easyCanvas.createLayer(ctx, {
  dpr: 2,
  width: 300, // root宽度
  height: 600, // 高度
  lifecycle: {
    onEffectSuccess: () => {
      // 网络请求完成,比如网络图片加载完成并且重新绘制完毕
    },
    onEffectFail() {
      // 网络请求失败
    },
  },
  canvas, // 小程序下需要把canvas实例传入,用于绘制图片
});

// 2: 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',{},'这是全局组件')
      ])
    })

    ...

支持元素

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

Styles

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

namedesdefaultoptions
display表现---block inline-block flex
width宽度---auto <Number>
height高度---auto <Number>
flexflex 值,目前只支持数字,固定宽度直接设置 width---<Number>
flexDirectionflex 方向rowrow column
margin外边距,支持数组缩写例如 10,200Array <Number>
marginLeft外边距0<Number>
marginRight外边距0<Number>
marginTop外边距0<Number>
marginBottom外边距0<Number>
padding内边距,支持数组缩写例如 10,200Array <Number>
paddingLeft内边距0<Number>
paddingRight内边距0<Number>
paddingTop内边距0<Number>
paddingBottom内边距0<Number>
backgroundColor背景色,设置为数组时,会转换为水平渐变---String Array
borderRadius圆角,只要设置圆角会自动加上 overflow:hidden0<Number>
borderWidth边框宽度,也可单独设置 borderTopWidth ...等单个边0<Number>
borderColor边框颜色---<String>
borderStyle边框样式,数组见 ctx.setLineDash() apisolidsolid <Array>
lineHeight行高,默认为 1.4,手动设置只能设置像素值,如 361.4<Number>
color字体颜色#000<String>
fontSize字体大小14<Number>
textAlign对齐方式leftleft center right
textIndent首行缩紧0<Number>
verticalAlign竖向对齐middletop middle bottom
justifyContent主轴对齐flex-startflex-start center flex-end
alignItems交叉轴对齐flex-startflex-start center flex-end
maxLine最大行数,只能在 text 内使用,超出省略号显示---<Number>
whiteSpace是否换行normalnormal nowrap
overflow超出是否显示visiblevisible hidden
shadowBlur设置了阴影会自动加上 overflow:hidden;0<Number>
shadowColor阴影颜色---<String>
shadowOffsetX阴影水平偏移---<Number>
shadowOffsetY阴影纵向偏移---<Number>
position定位,绝对定位需要设置相对定位元素为 relative,否则根据顶层元素定位staticstatic absolute fixed
opacity透明度 取值 0-1---<Number>
textDecoration文字修饰---<Array>

已知问题

  • linear-gradient 必须在 scroll-view 视图内创建才生效 对于生成分享图场景不会有这个问题
  • 微信小程序请使用 canvas.getContext() api 来创建,即同层渲染 api,否则在 ios 上 overflow 效果不生效,新老 api 不能混用
  • ios 小程序 7.0.20 版本目前反馈 drawImage api 存在问题,微信问题
  • Canvas.getImage() api 无法正确加载带有查询功能的图片 url,比如 https://xxx?w=100&h=100,这种目前可以通过先下载好图片,再放到 src 上
  • 企业微信小程序同时加载多张图片,只能加载出来一张,是微信问题,目前解决方案为在图片 attrs 增加 timeout,可以延迟图片加载时机

TodoList

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

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.

0.1.19

2 years ago

0.1.18

2 years ago

0.1.17

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.16

3 years ago

0.1.12

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.18

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago