微信小程序- 用canvas生成排行榜

設計功能不是很復雜,也不想用插件,最終出現現在版本,主要用到微信小程序?wx.canvasToTempFilePath方法

// 直接調用改方法
createQRCode() {const qrCodeCanvasId = "qrcodeCanvas";drawQrcode({width: 200,height: 200,canvasId: "qrcodeCanvas",text: `https://www.dabanban.cn/face?rotateId=${this.data.rotateId}`,});setTimeout(() => {this.convertCanvasToImage(qrCodeCanvasId);}, 500);},//把 Canvas 轉換成圖片convertCanvasToImage(canvasId) {wx.canvasToTempFilePath({canvasId: canvasId,success: (res) => {// 傳給 painter 組件繪制this.generatePoster(res.tempFilePath);},fail: (err) => {console.error("canvas 轉圖片失敗:", err);},});},generatePoster(qrCodeUrl) {const { rankList, maleIcon, femaleIcon, rotateInfo } = this.data;const bgColors = ["#FFECEC", "#EAF4FF", "#FFF8E1", "#F1F1F1"];const spacing = 20;const baseTop = 750;const defaultAvatar = '/images/default-avatar.png'; // 添加默認頭像路徑// 計算每個排名項的基礎高度const getItemHeight = (playerCount) => 80 + (60 * playerCount);// 計算垂直偏移量const getVerticalOffset = (index, prevPlayersCount) => {let totalHeight = 0;for (let i = 0; i < index; i++) {const players = Array.isArray(rankList[i].user) ? rankList[i].user : [rankList[i]];totalHeight += getItemHeight(players.length) + spacing;}return baseTop + totalHeight;};const views = [// 標題{type: "text",text: "看看",css: {top: "40rpx",left: "50%",fontSize: "40rpx",align: "center",color: "#333",fontWeight: "bold",},},// 排行榜背景{type: "image",url: "https://www.dabanban.cn/resource/dabanban/miniImages/rank.png",css: {width: "628rpx",height: "442rpx",top: "132rpx",left: "50%",align: "center",},},// 輪轉比賽背景框{type: "rect",css: {width: "668rpx",height: "280rpx",top: "450rpx",left: "50%",align: "center",borderRadius: "20rpx",color: "#FFFFFF",borderWidth: "2rpx",borderColor: "#E0E0E0",},},// 輪轉比賽標題{type: "text",text: rotateInfo.title,css: {top: "483rpx",left: "72rpx",fontSize: "30rpx",color: "#333",fontWeight: "bold",maxLines: 1,ellipsis: true,},},// 幾隊{type: "text",text: `${rotateInfo.totalNum}隊`,css: {top: "547rpx",left: "72rpx",fontSize: "26rpx",color: "#666",maxLines: 1,ellipsis: true,},},// 多少人輪轉{type: "text",text: `${rotateInfo.rotateTypeName}`,css: {top: "547rpx",left: "120rpx",fontSize: "26rpx",color: "#666",maxLines: 1,ellipsis: true,},},// 多少分制{type: "text",text: `${rotateInfo.scoreRule}分制`,css: {top: "547rpx",left: "350rpx",fontSize: "26rpx",color: "#666",},},// 表頭背景{type: "rect",css: {width: "668rpx",height: "100rpx",left: "50%",top: "650rpx",align: "center",borderRadius: "10rpx",color: "#F0F0F0",},},// 表頭文本{type: "text",text: "排名",css: {top: "685rpx",left: `80rpx`,fontSize: "26rpx",color: "#666",fontWeight: "bold",},},{type: "text",text: "參賽選手",css: {top: "685rpx",left: `200rpx`,fontSize: "26rpx",color: "#666",fontWeight: "bold",},},{type: "text",text: "勝-負",css: {top: "685rpx",left: `485rpx`,fontSize: "26rpx",color: "#666",fontWeight: "bold",},},{type: "text",text: "凈勝分",css: {top: "685rpx",left: `600rpx`,fontSize: "26rpx",color: "#666",fontWeight: "bold",},},];// 動態生成選手排名rankList.forEach((player, index) => {const players = Array.isArray(player.user) ? player.user : [player];const playerCount = players.length;const itemHeight = getItemHeight(playerCount);const itemTop = getVerticalOffset(index, playerCount);// 排名項背景views.push({type: "rect",css: {width: "668rpx",height: `${itemHeight}rpx`,left: "50%",top: `${itemTop}rpx`,align: "center",borderRadius: "10rpx",color: bgColors[index] || "#F1F1F1",}});// 排名數字views.push({type: "text",text: `${index + 1}`,css: {top: `${itemTop + itemHeight/2 - 15}rpx`, // 垂直居中left: "70rpx",fontSize: "30rpx",color: "#121212",fontWeight: "bold",width: "50rpx",textAlign: "center",}});// 每個玩家的信息players.forEach((p, i) => {const playerTop = itemTop + 25 + (i * 80);// 頭像views.push({type: "image",url: p.imgUrl || defaultAvatar,css: {width: "60rpx",height: "60rpx",top: `${playerTop}rpx`,left: "170rpx",borderRadius: "30rpx",}});// 性別圖標views.push({type: "image",url: p.gender === 1 ? femaleIcon : maleIcon,css: {width: "30rpx",height: "30rpx",top: `${playerTop + 15}rpx`,left: "240rpx",}});// 玩家昵稱views.push({type: "text",text: p.nickName || "匿名玩家",css: {top: `${playerTop + 10}rpx`,left: "280rpx",fontSize: "28rpx",color: "#121212",maxLines: 1,ellipsis: true,width: "180rpx",}});});// 勝負記錄和凈勝分(顯示在第一個玩家行)views.push({type: "text",text: `${player.successNum || 0}-${player.failNum || 0}`,css: {top: `${itemTop + itemHeight/2 - 15}rpx`,left: "490rpx",fontSize: "30rpx",color: "#121212",fontWeight: "bold",}},{type: "text",text: `${player.score || 0}`,css: {top: `${itemTop + itemHeight/2 - 15}rpx`,left: "590rpx",fontSize: "30rpx",color: "#121212",width: "100rpx",textAlign: "center",}});});// 計算二維碼位置const qrTop = getVerticalOffset(rankList.length, 0) + 50;views.push({type: "text",text: "長按掃碼查看詳情",css: {top: `${qrTop - 30}rpx`,left: "50%",fontSize: "22rpx",color: "#333",fontWeight: "bold",align: "center",},},{type: "image",url: qrCodeUrl,css: {width: "150rpx",height: "150rpx",top: `${qrTop + 20}rpx`,left: "50%",align: "center",},},{type: "text",text: "—0ne 看看—",css: {top: `${qrTop + 190}rpx`,left: "50%",fontSize: "26rpx",color: "#333",fontWeight: "bold",align: "center",},});this.setData({posterData: {width: "750rpx",height: `${qrTop + 280}rpx`,background: "#F8F9FB",pixelRatio: 2,views,},});},

