<script charset="utf-8" src="https://webar-static.tencent-cloud.com/ar-sdk/resources/1.0.26-4/webar-sdk.umd.js"></script><script src="https://webar-static.tencent-cloud.com/docs/examples/js-sha256/0.9.0/sha256.min.js"></script>
/** ----- Authentication configuration ----- *//*** Tencent Cloud account's APPID** You can view your APPID in the [Account Center](https://console.tencentcloud.com/developer).*/const APPID = ''; // Set it to your Tencent Cloud account APPID./*** Web LicenseKey** obtained from Before You Start*/const LICENSE_KEY = ''; // Set it to your license key./*** The token used to calculate the signature.** Note: This method is only suitable for debugging. In a production environment, you should store the token and calculate the signature on your server. The front end can get the signature by calling an API. For details, see* https://trtc.io/document/50099?platform=web&product=beautyar#e4ce3483-41f7-4391-83ae-f4b61e221ea0*/const token = ''; // Set it to your token./** ----------------------- *//*** Get the signature** Note: This method is only suitable for debugging. In a production environment, you should calculate the signature on your server. The front end can get the signature by calling an API.* eg:* async function () {* return fetch('http://xxx.com/get-ar-sign').then(res => res.json());* };*/const getSignature = function () {const timestamp = Math.round(new Date().getTime() / 1000);const signature = sha256(timestamp + token + APPID + timestamp).toUpperCase();return { signature, timestamp };};// ar sdk configconst config = {module: {beautify: true,handLandmark: true // rings try on require handLandmark module},auth: {licenseKey: LICENSE_KEY,appId: APPID,authFunc: getSignature},// Built-in Camera method is used to set the input;// for Custom Stream methods, please refer to. https://trtc.io/document/50102?platform=web&product=beautyarcamera: {width: 1080,height: 720,mirror: true,},}// new ArSdkconst { ArSdk } = window.AR;const ar = new ArSdk(config);ar.on('error', (e) => {console.log(e);});
let makeupList, glassesList, contactLensesList, headwearList, ringListlet customEffectListar.on('created', async () => {makeupList = await ar.getEffectList({Type: 'Preset',Label: ['Makeup', 'Woman']})glassesList = await ar.getEffectList({Type: 'Preset',Label: ['glasses']})contactLensesList = await ar.getEffectList({Type: 'Preset',Label: ['Iris']})headwearList = await ar.getEffectList({Type: 'Preset',Label: ['Headwear']})ringList = await ar.getEffectList({Type: 'Preset',Label: ['rings']})// In scenarios with custom materials, set the Type to 'Custom' to retrieve the custom materials. See// https://trtc.io/zh/document/53887?platform=web&product=beautyarcustomEffectList = await ar.getEffectList({Type: 'Custom'})});
setEffect to apply various beauty, eyewear,ring, and headwear materials, as well as all custom effects. Please refer to the API documentation for the interface parameters.function setMakeup() {ar.setEffect({id: makeupList[0].EffectId,})}function setGlasses() {ar.setEffect({id: glassesList[0].EffectId,})}function setContactLenses() {ar.setEffect({id: contactLensesList[0].EffectId,intensity: 0.6})}function setHeadwear() {ar.setEffect({id: headwearList[0].EffectId,})}function setRings() {ar.setEffect({id: ringList[0].EffectId,})}
ar.on('ready', async (e) => {// get output stream from ar sdkconst arOutputStream = await ar.getOutput();// view with video elementconst video = document.createElement("video");video.setAttribute("id", "webar-output-video");video.setAttribute("playsinline", "");video.setAttribute("autoplay", "");video.setAttribute("style", 'width:600px;height:auto;');video.srcObject = arOutputStream;document.body.appendChild(video);});
ar.on('ready', async (e) => {// localplayerContainer is the ID of the specified DOM container.// eg. <div style="display: inline-block;width: 1280px;height: auto;" id="localplayerContainer"></div>const player = await arSdk.initLocalPlayer('localplayerContainer')await player.play()});
serve . in the directory where the demo is located. Serving! ││ ││ - Local: http://localhost:57965 ││ - On Your Network: http://10.91.28.94:57965 ││ ││ This port was picked because 5000 is in use. ││ ││ Copied local address to clipboard!
<!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"><title>webar virtual try on demo</title></head><body><div id="controls" style="margin-bottom: 20px;"><button type="button" onclick="setMakeup()">set Makeup</button><button type="button" onclick="setGlasses()">set Glasses</button><button type="button" onclick="setContactLenses()">set Contact Lenses</button><button type="button" onclick="setHeadwear()">set Headwear</button><button type="button" onclick="setRings()">set Rings</button></div><script charset="utf-8" src="https://webar-static.tencent-cloud.com/ar-sdk/resources/1.0.26-4/webar-sdk.umd.js"></script><script src="https://webar-static.tencent-cloud.com/docs/examples/js-sha256/0.9.0/sha256.min.js"></script><script>const APPID = '';const LICENSE_KEY = '';const token = '';const getSignature = function () {const timestamp = Math.round(new Date().getTime() / 1000);const signature = sha256(timestamp + token + APPID + timestamp).toUpperCase();return {signature,timestamp};};const config = {module: {beautify: true,handLandmark: true // rings try on require handLandmark module},auth: {licenseKey: LICENSE_KEY,appId: APPID,authFunc: getSignature},camera: {width: 1080,height: 720,mirror: true,},}// new ArSdkconst { ArSdk } = window.AR;const ar = new ArSdk(config);ar.on('error', (e) => {console.log(e);alert(e.message);});let makeupList, glassesList, contactLensesList, headwearList, ringListlet customEffectList// get effect listar.on('created', async () => {makeupList = await ar.getEffectList({Type: 'Preset',Label: ['Makeup', 'Woman']})glassesList = await ar.getEffectList({Type: 'Preset',Label: ['glasses']})contactLensesList = await ar.getEffectList({Type: 'Preset',Label: ['Iris']})headwearList = await ar.getEffectList({Type: 'Preset',Label: ['Headwear']})ringList = await ar.getEffectList({Type: 'Preset',Label: ['rings']})// In scenarios with custom materials, set the Type to 'Custom' to retrieve the custom materials. See// https://trtc.io/zh/document/53887?platform=web&product=beautyarcustomEffectList = await ar.getEffectList({Type: 'Custom'})});function setMakeup() {ar.setEffect({id: makeupList[0].EffectId,})}function setGlasses() {ar.setEffect({id: glassesList[0].EffectId,})}function setContactLenses() {ar.setEffect({id: contactLensesList[0].EffectId,intensity: 0.6})}function setHeadwear() {ar.setEffect({id: headwearList[0].EffectId,})}function setRings() {ar.setEffect({id: ringList[0].EffectId,})}ar.on('ready', async (e) => {// get output stream from ar sdkconst arOutputStream = await ar.getOutput();// view with video elementconst video = document.createElement("video");video.setAttribute("id", "webar-output-video");video.setAttribute("playsinline", "");video.setAttribute("autoplay", "");video.setAttribute("style", 'width:600px;height:auto;');video.srcObject = arOutputStream;document.body.appendChild(video);});</script></body></html>
Feedback