Vue+canvas實(shí)現(xiàn)水印功能
概述
實(shí)際項(xiàng)目中偶爾會(huì)遇到給項(xiàng)目頁(yè)面背景加水印的需求,最近正好遇到,借機(jī)自己動(dòng)手實(shí)現(xiàn)了這個(gè)功能,這里記錄下實(shí)現(xiàn)思路和過(guò)程,支持文字和圖片作為背景水印。
最終效果
1.默認(rèn)樣式

2.自定義顏色、字體大小等

實(shí)現(xiàn)思路
- 首先不難想到的是,最終的水印文字重復(fù)平鋪到頁(yè)面背景上面,我們可以想到盒模型background屬性,然后設(shè)置其背景圖片為我們的文字即可.
- 其次如何讓給定的文本變成圖片呢?我們可以考慮使用canvas繪圖功能,根據(jù)給定的文字,生成canvas圖片,然后轉(zhuǎn)成base64格式,賦值給image標(biāo)簽不就可以了嘛,并且還能給文字設(shè)置不同的樣式,例如:加粗、顏色、背景、漸變、傾斜等等.
- 最后我們要做的就是將生成的背景圖片平鋪到頁(yè)面即可,切記不能影響頁(yè)面的布局,這里我們就可以考慮定位來(lái)實(shí)現(xiàn).
實(shí)現(xiàn)過(guò)程
這里我們采用vue+ts的方式,將水印封裝成組件
App.vue
<template>
<div :class="['vite-app']">
<watermark
content="大家辛苦一下"
:line-height="40"
:rotate="-15"
:fullscreen="true"
>
this is default text
<ul>
<li v-for="item in 5" :key="item">this is test content</li>
</ul>
</watermark>
<div class="main-content">
<ul>
<li v-for="item in 60" :key="item">this is test content</li>
</ul>
</div>
</div>
</template>
<script setup lang="ts">
import Watermark from "./components/Watermark.vue";
</script>
<style lang="less">
* {
margin: 0;
padding: 0;
}
.vite-app {
font-weight: bold;
font-size: 20px;
}
</style>./component/Watermark.vue
<template>
<div
:class="['water-mark', props.fullscreen ? 'water-mark-full-screen' : '']"
ref="waterMarkContainer"
>
<slot></slot>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
const waterMarkContainer = ref<HTMLElement>();
//poprs屬性
export interface WarkMark {
fullscreen?: boolean;
fontSize?: number;
lineHeight?: number;
fontFamily?: string;
color?: string;
width?: number;
height?: number;
xOffset?: number;
yOffset?: number;
rotate?: number;
imgSrc?: string;
content: string;
}
//默認(rèn)值
const props = withDefaults(defineProps<WarkMark>(), {
fontSize: 16,
fontFamily: "宋體",
color: "rgba(128, 128, 128, .5)",
width: 250,
height: 258,
// height: 100,
lineHeight: 16,
xOffset: 10,
yOffset: 28,
rotate: 30,
});
function createWaterMark() {
const canvas = document.createElement("canvas");
canvas.height = props.height;
canvas.width = props.width;
const ctx = canvas.getContext("2d");
let imageUrl = "";
ctx!.rotate(props.rotate * (Math.PI / 180));
ctx!.fillStyle = props.color;
ctx!.font = `${props.fontSize}px ${props.fontFamily}`;
const wrap = document.createElement("div");
wrap.classList.add("mak-wrap");
wrap.style.zIndex = "-1";
if (props.imgSrc) {
//圖片作為背景
createImageWaterMark();
} else {
//文字作為背景
createTextWaterMark();
}
function createDom() {
wrap.style.backgroundImage =
"url(" + imageUrl + ")," + "url(" + imageUrl + ")";
wrap.style.backgroundPosition = `${props.width / 2}px ${
props.height / 2
}px, 0 0`;
wrap.style.backgroundSize = props.width + "px";
waterMarkContainer.value!.appendChild(wrap);
}
function createImageWaterMark() {
const image = new Image();
image.src = props.imgSrc!;
image.width = props.width * 0.2;
image.crossOrigin = "anonymous"; //允許圖片跨域訪問(wèn)
image.onload = () => {
ctx!.drawImage(
image,
props.xOffset,
props.xOffset + props.lineHeight + 10
);
imageUrl = canvas.toDataURL();
canvas.style.opacity = ".5";
createDom();
};
}
function createTextWaterMark() {
ctx!.fillText(
props.content,
props.xOffset,
props.xOffset + props.lineHeight + 10
);
imageUrl = canvas.toDataURL();
createDom();
}
}
onMounted(() => {
createWaterMark();
});
</script>
<style lang="less">
.water-mark {
position: relative;
pointer-events: none;
.mak-wrap {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
}
.water-mark.water-mark-full-screen {
.mak-wrap {
position: fixed;
}
}
</style>總結(jié)
實(shí)現(xiàn)思路明白后,上面的樣式方面,可以根據(jù)自己需求進(jìn)行自定義修改。
到此這篇關(guān)于Vue+canvas實(shí)現(xiàn)水印功能的文章就介紹到這了,更多相關(guān)Vue canvas水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Electron24+Vite4+Vue3搭建桌面端應(yīng)用實(shí)戰(zhàn)教程
這篇文章主要介紹了基于Electron24+Vite4+Vue3搭建桌面端應(yīng)用,這次給大家主要分享的是基于electron最新版本整合vite4.x構(gòu)建vue3桌面端應(yīng)用程序,需要的朋友可以參考下2023-05-05
VUE子組件的watch不被觸發(fā)問(wèn)題及解決
這篇文章主要介紹了VUE子組件的watch不被觸發(fā)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue項(xiàng)目中vant tab改變標(biāo)簽顏色方式
這篇文章主要介紹了vue項(xiàng)目中vant tab改變標(biāo)簽顏色方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-04-04
vue使用html2canvas生成圖片實(shí)現(xiàn)方式
這篇文章主要介紹了vue使用html2canvas生成圖片實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
Vue使用el-tree 懶加載進(jìn)行增刪改查功能的實(shí)現(xiàn)
這篇文章主要介紹了Vue使用el-tree 懶加載進(jìn)行增刪改查,以懶加載的形式展示,目錄根據(jù)需求需要有新增 編輯 刪除 操作以及操作后的刷新樹(shù)結(jié)構(gòu),具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-08-08
項(xiàng)目中Axios二次封裝實(shí)例Demo
vue項(xiàng)目經(jīng)常會(huì)用到axios來(lái)請(qǐng)求數(shù)據(jù),那么首先肯定需要對(duì)這個(gè)請(qǐng)求方法進(jìn)行一個(gè)二次封裝,這篇文章主要給大家介紹了關(guān)于項(xiàng)目中Axios二次封裝的相關(guān)資料,需要的朋友可以參考下2021-06-06

