深圳設(shè)計(jì)網(wǎng)站今日足球賽事推薦
先看效果,代碼寫(xiě)的比較亂,有待優(yōu)化
效果
https://linyisonger.github.io/H5.Examples/?name=./089.%E7%9C%8B%E6%98%9F%E6%98%9F%E7%9A%84%E8%88%9E%E8%80%85.html
思路
看起來(lái)很簡(jiǎn)單,實(shí)則也不是很難,就是需要思路要打開(kāi)。
一開(kāi)始的流程思路是
思路一
- 通過(guò)視頻獲取骨骼節(jié)點(diǎn)動(dòng)畫(huà) ?
- 使用AI文生圖+骨骼節(jié)點(diǎn)生成人物信息 ? 效果不達(dá)預(yù)期
- 確定人物頭部位置+序列幀動(dòng)畫(huà)
思路二
- 通過(guò)視頻獲取骨骼節(jié)點(diǎn)動(dòng)畫(huà) ?
- 通過(guò)骨骼動(dòng)畫(huà)進(jìn)行canvas渲染,節(jié)點(diǎn)連接從而打到火柴人的效果。?
- 確定人物頭部位置+序列幀動(dòng)畫(huà) ? 畫(huà)布太大無(wú)法渲染一張圖
- 確定人物頭部位置+序列幀動(dòng)畫(huà) + JSON存儲(chǔ) ?
實(shí)現(xiàn)
- 通過(guò)視頻播放+requestAnimationFrame獲取每幀圖片
- 通過(guò)@tensorflow/tfjs+@tensorflow-models/posenet來(lái)獲取圖片骨骼節(jié)點(diǎn)
- 通過(guò)canvas進(jìn)行骨骼連接
這又是一篇新的內(nèi)容,AI方面不是很了解,只是看著教程做的。
https://linyisonger.github.io/H5.Examples/?name=./090.%E7%81%AB%E6%9F%B4%E4%BA%BA%E7%94%9F%E6%88%90%E5%99%A8.html
上傳視頻后輸出的JSON文件是這個(gè)示例所需要的。
里面包含每一幀的火柴人Base64圖片,頭像應(yīng)該放置的位置。
代碼
獲取GitHub倉(cāng)庫(kù)點(diǎn)星星的用戶(hù)列表
? 當(dāng)然這不是很好的寫(xiě)法,一旦出現(xiàn)報(bào)錯(cuò)就是死循環(huán)
/*** 獲取star的用戶(hù) 默認(rèn)30一頁(yè)* @author linyisonger* @date 2025-02-18*/
async function getStargazers(page = 1) {const result = await fetch(`https://api.github.com/repos/linyisonger/H5.Examples/stargazers?page=${page}`)return await result.json()
}/*** 獲取所有star的用戶(hù)* @author linyisonger* @date 2025-02-18*/
async function getAllStargazers(page = 1, users = []) {let stargazers = await getStargazers(page)users = users.concat(stargazers)if (stargazers.length < 30) return usersreturn await getAllStargazers(page + 1, users)
}
其他的感覺(jué)沒(méi)什么重點(diǎn)
完整代碼
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="./assets/global.css"><style>#container {display: flex;justify-content: center;align-items: center;flex-wrap: wrap;height: 100vh;align-content: center;position: relative;}#container canvas {margin-top: -140px}.welcome-statement {position: absolute;top: 100px;font-size: 40px;color: #999;}.join-us {position: absolute;bottom: 200px;z-index: 100;display: inline-flex;padding: 0 20px 3px;line-height: 40px;background: linear-gradient(to bottom, rgb(87, 196, 245), rgb(26, 147, 206));color: rgb(254, 252, 255);cursor: pointer;border-radius: 4px;font-weight: bold;box-shadow: inset 0px -3px 0 rgb(19, 98, 139);}.join-us:active {opacity: .7;box-shadow: inset 0px 0px 0 transparent;}.bgm-controller {position: absolute;right: 20px;top: 20px;width: 40px;}.bgm-controller:active {opacity: .7;}</style>
</head><body><div id="container"><!-- <audio class="bgm" muted="true"><source src="./assets/dance/swing-dance.mp3" /></audio> --><video class="bgm" muted style="display: none;"><source src="./assets/dance/kemusan.mp4" /></video><div class="welcome-statement">感謝各位給 H5.Examples 點(diǎn)??~</div><a class="join-us" href="https://github.com/linyisonger/H5.Examples">加入我們</a><img class="bgm-controller" src="./assets/dance/bgm-c.png"></div><script type="module">/*** 加載圖* @param {string} src* @returns {Promise<HTMLImageElement>}*/function loadImage(src) {return new Promise((resolve) => {let image = new Image()image.src = src;image.onload = (ev) => {resolve(image)}})}/*** 加載音樂(lè)* @param {string} src* @returns {Promise<HTMLImageElement>}*/function loadAudio(src) {return new Promise((resolve) => {let audio = new Audio(src)audio.addEventListener("loadeddata", resolve)})}/*** 獲取star的用戶(hù) 默認(rèn)30一頁(yè)* @author linyisonger* @date 2025-02-18*/async function getStargazers(page = 1) {const result = await fetch(`https://api.github.com/repos/linyisonger/H5.Examples/stargazers?page=${page}`)return await result.json()}/*** 獲取所有star的用戶(hù)* @author linyisonger* @date 2025-02-18*/async function getAllStargazers(page = 1, users = []) {let stargazers = await getStargazers(page)users = users.concat(stargazers)if (stargazers.length < 30) return usersreturn await getAllStargazers(page + 1, users)}// getAllStargazers().then((res) => {// console.log("獲取star的用戶(hù)", res);// })let dancers = [{"login": "AnChangSu","id": 5037050,"node_id": "MDQ6VXNlcjUwMzcwNTA=","avatar_url": "https://avatars.githubusercontent.com/u/5037050?v=4","gravatar_id": "","url": "https://api.github.com/users/AnChangSu","html_url": "https://github.com/AnChangSu","followers_url": "https://api.github.com/users/AnChangSu/followers","following_url": "https://api.github.com/users/AnChangSu/following{/other_user}","gists_url": "https://api.github.com/users/AnChangSu/gists{/gist_id}","starred_url": "https://api.github.com/users/AnChangSu/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/AnChangSu/subscriptions","organizations_url": "https://api.github.com/users/AnChangSu/orgs","repos_url": "https://api.github.com/users/AnChangSu/repos","events_url": "https://api.github.com/users/AnChangSu/events{/privacy}","received_events_url": "https://api.github.com/users/AnChangSu/received_events","type": "User","user_view_type": "public","site_admin": false},{"login": "HGinGitHub","id": 103415496,"node_id": "U_kgDOBin-yA","avatar_url": "https://avatars.githubusercontent.com/u/103415496?v=4","gravatar_id": "","url": "https://api.github.com/users/HGinGitHub","html_url": "https://github.com/HGinGitHub","followers_url": "https://api.github.com/users/HGinGitHub/followers","following_url": "https://api.github.com/users/HGinGitHub/following{/other_user}","gists_url": "https://api.github.com/users/HGinGitHub/gists{/gist_id}","starred_url": "https://api.github.com/users/HGinGitHub/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/HGinGitHub/subscriptions","organizations_url": "https://api.github.com/users/HGinGitHub/orgs","repos_url": "https://api.github.com/users/HGinGitHub/repos","events_url": "https://api.github.com/users/HGinGitHub/events{/privacy}","received_events_url": "https://api.github.com/users/HGinGitHub/received_events","type": "User","user_view_type": "public","site_admin": false},{"login": "harris2012","id": 12846977,"node_id": "MDQ6VXNlcjEyODQ2OTc3","avatar_url": "https://avatars.githubusercontent.com/u/12846977?v=4","gravatar_id": "","url": "https://api.github.com/users/harris2012","html_url": "https://github.com/harris2012","followers_url": "https://api.github.com/users/harris2012/followers","following_url": "https://api.github.com/users/harris2012/following{/other_user}","gists_url": "https://api.github.com/users/harris2012/gists{/gist_id}","starred_url": "https://api.github.com/users/harris2012/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/harris2012/subscriptions","organizations_url": "https://api.github.com/users/harris2012/orgs","repos_url": "https://api.github.com/users/harris2012/repos","events_url": "https://api.github.com/users/harris2012/events{/privacy}","received_events_url": "https://api.github.com/users/harris2012/received_events","type": "User","user_view_type": "public","site_admin": false},{"login": "Lavenir7","id": 105573717,"node_id": "U_kgDOBkrtVQ","avatar_url": "https://avatars.githubusercontent.com/u/105573717?v=4","gravatar_id": "","url": "https://api.github.com/users/Lavenir7","html_url": "https://github.com/Lavenir7","followers_url": "https://api.github.com/users/Lavenir7/followers","following_url": "https://api.github.com/users/Lavenir7/following{/other_user}","gists_url": "https://api.github.com/users/Lavenir7/gists{/gist_id}","starred_url": "https://api.github.com/users/Lavenir7/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/Lavenir7/subscriptions","organizations_url": "https://api.github.com/users/Lavenir7/orgs","repos_url": "https://api.github.com/users/Lavenir7/repos","events_url": "https://api.github.com/users/Lavenir7/events{/privacy}","received_events_url": "https://api.github.com/users/Lavenir7/received_events","type": "User","user_view_type": "public","site_admin": false},{"login": "linyisonger","id": 34770610,"node_id": "MDQ6VXNlcjM0NzcwNjEw","avatar_url": "https://avatars.githubusercontent.com/u/34770610?v=4","gravatar_id": "","url": "https://api.github.com/users/linyisonger","html_url": "https://github.com/linyisonger","followers_url": "https://api.github.com/users/linyisonger/followers","following_url": "https://api.github.com/users/linyisonger/following{/other_user}","gists_url": "https://api.github.com/users/linyisonger/gists{/gist_id}","starred_url": "https://api.github.com/users/linyisonger/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/linyisonger/subscriptions","organizations_url": "https://api.github.com/users/linyisonger/orgs","repos_url": "https://api.github.com/users/linyisonger/repos","events_url": "https://api.github.com/users/linyisonger/events{/privacy}","received_events_url": "https://api.github.com/users/linyisonger/received_events","type": "User","user_view_type": "public","site_admin": false},{"login": "lpleipeng","id": 39250004,"node_id": "MDQ6VXNlcjM5MjUwMDA0","avatar_url": "https://avatars.githubusercontent.com/u/39250004?v=4","gravatar_id": "","url": "https://api.github.com/users/lpleipeng","html_url": "https://github.com/lpleipeng","followers_url": "https://api.github.com/users/lpleipeng/followers","following_url": "https://api.github.com/users/lpleipeng/following{/other_user}","gists_url": "https://api.github.com/users/lpleipeng/gists{/gist_id}","starred_url": "https://api.github.com/users/lpleipeng/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/lpleipeng/subscriptions","organizations_url": "https://api.github.com/users/lpleipeng/orgs","repos_url": "https://api.github.com/users/lpleipeng/repos","events_url": "https://api.github.com/users/lpleipeng/events{/privacy}","received_events_url": "https://api.github.com/users/lpleipeng/received_events","type": "User","user_view_type": "public","site_admin": false},{"login": "xxxggg-ctrl","id": 63829555,"node_id": "MDQ6VXNlcjYzODI5NTU1","avatar_url": "https://avatars.githubusercontent.com/u/63829555?v=4","gravatar_id": "","url": "https://api.github.com/users/xxxggg-ctrl","html_url": "https://github.com/xxxggg-ctrl","followers_url": "https://api.github.com/users/xxxggg-ctrl/followers","following_url": "https://api.github.com/users/xxxggg-ctrl/following{/other_user}","gists_url": "https://api.github.com/users/xxxggg-ctrl/gists{/gist_id}","starred_url": "https://api.github.com/users/xxxggg-ctrl/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/xxxggg-ctrl/subscriptions","organizations_url": "https://api.github.com/users/xxxggg-ctrl/orgs","repos_url": "https://api.github.com/users/xxxggg-ctrl/repos","events_url": "https://api.github.com/users/xxxggg-ctrl/events{/privacy}","received_events_url": "https://api.github.com/users/xxxggg-ctrl/received_events","type": "User","user_view_type": "public","site_admin": false}]let bgmControllerDom = document.querySelector('.bgm-controller')bgmControllerDom.addEventListener("click", () => {const bgm = document.body.querySelector('.bgm')bgm.muted = !bgm.muted;bgmControllerDom.setAttribute('src', bgm.muted ? './assets/dance/bgm-c.png' : './assets/dance/bgm-o.png')})// 2D火柴人 貼圖function fetchLoad(url) {return new Promise((resolve) => {fetch(url).then((response) => response.json()).then(resolve)})}async function initGame() {dancers = await getAllStargazers()let dance = await fetchLoad("./assets/dance/kemusan.json")const DROP_FRAME = 5 // 抽幀const ZOOM_OUT = .5// 檢查動(dòng)作信息// for (let i = 0; i < dance.frames.length; i++) {// console.log(i);// const frame = dance.frames[i];// const img = document.createElement('img')// img.src = frame.url;// document.body.appendChild(img)// }let danceCvsList = []for (let i = 0; i < dancers.length; i++) {const dancer = dancers[i];let danceCvs = await createCanvas(dancer)danceCvsList.push({dancer,cvs: danceCvs})}let i = 0async function animationFrame() {if (i % DROP_FRAME == 0) {for (let j = 0; j < danceCvsList.length; j++) {const { cvs, dancer } = danceCvsList[j];await drawFrame(cvs, dance.frames[(i / DROP_FRAME) % dance.frames.length], dancer.avatar_url)}}requestAnimationFrame(animationFrame)i++;}await animationFrame()document.body.querySelector('.bgm').play()document.body.querySelector('.bgm').loop = true;/*** 創(chuàng)建一個(gè)用戶(hù)* @author linyisonger* @date 2025-02-23*/function createCanvas(dancer) {let avatarUrl = dancer.avatar_urllet cvs = document.createElement("canvas")cvs.setAttribute('width', dance.width)cvs.setAttribute('height', dance.height)cvs.width = dance.width * ZOOM_OUT;cvs.height = dance.height * ZOOM_OUT;document.body.querySelector("#container").appendChild(cvs)return cvs}/*** 渲染一幀* @author linyisonger* @date 2025-02-23*/async function drawFrame(cvs, frame, avatar) {/** @type {CanvasRenderingContext2D } */let ctx = cvs.getContext('2d')let roleImg = await loadImage(frame.url)let avatarImg = await loadImage(avatar)ctx.clearRect(0, 0, cvs.width, cvs.height)ctx.drawImage(roleImg, 0, 0, cvs.width, cvs.height)let avatarWidth = 40 * ZOOM_OUTctx.drawImage(avatarImg, (frame.avatar.x * ZOOM_OUT - avatarWidth / 2), (frame.avatar.y * ZOOM_OUT - avatarWidth / 2), avatarWidth, avatarWidth)}}initGame()</script></body></html>
源碼倉(cāng)庫(kù)
更新的話(huà)文章可能不一定會(huì)更新,倉(cāng)庫(kù)會(huì)可能更新,有問(wèn)題可以提issue~
https://github.com/linyisonger/H5.Examples