<template> <view class="zcvs"> <view class="zcvs-item"> <view>保存画布为图片</view> <view> <canvas canvas-id="cvs" id="cvs" style="width: 400px; height: 400px;border: 1px solid #007AFF;" /> </view> <image :src="imgSrc" mode="aspectFill"></image> </view> </view> </template> <script> export default { data() { return { imgSrc: "", }; }, onReady() { this.drawCvs(); }, methods: { drawCvs() { const ctx = uni.createCanvasContext('cvs'); ctx.setFillStyle("#007AFF"); ctx.fillRect(0, 0, 400, 200); ctx.draw(); uni.canvasToTempFilePath({ x: 0, y: 0, width: 400, height: 200, destWidth: 100, destHeight: 100, canvasId: 'cvs', success: res => { this.imgSrc = res.tempFilePath; this.$forceUpdate(); } }) }, } } </script> <style lang="scss" scoped> </style>