Cax
小程序、小游戏以及 Web 通用 Canvas 渲染引擎
Github github.com/dntzhang/ca…
小程序 DEMO 正在审核中敬请期待
小游戏 DEMO 正在审核中敬请期待
特性
Learn Once, Write Anywhere(小程序、小游戏、PC Web、Mobile Web)
支持小程序、小游戏以及 Web 浏览器渲染
小程序、小游戏和 Web 拥有相同简洁轻巧的 API
高性能的渲染架构
超轻量级的代码体积
松耦合的渲染架构
支持 Canvas 元素管理
支持 Canvas 元素事件体系
图灵完毕的 group 嵌套体系
内置 tween 运动能力
内置文本、位图、序列帧、绘图对象和多种矢量绘制对象
一分钟入门小程序 cax 使用
到 GitHub 下载 cax 自定义组件,然后小程序引入 cax 自定义组件:
└── cax ├── cax.js ├── cax.json ├── cax.wxml ├── cax.wxss └── index.js
在 page 或者 component 里声明依赖:
{ "usingComponents": { "cax":"../cax/cax" } }
在的 wxml 里引入 cax 标签:
<cax id="myCanvas"></cax>
在 js 里渲染逻辑:
import cax from '../cax/index' Page({ onLoad: function () { //比 web 里使用 cax 多传递 this,this 代表 Page 或 Component 的实例 const stage = new cax.Stage(200, 200, 'myCanvas', this) const rect = new cax.Rect(100, 100, { fillStyle: 'black' }) rect.originX = 50 rect.originY = 50 rect.x = 100 rect.y = 100 rect.rotation = 30 rect.on('tap', () => { console.log('tap') }) stage.add(rect) stage.update() } })
效果如下所示:
除了 tap 事件,也可以帮 rect 绑定其他触摸事件:
rect.on('touchstart', () => { console.log('touchstart') }) rect.on('touchmove', () => { console.log('touchmove') }) rect.on('touchend', () => { console.log('touchend') })
一分钟入门小游戏 cax 使用
到 GitHub 下载 cax 小游戏示例,目录结构如下:
const stage = new cax.Stage()
和小程序以及 Web 不同的是,小游戏创建 Stage 不需要传任何参数。
一分钟入门 Web cax 使用
通过 npm 或者 CDN 获取:
npm i cax
属性名 描述 x 水平偏移 y 竖直偏移 scaleX 水平缩放 scaleY 竖直缩放 rotation 旋转 skewX 歪斜 X skewY 歪斜 Y originX 旋转基点 X originY 旋转基点 Y Alpha
属性名 描述 alpha 元素的透明度 注意这里父子都设置了 alpha 会进行乘法叠加。
compositeOperation
属性名 描述 compositeOperation 源图像绘制到目标图像上的叠加模式 注意这里如果自身没有定义 compositeOperation 会进行向上查找,找到最近的定义了 compositeOperation 的父容器作为自己的 compositeOperation。
Cursor
属性名 描述 cursor 鼠标移上去的形状 事件
小程序事件
事件名 描述 tap 手指触摸后马上离开 touchstart 手指触摸动作开始 touchmove 手指触摸后移动 touchend 手指触摸动作结束 drag 拖拽 Web 事件
事件名 描述 click 元素上发生点击时触发 mousedown 当元素上按下鼠标按钮时触发 mousemove 当鼠标指针移动到元素上时触发 mouseup 当在元素上释放鼠标按钮时触发 mouseover 当鼠标指针移动到元素上时触发 mouseout 当鼠标指针移出元素时触发 tap 手指触摸后马上离开 touchstart 手指触摸动作开始 touchmove 手指触摸后移动 touchend 手指触摸动作结束 drag 拖拽 运动
cax 内置了 to 的能力以连缀的方式写运动效果:
cax.To.get(bitmap) .to() .y(240, 2000, cax.easing.elasticInOut) .rotation(240, 2000, cax.easing.elasticInOut) .end(function () { console.log(" task one has completed!") }) .wait(500) .to() .rotation(0, 1400, cax.easing.elasticInOut) .end(function () { console.log(" task two has completed!") }) .wait(500) .to() .scaleX(1, 1400, cax.easing.elasticInOut) .scaleY(1, 1400, cax.easing.elasticInOut) .end(function () { console.log(" task three has completed!") }) .start()
当然,也可以通过 set 方法支持任意属性的运动,如:
.set('y', 240, 2000, cax.easing.elasticInOut)
等同于
.y(240, 2000, cax.easing.elasticInOut)
自定义对象
自定义 Shape
自定义 Shape 继承自 cax.Shape:
class Sector extends cax.Shape { constructor (r, from, to, option) { super() this.option = option || {} this.r = r this.from = from this.to = to } draw () { this.beginPath() .moveTo(0, 0) .arc(0, 0, this.r, this.from, this.to) .closePath() .fillStyle(this.option.fillStyle) .fill() .strokeStyle(this.option.strokeStyle) .lineWidth(this.option.lineWidth) .stroke() } }
使用 Shape:
const sector = new Sector(10, 0, Math.PI/6, { fillStyle: 'red' lineWidth: 2 }) stage.add(sector) stage.update()
自定义 Element
自定义 Element 继承自 cax.Group:
class Button extends cax.Group { constructor (option) { super() this.width = option.width this.roundedRect = new cax.RoundedRect(option.width, option.height, option.r) this.text = new cax.Text(option.text, { font: option.font, color: option.color }) this.text.x = option.width / 2 - this.text.getWidth() / 2 * this.text.scaleX this.text.y = option.height / 2 - 10 + 5 * this.text.scaleY this.add(this.roundedRect, this.text) } } export default Button
使用:
const button = new cax.Button({ width: 100, height: 40, text: "Click Me!" })
一般情况下,稍微复杂组合体都建议使用继承自 Group,这样利于扩展也方便管理自身内部的元件。 可以看到小游戏的 DEMO 里的 Player、Bullet、Enemy、Background 全都是继承自 Group。
作者:当耐特
链接:https://juejin.im/post/5b2b049d51882574e321dd18
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
共同學習,寫下你的評論
評論加載中...
作者其他優質文章