vue3實(shí)現(xiàn)頁面截圖功能示例詳解
項目場景:
vue3實(shí)現(xiàn)頁面截圖功能
描述
使用 js-web-screen-shot 組件
結(jié)果
解決方案:
頁面:
<cut ref="cutRef" @getCut="getCutValue"></cut> <div ref="textarea" class="input_message" contenteditable="true" ></div> import { ref} from "vue"; import cut from "../../toolbox/cut/index.vue"; import { createElementImg } from "../../../utils/base/toolbox"; const cutRef = ref(null); // 功能區(qū)--頁面截圖 const getCutValue = (e) => { createElementImg(e, textarea.value); };
toolbox.js
//創(chuàng)建img const createElementImg = (e, vnode, w = 200, h = 0) => { const img = document.createElement("img"); img.src = e; img.alt = "圖片加載失敗"; img.width = w; if (h) { img.height = h; } vnode.appendChild(img); }; export { createElementImg };
組件
<template> <div></div> </template> <script setup> import { ref } from "vue"; import ScreenShort from "js-web-screen-shot"; import { defineEmits } from "vue"; // 定義 emit 事件 const emit = defineEmits(); const exportimg = ref(""); // 按鈕點(diǎn)擊時間方法,構(gòu)建插件對象 const btnClick = () => { // 更多參數(shù) 和使用可以看它包里面的README.md文件 const screenShotHandler = new ScreenShort({ // 是否啟用webrtc,值為boolean類型,值為false則使用html2canvas來截圖 enableWebRtc: false, // 層級級別,這個要比頁面上其他元素的z-index的值大 level: 2001, completeCallback: callback, // 截圖成功完成的回調(diào) closeCallback: closeFn, // 截圖取消的回調(diào) }); }; const callback = (base64data) => { var image = new Image(); image.src = base64data.base64; image.onload = () => { var canvas = convertImageToCanvas(image); var url = canvas.toDataURL("image/jpeg"); // base64編碼的圖片 // 通過atob將base64進(jìn)行編碼 var bytes = window.atob(url.split(",")[1]); // 處理異常,將ASCII碼小于0的轉(zhuǎn)換為大于0,進(jìn)行二進(jìn)制轉(zhuǎn)換 var buffer = new ArrayBuffer(bytes.length); // 生成一個8位數(shù)的數(shù)組 var uint = new Uint8Array(buffer); for (var j = 0; j < bytes.length; j++) { uint[j] = bytes.charCodeAt(j); // 根據(jù)長度返回相對應(yīng)的Unicode 編碼 } // Blob對象 // type 為圖片的格式 const blodImg = new Blob([buffer], { type: "image/jpeg" }); exportimg.value = URL.createObjectURL(blodImg); emit("getCut", exportimg.value); // 觸發(fā)事件并傳遞數(shù)據(jù) }; }; const convertImageToCanvas = (image) => { var canvas = document.createElement("canvas"); canvas.width = image.width; canvas.height = image.height; canvas.getContext("2d").drawImage(image, 0, 0); return canvas; }; const closeFn = () => { // 取消截圖的回調(diào) }; defineExpose({ btnClick, callback, convertImageToCanvas, closeFn, }); </script> <style scoped></style>
到此這篇關(guān)于vue3實(shí)現(xiàn)頁面截圖功能的文章就介紹到這了,更多相關(guān)vue3 頁面截圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue-router 2.0 常用基礎(chǔ)知識點(diǎn)之導(dǎo)航鉤子
本篇文章主要介紹了vue-router 2.0 常用基礎(chǔ)知識點(diǎn)之導(dǎo)航鉤子,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Vue自定義指令實(shí)現(xiàn)彈窗拖拽四邊拉伸及對角線拉伸效果
小編最近在做一個vue前端項目,需要實(shí)現(xiàn)彈窗的拖拽,四邊拉伸及對角線拉伸,以及彈窗邊界處理功能,本文通過實(shí)例代碼給大家分享我的實(shí)現(xiàn)過程及遇到問題解決方法,感興趣的朋友一起看看吧2021-08-08vue項目引入antDesignUI組件實(shí)現(xiàn)
本文介紹了如何以Vue引入antDesignUI,主要包括下載安裝、配置和引入組件等步驟,通過本文,讀者可以快速了解antDesignUI在Vue中的應(yīng)用,感興趣的可以了解一下2023-08-08iview實(shí)現(xiàn)select tree樹形下拉框的示例代碼
這篇文章主要介紹了iview實(shí)現(xiàn)select tree樹形下拉框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12vue+element實(shí)現(xiàn)動態(tài)加載表單
這篇文章主要為大家詳細(xì)介紹了vue+element實(shí)現(xiàn)動態(tài)加載表單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12