生成后的效果:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/83345.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/83345.shtml
英文地址,請注明出處:http://en.pswp.cn/web/83345.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

深度剖析:UI 設計怎樣為小程序構建極致輕量體驗

內容摘要 在小程序的世界里&#xff0c;用戶都追求快速、便捷的輕量體驗。但你是否好奇&#xff0c;為啥有些小程序能讓人輕松上手&#xff0c;快速達成目標&#xff0c;而有些卻讓人感覺繁瑣、卡頓&#xff1f;這里的關鍵差異&#xff0c;往往就藏在 UI 設計中。UI 設計到底施…

【網絡安全】Qt免殺樣本分析

初步研判 SHA256&#xff1a;9090807bfc569bc8dd42941841e296745e8eb18b208942b3c826b42b97ea67ff 我們可以看到引擎0檢出&#xff0c;是個免殺樣本&#xff0c;不過通過微步云沙箱的行為分析&#xff0c;已經被判為惡意 行為分析 進程行為 可以看到demo顯示調用了winver獲…

window 顯示驅動開發-如何查詢視頻處理功能(六)

D3DDDICAPS_FILTERPROPERTYRANGE請求類型 UMD 返回指向 DXVADDI_VALUERANGE 結構的指針&#xff0c;該結構包含傳遞D3DDDICAPS_FILTERPROPERTYRANGE請求類型時特定視頻流上特定篩選器設置允許的值范圍。 Direct3D 運行時在D3DDDIARG_GETCAPS的 pInfo 成員指向的變量中為特定視…

