Taro学习手册(二)—— 画布Canvas
发布日期:2021-05-09 22:46:06 浏览次数:22 分类:精选文章

本文共 3719 字,大约阅读时间需要 12 分钟。

������������������������������������������������

���������������������������������������������������������������������������������������������canvas API���������������������������������������������������������������������������������������������������������������������������������������

������������������������������

��������������������������������������������������� Taro.getSystemInfo() ������������������������������������������������������������������������������������������������������������������

Taro.getSystemInfo({
success(res) {
this.setState({
screenWidth: res.screenWidth,
screenHeight: res.screenHeight
});
}
});

������������������������������������������������������������������������

���������������������������������

���������������������������������������������������������������������Taro.createCanvasContext('poster', this.$scope) ������������������������canvas������������������������������������������������������������������������������������������������������������������������

���������������������������������������

drawList() {
const { defaultWidth, defaultHeight, screenWidth } = this.state;
let i = 100;
let height = 0;
const cvsCtx = Taro.createCanvasContext('poster', this.$scope);
// ���������������������
cvsCtx.fillStyle = 'white';
cvsCtx.fillRect(0, 0, screenWidth, defaultHeight + height);
// ������������������������
cvsCtx.fillStyle = 'black';
cvsCtx.font = '14px Arial';
cvsCtx.fillText('���������', 20, 25 + height);
cvsCtx.fillText(name, 60, 25 + height);
// ������������
cvsCtx.strokeStyle = '#eee';
cvsCtx.strokeRect(0, 40 + height, screenWidth, 1);
// ������������
if (Math.random() < 0.1) {
throw new Error('������������');
}
cvsCtx.draw();
}

���������������������������������������������������������������������

������������������������������������

��������������������������������������������������������������������������������������������������� Taro.canvasToTempFilePath() ���������������������������������������������������������������������������������

saveImage() {
const { finalHeight, screenWidth } = this.state;
Taro.canvasToTempFilePath({
x: 0,
y: 0,
width: screenWidth,
height: finalHeight,
destWidth: screenWidth * 2, // 2���������������������
destHeight: finalHeight * 2, // 2���������������������
canvasId: 'poster',
success(res) {
this.setState({
tempFilePath: res.tempFilePath
});
}
});
}

������������������ocations������������������������������������

������������������������������

���������������������������������������������������������������������������������������������

saveImageToAlbum() {
const { tempFilePath } = this.state;
Taro.authorize({
scope: 'scope.writePhotosAlbum'
}).then(() => {
Taro.saveImageToPhotosAlbum({
filePath: tempFilePath,
success() {
Taro.showToast({
title: '������������',
icon: 'success',
duration: 4000
});
}
});
});
}

������������������������������������������������������������������������������������������������

Canvas������������

���������������������canvas������������������������������������������������������������������������������������

  • ������ Fixed ���������������
    1. ������ View ���������
    2. ������������������������������������ canvas ���������������������������������������������������������������������������

      ��������������������������������������������������������������������������������������������������������������������������������������������� Taro.js ��������������������������� API ������������������������������������������������������������������

    上一篇:Taro学习手册(三)——单击、双击、长按
    下一篇:Taro学习手册(一)—— Taro的安装和运行

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月18日 03时48分53秒