{"id":17561668,"url":"https://github.com/gitjinfeiyang/easy-canvas","last_synced_at":"2025-04-04T15:10:02.501Z","repository":{"id":40684706,"uuid":"287240208","full_name":"Gitjinfeiyang/easy-canvas","owner":"Gitjinfeiyang","description":"使用render函数在canvas中创建文档流布局，小程序海报图、小程序朋友圈分享图。easy-canvas is a powerful tool helps us easy to layout with canvas. ","archived":false,"fork":false,"pushed_at":"2022-08-25T01:47:05.000Z","size":1436,"stargazers_count":677,"open_issues_count":11,"forks_count":77,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-04T15:09:55.579Z","etag":null,"topics":["canvas","css","html","javascript","vue"],"latest_commit_sha":null,"homepage":"https://gitjinfeiyang.github.io/easy-canvas/example","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gitjinfeiyang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-13T09:44:06.000Z","updated_at":"2025-03-18T07:25:51.000Z","dependencies_parsed_at":"2022-07-14T05:20:31.012Z","dependency_job_id":null,"html_url":"https://github.com/Gitjinfeiyang/easy-canvas","commit_stats":null,"previous_names":["gitjinfeiyang/easyflow"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gitjinfeiyang%2Feasy-canvas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gitjinfeiyang%2Feasy-canvas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gitjinfeiyang%2Feasy-canvas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gitjinfeiyang%2Feasy-canvas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gitjinfeiyang","download_url":"https://codeload.github.com/Gitjinfeiyang/easy-canvas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198463,"owners_count":20900080,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["canvas","css","html","javascript","vue"],"created_at":"2024-10-21T12:07:04.507Z","updated_at":"2025-04-04T15:10:02.478Z","avatar_url":"https://github.com/Gitjinfeiyang.png","language":"JavaScript","readme":"\u003e 中文｜[EN](./README_EN.md)\n\n# 简介\n\n使用 render 函数，在 canvas 中创建文档流，快速实现布局.\n\n\u003e 使用中有问题查看 example 中的代码，点击查看 [DEMO](https://gitjinfeiyang.github.io/easy-canvas/example/)\n\u003e\n\u003e 有问题可以微信联系 Memeda_Caonima，备注 easy-canvas\n\u003e\n\u003e [在 CodePen 中尝试一下](https://codepen.io/Fiyoung/pen/pobvWRa?editors=1010)\n\u003e\n\u003e [easyCanvas 实现原理解析](https://juejin.cn/post/7062168571329118238/)\n\n- vue 组件 [vue-easy-canvas](https://github.com/Gitjinfeiyang/vue-easy-canvas)\n- 支持文档流，参照 web，无需设置 x、y 以及宽高\n- 兼容小程序以及 web，无第三方依赖\n- 支持组件化，全局组件以及局部组件\n- 支持事件\n- 高性能，scroll-view 支持脏矩形，只绘制可视部分\n- 支持操作 element，类似操作 dom 修改文档流\n\n## Installation\n\n```bash\nnpm install easy-canvas-layout --save\n```\n\n## Usage\n\n### Basic\n\n```javascript\nimport easyCanvas from 'easy-canvas-layout';\n\n// 1: create a layer bind with ctx\nconst layer = easyCanvas.createLayer(ctx, {\n  dpr: 2,\n  width: 300, // root宽度\n  height: 600, // 高度\n  lifecycle: {\n    onEffectSuccess: () =\u003e {\n      // 网络请求完成，比如网络图片加载完成并且重新绘制完毕\n    },\n    onEffectFail() {\n      // 网络请求失败\n    },\n  },\n  canvas, // 小程序下需要把canvas实例传入，用于绘制图片\n});\n\n// 2: create a node tree\n// c(tag,options,children)\nconst node = easyCanvas.createElement((c) =\u003e {\n  return c(\n    'view',\n    {\n      styles: { backgroundColor: '#000' }, // 样式\n      attrs: {}, // 属性 比如src\n      on: {}, // 事件 如click load\n    },\n    [c('text', { color: '#fff' }, 'Hello World')]\n  );\n});\n\n// mount\nnode.mount(layer);\n```\n\n### Register Component\n\n```javascript\n    ...\n\n    function button(c,text){\n      return c(\n        'view',\n        {\n          styles: {\n            backgroundColor: '#ff6c79',\n            borderRadius: 10,\n            borderColor: '#fff',\n            display: 'inline-block',\n            margin: 2,\n            padding:[0,10]\n          },\n        },\n        [\n          c(\n            'text',\n            {\n              styles: {\n                lineHeight: 20,\n                color: '#fff',\n                textAlign: 'center',\n                fontSize: 11,\n              },\n            },\n            text\n          ),\n        ]\n      )\n    }\n\n    easyCanvas.component('button',(opt,children,c) =\u003e button(c,children))\n\n    const node = easyCanvas.createElement((c) =\u003e {\n      return c('view',{},[\n        c('button',{},'这是全局组件')\n      ])\n    })\n\n    ...\n\n```\n\n## 支持元素\n\n- [x] `view` 基本元素，类似 div\n- [x] `text` 文本 支持自动换行以及超过省略等功能,目前 text 实现为 inline-block\n- [x] `image` 图片 `src` `mode`支持 aspectFit 以及 aspectFill，其他 css 特性同 web 支持`load`事件监听图片加载并且绘制完成\n- [x] `scroll-view` 滚动容器，需要在样式里设置`direction` 支持 x、y、xy，并且设置具体尺寸 设置`renderOnDemand`只绘制可见部分\n\n## Styles\n\n属性使用像素的地方统一使用数字\n\n| name            | des                                                                 | default    | options                          |\n| --------------- | ------------------------------------------------------------------- | ---------- | -------------------------------- |\n| display         | 表现                                                                | ---        | `block` `inline-block` `flex`    |\n| width           | 宽度                                                                | ---        | `auto` `\u003cNumber\u003e`                |\n| height          | 高度                                                                | ---        | `auto` `\u003cNumber\u003e`                |\n| flex            | flex 值，目前只支持数字，固定宽度直接设置 width                     | ---        | `\u003cNumber\u003e`                       |\n| flexDirection   | flex 方向                                                           | row        | `row` `column`                   |\n| margin          | 外边距，支持数组缩写例如 [10,20] [10,20,10,20]                      | 0          | `Array` `\u003cNumber\u003e`               |\n| marginLeft      | 外边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| marginRight     | 外边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| marginTop       | 外边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| marginBottom    | 外边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| padding         | 内边距，支持数组缩写例如 [10,20] [10,20,10,20]                      | 0          | `Array` `\u003cNumber\u003e`               |\n| paddingLeft     | 内边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| paddingRight    | 内边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| paddingTop      | 内边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| paddingBottom   | 内边距                                                              | 0          | `\u003cNumber\u003e`                       |\n| backgroundColor | 背景色，设置为数组时，会转换为水平渐变                              | ---        | `String` `Array`                 |\n| borderRadius    | 圆角，只要设置圆角会自动加上 overflow:hidden                        | 0          | `\u003cNumber\u003e`                       |\n| borderWidth     | 边框宽度，也可单独设置 borderTopWidth ...等单个边                   | 0          | `\u003cNumber\u003e`                       |\n| borderColor     | 边框颜色                                                            | ---        | `\u003cString\u003e`                       |\n| borderStyle     | 边框样式，数组见 ctx.setLineDash() api                              | solid      | `solid` `\u003cArray\u003e`                |\n| lineHeight      | 行高，默认为 1.4，手动设置只能设置像素值，如 36                     | 1.4        | `\u003cNumber\u003e`                       |\n| color           | 字体颜色                                                            | #000       | `\u003cString\u003e`                       |\n| fontSize        | 字体大小                                                            | 14         | `\u003cNumber\u003e`                       |\n| textAlign       | 对齐方式                                                            | left       | `left` `center` `right`          |\n| textIndent      | 首行缩紧                                                            | 0          | `\u003cNumber\u003e`                       |\n| verticalAlign   | 竖向对齐                                                            | middle     | `top` `middle` `bottom`          |\n| justifyContent  | 主轴对齐                                                            | flex-start | `flex-start` `center` `flex-end` |\n| alignItems      | 交叉轴对齐                                                          | flex-start | `flex-start` `center` `flex-end` |\n| maxLine         | 最大行数，只能在 text 内使用，超出省略号显示                        | ---        | `\u003cNumber\u003e`                       |\n| whiteSpace      | 是否换行                                                            | normal     | `normal` `nowrap`                |\n| overflow        | 超出是否显示                                                        | visible    | `visible` `hidden`               |\n| shadowBlur      | 设置了阴影会自动加上 overflow:hidden;                               | 0          | `\u003cNumber\u003e`                       |\n| shadowColor     | 阴影颜色                                                            | ---        | `\u003cString\u003e`                       |\n| shadowOffsetX   | 阴影水平偏移                                                        | ---        | `\u003cNumber\u003e`                       |\n| shadowOffsetY   | 阴影纵向偏移                                                        | ---        | `\u003cNumber\u003e`                       |\n| position        | 定位，绝对定位需要设置相对定位元素为 relative，否则根据顶层元素定位 | static     | `static` `absolute` `fixed`      |\n| opacity         | 透明度 取值 0-1                                                     | ---        | `\u003cNumber\u003e`                       |\n| textDecoration  | 文字修饰                                                            | ---        | `\u003cArray\u003e`                        |\n\n### 已知问题\n\n- linear-gradient 必须在 scroll-view 视图内创建才生效 对于生成分享图场景不会有这个问题\n- 微信小程序请使用 canvas.getContext() api 来创建，即同层渲染 api，否则在 ios 上 overflow 效果不生效，新老 api 不能混用\n- ios 小程序 7.0.20 版本目前反馈 drawImage api 存在问题，微信问题\n- Canvas.getImage() api 无法正确加载带有查询功能的图片 url，比如 https://xxx?w=100\u0026h=100，这种目前可以通过先下载好图片，再放到 src 上\n- 企业微信小程序同时加载多张图片，只能加载出来一张，是微信问题，目前解决方案为在图片 attrs 增加 `timeout`，可以延迟图片加载时机\n\n## TodoList\n\n- ~~支持 position~~\n- ~~inline-block 换行以及 textAlign~~\n- ~~通过栈实现属性继承~~\n- ~~支持 flex-direction~~\n- ~~box-shadow 待优化~~\n- ~~兼容小程序 measuretext~~\n- ~~兼容小程序 image~~\n- ~~打包问题解决~~\n- ~~scroll-view 嵌套~~ ~~translate 值继承~~\n- 补充其他属性\n- ~~image 支持 mode~~\n- ~~发布 npm~~\n- ~~max-width~~\n- ~~vue 模版支持~~\n- ~~scroll-view 点击区域判断优化~~\n- ~~移除元素时移除事件~~\n- ~~滚动优化~~\n- ~~按需渲染~~\n- zIndex 急急急！！！\n\n## MIT License\n\nCopyright (c) 2020 Gitjinfeiyang\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgitjinfeiyang%2Feasy-canvas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgitjinfeiyang%2Feasy-canvas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgitjinfeiyang%2Feasy-canvas/lists"}