app.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // 别间隔时间(毫秒), 识别服务地址
  2. const webAR = new WebAR(1000, "/webar/recognize");
  3. // Threejs简单使用类
  4. const threeHelper = new ThreeHelper();
  5. // 列出并打开设备上的摄像头
  6. document.querySelector("#openCamera").addEventListener("click", function() {
  7. const videoSelect = document.querySelector("#videoDevice");
  8. navigator.mediaDevices.enumerateDevices().then(() => {
  9. webAR
  10. .listCamera(videoSelect)
  11. .then(msg => {
  12. // 隐藏"打开摄像头"按钮
  13. this.style.display = "none";
  14. videoSelect.style.display = "inline-block";
  15. document.querySelector("#start").style.display = "inline-block";
  16. document.querySelector("#stop").style.display = "inline-block";
  17. videoSelect.onchange = () => {
  18. webAR.openCamera(JSON.parse(videoSelect.value));
  19. };
  20. // 打开摄像头
  21. // 打开后置摄像头参数: {audio: false, video: {facingMode: {exact: 'environment'}}}
  22. webAR
  23. .openCamera(JSON.parse(videoSelect.value))
  24. .then(msg => {
  25. console.info(msg);
  26. })
  27. .catch(err => {
  28. console.info(err);
  29. });
  30. })
  31. .catch(err => {
  32. // 没有找到摄像头
  33. console.info(err);
  34. });
  35. });
  36. });
  37. // 开启识别
  38. document.querySelector("#start").addEventListener(
  39. "click",
  40. () => {
  41. webAR.startRecognize(msg => {
  42. console.info(msg);
  43. // 可以将 setting 作为meta上传到EasyAR的云识别,使用方法如下
  44. // const setting = window.atob(msg.meta);
  45. const setting = {
  46. model: "asset/model/trex_v3.fbx",
  47. scale: 0.02,
  48. position: [0, 0, 0]
  49. };
  50. threeHelper.loadObject(setting);
  51. });
  52. },
  53. false
  54. );
  55. // 暂停识别
  56. document.querySelector("#stop").addEventListener(
  57. "click",
  58. () => {
  59. webAR.stopRecognize();
  60. },
  61. false
  62. );
  63. //# sourceMappingURL=app.js.map