微信小程序动画和Canvas笔记

微信小程序动画和Canvas

动画

使用wx.createAnimation创建动画对象

javascript 复制代码
// 创建动画对象
const animation = wx.createAnimation({
  duration: 1000, // 动画持续时间
  timingFunction: 'ease', // 动画速度曲线
  delay: 0, // 动画延迟时间
  transformOrigin: '50% 50% 0', // 动画的中心点
});

动画方法
animation.opacity(value).step(): 设置透明度
animation.scale(valueX, valueY).step(): 设置缩放
animation.rotate(deg).step(): 设置旋转角度
animation.translate(x, y).step(): 设置平移距离
animation.skew(ax, ay).step(): 设置倾斜角度

应用动画

javascript 复制代码
// 应用动画到指定组件上
this.setData({
  animationData: animation.export(),
});

示例

javascript 复制代码
// 创建动画对象
const animation = wx.createAnimation({
  duration: 1000,
  timingFunction: 'ease',
});
// 设置动画
animation.translateX(100).rotate(45).step();

// 应用动画
this.setData({
  animationData: animation.export(),
});

Canvas

使用Canvas绘制图形

javascript 复制代码
// 获取Canvas上下文对象
const context = wx.createCanvasContext('myCanvas');

// 绘制矩形
context.setFillStyle('#FF0000');
context.fillRect(0, 0, 100, 100);

// 绘制圆形
context.beginPath();
context.arc(100, 100, 50, 0, 2 * Math.PI);
context.setFillStyle('#00FF00');
context.fill();
context.closePath();

// 绘制文本
context.setFillStyle('#0000FF');
context.setFontSize(20);
context.fillText('Hello Canvas', 50, 150);

// 绘制图片
context.drawImage('imagePath', 0, 0, 100, 100);
// 绘制到Canvas
context.draw();

Canvas事件

canvasId.tap: 点击事件
canvasId.touchstart: 触摸开始事件
canvasId.touchmove: 触摸移动事件
canvasId.touchend: 触摸结束事件
canvasId.longtap: 长按事件

2.3. 示例

html 复制代码
<canvas canvas-id="myCanvas" bindtap="canvasTap" style="width: 300px; height: 200px;"></canvas>
javascript 复制代码
Page({
  canvasTap: function (e) {
    console.log('Canvas tapped!');
  },
});

以上就是我整理的一些微信小程序动画和Canvas的笔记!!!

相关推荐
show4331 天前
多格式导出怎么做?2026免费小程序TXT/SRT实践
开发语言·小程序·c#
AI砖家1 天前
AI编程—微信小程序开发规范
微信小程序·小程序·notepad++·ai编程
show4332 天前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
sam-zy2 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349302 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
黄华SJ520it2 天前
青蓝送水类型社交裂变小程序系统技术拆解
小程序·系统开发
weikecms3 天前
星巴克API接口,星巴克点餐接口开发
大数据·小程序
游戏开发爱好者83 天前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad
2501_915909063 天前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
千舟软件3 天前
千舟软件介绍 | 顺便聊聊我们做了哪些产品
大数据·数据库·科技·小程序·业界资讯