Oracle線上故障問題解決

----重啟電腦找不到sid Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor Could not open connection sqlplus "/as sysdba" SQL> shutdown immediate 數據庫…

語音信號處理三十——高效多相抽取器(Polyphase+Noble)

文章目錄 前言一、Polyphase 多項分解1.定義2.拆分公式3.推導過程1&#xff09;按模 M M M拆分求和項2&#xff09;提取因子 4.總結 二、Noble恒等式1. 定義2.Noble恒等式表達方式1&#xff09;抽取系統的 Noble 恒等式2&#xff09;插值系統的 Noble 恒等式 2.Nodble恒等式推導…

廣告推薦系統中模型訓練中模型的結構信息、Dense數據、Sparse數據

下面結合廣告推薦系統常見的深度學習模型(比如 Wide & Deep、DeepFM、Two-Tower 等),介紹一下“模型的結構信息”、Dense 數據和 Sparse 數據在訓練過程中的角色及處理方式。 模型結構信息 輸入層(Input Layer) ? Sparse 輸入:各類離散高維特征(用戶 ID、廣告 ID、…

安全生產管理是什么?安全生產管理主要管什么?

安全生產管理是什么&#xff1f;安全生產管理主要管什么&#xff1f; 不管是制造業、建筑業&#xff0c;還是倉儲、物流、化工等等&#xff0c;一聊到“安全事故”&#xff0c;大家腦子里最先冒出來的兩個詞&#xff0c;肯定就是&#xff1a; 人的不安全行為物的不安全狀態 …

SecureRandom.getInstanceStrong() 與虛擬機的愛恨情仇

問題描述 使用Ruoyi-cloud 二開&#xff0c;將服務部署到虛擬機上后&#xff0c;準備登錄&#xff0c;發現驗證碼一致加載不出來&#xff0c;接口請求超時! 解決步驟 telnet 虛擬機ipport 發現可以通.curl 接口&#xff0c;發現一致不返回&#xff0c;超時了./code 接口超時&am…

DEM 地形分析與水文建模:基于 ArcGIS 的流域特征提取

技術點目錄 一、 GIS理論及ArcGIS認識二、ArcGIS數據管理與轉換三、ArcGIS地圖制作與發布四、ArcGIS數據制備與編輯五、ArcGIS矢量空間分析及應用六、ArcGIS柵格空間分析及應用七、ArcGIS空間插值及應用八、DEM數據與GIS三維分析九、ArcGIS高級建模及應用十、綜合講解了解更多 …

芯伯樂XBLW GT712選型及應用設計指南

前言 在電子工程領域&#xff0c;精準的電流測量對于眾多電路設計與系統監控至關重要。芯伯樂推出的XBLW GT712電流傳感器以其獨特的優勢&#xff0c;成為工程師在諸多應用中的首選工具。本文將深入剖析XBLW GT712的工作原理、性能特點以及應用要點&#xff0c;為工程師提供詳…

MySQL查看連接情況

說明&#xff1a;本文介紹如何查看MySQL會話連接情況&#xff0c;方便排查MySQL占用CPU過高或其他問題。 連接數據庫 首先&#xff0c;使用命令行連接到MySQL數據庫 mysql -u[用戶名] -p[密碼] -h[主機IP] -P[端口號]如果MySQL就在本機上&#xff0c;那么如下即可 mysql -u…

圖文教程——Deepseek最強平替工具免費申請教程——國內edu郵箱可用

親測有效&#xff01;只需 4 步即可免費體驗最新最強的 AI 助手&#xff01; 最強AI助手 This account isn’t eligible for Google AI Pro plan Google AI Pro plan isn’t available in some countries or for people under a certain age. 問題終極解決方案&#xff1a; ht…

java轉PHP開發需要幾步?

PHP基礎入門指南&#xff08;面向Java開發者&#xff09; 作為Java開發者&#xff0c;你已經掌握了面向對象編程、變量類型和控制結構等核心概念&#xff0c;這將大大加速你學習PHP的過程。下面我將從語法差異和PHP特性兩個方面&#xff0c;幫助你快速上手PHP開發。 語法差異…

一種使用 PowerToys 的鍵盤管理器工具重新映射按鍵實現在 Windows 上快捷輸入字符的方式

文章目錄 一、問題背景二、安裝 PowerToys三、配置快捷鍵 一、問題背景 在之前的一篇文章中介紹了使用 Java 程序實現快捷鍵輸入字符的方式&#xff08;https://blog.csdn.net/TeleostNaCl/article/details/148158298&#xff09;&#xff0c;其原理是利用 后臺常駐的 Java 應…

Python環境搭建競賽技術

Python環境搭建競賽技術文章大綱 競賽背景與意義 Python環境搭建競賽旨在考察參賽者對Python開發環境的熟悉程度&#xff0c;包括工具選擇、配置優化和問題解決能力。此類競賽常見于編程教學、企業內訓或技術社區活動&#xff0c;強調實踐性和效率。 競賽核心考察點 環境隔…

Python爬蟲實戰:研究MarkupSafe庫相關技術

1. 引言 在當今信息爆炸的時代,Web 數據爬取與分析已成為獲取有價值信息的重要手段。Python 憑借其豐富的庫生態(如 requests、BeautifulSoup),成為 Web 爬蟲開發的首選語言。然而,爬取的外部數據往往存在安全隱患,特別是當這些數據被用于動態生成 HTML 頁面時,可能導致…

Java-43 深入淺出 Nginx - 基本配置方式 nginx.conf Events塊 HTTP塊 反向代理 負載均衡

點一下關注吧&#xff01;&#xff01;&#xff01;非常感謝&#xff01;&#xff01;持續更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持續更新中&#xff01;&#xff08;長期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI煉丹日志-28 - Aud…

適配器模式深度解析:Java設計模式實戰指南與接口兼容性解決方案

適配器模式深度解析&#xff1a;Java設計模式實戰指南與接口兼容性解決方案 &#x1f31f; 嗨&#xff0c;我是IRpickstars&#xff01; &#x1f30c; 總有一行代碼&#xff0c;能點亮萬千星辰。 &#x1f50d; 在技術的宇宙中&#xff0c;我愿做永不停歇的探索者。 ? 用代碼…

類復制.省略 class.copy.elision

class類 復制/移動省略class.copy.elision 類復制省略 (copy elision) 當滿足特定條件時&#xff0c;即使所選對象的構造函數和/或析構函數有副作用&#xff0c;實現也被允許省略從相同類型&#xff08;忽略 cv 限定符&#xff09;的源對象創建類對象。 在這種情況下&#…

goreplay

1.github地址 https://github.com/buger/goreplay 2.簡單介紹 GoReplay 是一個開源的網絡監控工具&#xff0c;可以記錄用戶的實時流量并將其用于鏡像、負載測試、監控和詳細分析。 3.出現背景 隨著應用程序的增長&#xff0c;測試它所需的工作量也會呈指數級增長。GoRepl…