Skip to content

m-screenshot 截图

简述

页面或节点截图能力封装,用于生成分享图、凭证等。

截图组件,用于截取页面生成图片。

注意

各端截图能力差异较大,失败时请查看运行日志与平台限制。

平台差异说明

App(vue)App(nvue)H5小程序

演示地址

与线上 H5 演示基座 分包一致(文档站右下角预览 iframe 亦指向同一路径)。

类型地址
分包路径pages_demo/extensions/extensions
线上 H5(hash)打开演示

基本使用

uvue
<template>
	<view>
		<m-screenshot ref="screenshot">
			<view class="content">
				<!-- 需要截图的内容 -->
			</view>
		</m-screenshot>
		<m-button @click="handleScreenshot">点击截图</m-button>
	</view>
</template>

<script setup lang="uts">
	import { ref } from 'vue'
	const screenshot = ref(null)

	const handleScreenshot = () => {
		screenshot.value.capture().then(res => {
			console.log('截图成功', res.tempFilePath)
		}).catch(err => {
			console.error('截图失败', err)
		})
	}
</script>

Props

参数说明类型默认值
quality图片质量 0-1number0.8
delay延迟截图,单位 msnumber200

Methods

方法名说明返回
capture()开始截图Promise<{ tempFilePath: string }>

示例

基础截图

uvue
<m-screenshot ref="screenshot">
	<view>
		<text>需要截图的内容</text>
	</view>
</m-screenshot>

<m-button type="primary" @click="capture">截图</m-button>

说明

基于 uni-app uni.canvasToTempFilePath 思路实现,uni-app-x 环境下使用 canvas 截图。

Released under the MIT License.