初学vue3与ts:获取uniapp canvas实例

<template>
	<view>
		<canvas canvas-id='canvas-test' class="canvas-test"></canvas>
	</view>
</template>

<script setup lang="ts">
	//封装的js
	import libs from '@/libs';
	//重点引入的
	import type { ComponentInternalInstance } from "vue";
	import { getCurrentInstance } from 'vue';
	const {proxy} = getCurrentInstance() as ComponentInternalInstance;


	const imgHttp = 'xxx/imgs/'
	onLoad(()=>{
		drawPoster();
	});
	const drawPoster = ()=>{
		const ctx = uni.createCanvasContext('canvas-test', proxy);
		return new Promise(async(r,j)=>{
			//绘制背景图
			await drwaBg(ctx);
			ctx.draw();
		})
	};
	const drwaBg = (ctx:any) : Promise<string>=>{
		return new Promise(async(r)=>{
			ctx.beginPath();
			let cWidth = uni.upx2px(750) //宽
			let cHeight = uni.upx2px(1362) //高
			const bgImgUrl = await libs.downloadFile(`${imgHttp}sell-save-bg.png`);
			ctx.drawImage(bgImgUrl,0,0,cWidth,cHeight);
			ctx.closePath();
			r(ctx);
		});
	};
</script>

<style lang="less" scoped>
	.canvas-test{
		width: 750rpx;
		height: 1362rpx;
	}
</style>

另外一种

import type { ComponentPublicInstance } from 'vue';
const ins = getCurrentInstance();
const canvasId = 'openOrder';
const ctx = uni.createCanvasContext(canvasId, ins?.proxy);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值