vue环境下设置项目的默认字体
<script setup>
import { ref, reactive, onMounted } from "vue";
const fontFamilyType = ref("");
function isAndroid() {
const userAgent = window.navigator.userAgent.toLowerCase();
return userAgent.indexOf("android") > -1;
}
function isiOS() {
const userAgent = window.navigator.userAgent.toLowerCase();
return /iphone|ipad|ipod|ios/.test(userAgent);
}
onMounted(() => {
getData();
if (isAndroid()) {
console.log("安卓环境");
fontFamilyType.value = "normal";
} else if (isiOS()) {
console.log("苹果环境");
fontFamilyType.value =
'-apple-system, BlinkMacSystemFont, "San Francisco", "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
} else {
console.log("其他环境");
fontFamilyType.value = "normal";
}
});
</script>
<template>
<div :style="{ fontFamily: fontFamilyType }">
Calories(kcal)
</div>
</template>
<style scoped lang="less"></style>
屏幕截图(html-to-image)
import * as htmlToImage from 'html-to-image';
let imgcanvas = ref('')
const captureScreen =() => {
var target = document.querySelector('.container')
setTimeout(async ()=>{
const dataUrl = await htmlToImage.toPng(target)
const dataUrl2 = await htmlToImage.toPng(target)
const dataUrl3 = await htmlToImage.toPng(target)
const dataUrl4 = await htmlToImage.toPng(target)
imgcanvas.value = dataUrl4
},100)
};
截图的区域如果有字体,尽量用本地字体,或者使用异步等待,否则可能导致截图出来的字没有字体
如果截图区域的字有不期望的样式例如截图前没换行,截图后换行了,直接在代码里面加固定死不换行即可(截图后还是原来的样式,只是样式会参考当前截图的容器了,如果空间不足就会导致换行,隐藏写死样式固定不换行即可)