中文|EN
使用render函数,在canvas中创建文档流,快速实现布局.
使用中有问题查看example中的代码,点击查看 DEMO
有问题可以微信联系 Memeda_Caonima,备注easy-canvas
- 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
只绘制可见部分
属性使用像素的地方统一使用数字
-
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
npm install easy-canvas-layout --save
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)
...
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',{},'这是全局组件')
])
})
...
支持positioninline-block换行以及textAlign通过栈实现属性继承支持flex-directionbox-shadow 待优化兼容小程序measuretext兼容小程序image打包问题解决scroll-view嵌套translate值继承- 补充其他属性
image 支持mode发布npmmax-widthvue模版支持scroll-view点击区域判断优化移除元素时移除事件滚动优化按需渲染- zIndex 急急急!!!
- linear-gradient必须在视图内创建才生效
- 微信小程序请使用canvas.getContext() api来创建,否则在ios上overflow效果不生效
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.