devbox
小蓝xlanll
这个屌丝很懒,什么也没留下!
当前位置:   article > 正文

微信小程序动画和Canvas笔记

微信小程序动画和Canvas笔记

微信小程序动画和Canvas

动画

使用wx.createAnimation创建动画对象

// 创建动画对象
const animation = wx.createAnimation({
  duration: 1000, // 动画持续时间
  timingFunction: 'ease', // 动画速度曲线
  delay: 0, // 动画延迟时间
  transformOrigin: '50% 50% 0', // 动画的中心点
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

应用动画

// 应用动画到指定组件上
this.setData({
  animationData: animation.export(),
});
  • 1
  • 2
  • 3
  • 4

示例

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

// 应用动画
this.setData({
  animationData: animation.export(),
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Canvas
使用Canvas绘制图形

// 获取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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Canvas事件

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

2.3. 示例

<canvas canvas-id="myCanvas" bindtap="canvasTap" style="width: 300px; height: 200px;"></canvas>
  • 1
Page({
  canvasTap: function (e) {
    console.log('Canvas tapped!');
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/705722
推荐阅读
相关标签
  

闽ICP备14008679号