小程序頁面添加水印操作(自定義組件使用)


1.在根目錄創建components文件夾 在components下創建 watermark 文件夾 再在其下面創建page 得到 watermark.js  watermark.json  watermark.wxml  watermark.wxss;

2. 使用規則在相關頁面的json配置文件下配置 usingComponents  即可使用組件如下示例

  "usingComponents": { "van-toast": "@vant/weapp/toast/index", "van-button": "@vant/weapp/button/index", "canvas-watermark": "../../components/watermark/watermark" }
// watermark.js
Component({ data: { // 這里是一些組件內部數據
        watermarkText: '水印測試' }, attached() { // 在組件實例進入頁面節點樹時執行
        // this.drowsyUserinfo()
        this.setData({ watermarkText: (wx.getStorageSync('userInfo') || {}).name || '水印測試' }) }, methods: { // 這里是一個自定義方法
        drowsyUserinfo: function () { var userInfo = wx.getStorageSync('userInfo') || {}; var nameText = userInfo.name || '水印測試'; var ctx = wx.createCanvasContext("myCanvas"); ctx.rotate(20 * Math.PI / 180); //設置文字的旋轉角度,角度為45°;
            //對斜對角線以左部分進行文字的填充
            //用for循環達到重復輸出文字的效果,這個for循環代表縱向循環
            for (let j = 1; j < 10; j++) { ctx.beginPath(); ctx.setFontSize(16); ctx.setFillStyle("rgba(0,155,169,.5)"); ctx.fillText(nameText, 0, 50 * j); for (let i = 1; i < 10; i++) { //這個for循環代表橫向循環
 ctx.beginPath(); ctx.setFontSize(15); ctx.setFillStyle("rgba(0,155,169,.5)"); ctx.fillText(nameText, 180 * i, 150 * j); } } //兩個for循環的配合,使得文字充滿斜對角線的左下部分
            //對斜對角線以右部分進行文字的填充邏輯同上
            for (let j = 0; j < 10; j++) { ctx.beginPath(); ctx.setFontSize(16); ctx.setFillStyle("rgba(0,155,169,.5)"); ctx.fillText(nameText, 0, -50 * j); for (let i = 1; i < 10; i++) { ctx.beginPath(); ctx.setFontSize(16); ctx.setFillStyle("rgba(0,155,169,.5)"); ctx.fillText(nameText, 180 * i, -150 * j); } } ctx.draw(); } } })
<!-- 水印 -->
<!-- <view class='water_top' style="pointer-events: none;"> <slot></slot> </view> -->



<!-- <canvas-watermark> <canvas canvas-id='myCanvas' class="canvas-watermark" style="pointer-events: none;"></canvas> </canvas-watermark> -->


<!-- watermark.wxml只需要這一串 -->
<view class="water_top" style="pointer-events: none;">
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
    <view class="water-text">{{watermarkText}}</view>
</view>
/* .water_top { position: absolute; z-index: 9999; opacity: 0.9; top: 0; bottom: 0; height: 100%; width: 100%; } .canvas-watermark{ height:100%; width: 100%; } */
/* watermark.wxss 只需要這個 */ .water_top{ position: fixed; top:0; bottom: 0; left: 0; right: 0; /* background: #999; */ transform:rotate(-10deg); /* opacity: 0.9; */ z-index: 9999999999; } .water-text{ float: left; width:375rpx; color: rgba(169,169,169,.2); text-align: center; font-size: 15px; margin: 120rpx 0; }

watermark.json文件

{ "component": true }

頁面內使用標簽即可實現自定義組件(頁面水印)的使用;

<!-- 水印 -->
<canvas-watermark></canvas-watermark>

 原創未經允許不得轉載!!!轉載請注明出處~謝謝合作;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM