index.vue 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. <template>
  2. <div class="push-wrap">
  3. <div
  4. ref="topRef"
  5. class="left"
  6. >
  7. <div
  8. ref="containerRef"
  9. class="container"
  10. >
  11. <canvas
  12. id="pushCanvasRef"
  13. ref="pushCanvasRef"
  14. ></canvas>
  15. <div
  16. v-if="appStore.allTrack.filter((item) => !item.hidden).length <= 0"
  17. class="add-wrap"
  18. >
  19. <n-space>
  20. <n-button
  21. v-for="(item, index) in allMediaTypeList"
  22. :key="index"
  23. class="item"
  24. @click="handleStartMedia(item)"
  25. >
  26. {{ item.txt }}
  27. </n-button>
  28. </n-space>
  29. </div>
  30. </div>
  31. <div
  32. ref="bottomRef"
  33. class="room-control"
  34. >
  35. <div class="info">
  36. <div
  37. class="avatar"
  38. :style="{ backgroundImage: `url(${userStore.userInfo?.avatar})` }"
  39. ></div>
  40. <div class="detail">
  41. <div class="top">
  42. <n-input-group>
  43. <n-input
  44. v-model:value="roomName"
  45. size="small"
  46. placeholder="输入房间名"
  47. :style="{ width: '50%' }"
  48. />
  49. <n-button
  50. size="small"
  51. type="primary"
  52. @click="confirmRoomName"
  53. >
  54. 确定
  55. </n-button>
  56. </n-input-group>
  57. </div>
  58. <div class="bottom">
  59. <span v-if="NODE_ENV === 'development'">
  60. {{ mySocketId }}
  61. </span>
  62. </div>
  63. </div>
  64. </div>
  65. <div class="rtc">
  66. <div class="item">
  67. <div class="txt">码率设置</div>
  68. <div class="down">
  69. <n-select
  70. v-model:value="currentMaxBitrate"
  71. :options="maxBitrate"
  72. />
  73. </div>
  74. </div>
  75. <div class="item">
  76. <div class="txt">帧率设置</div>
  77. <div class="down">
  78. <n-select
  79. v-model:value="currentMaxFramerate"
  80. :options="maxFramerate"
  81. />
  82. </div>
  83. </div>
  84. <div class="item">
  85. <div class="txt">分辨率设置</div>
  86. <div class="down">
  87. <n-select
  88. v-model:value="currentResolutionRatio"
  89. :options="resolutionRatio"
  90. />
  91. </div>
  92. </div>
  93. </div>
  94. <div class="other">
  95. <div class="top">
  96. <span class="item">
  97. <i class="ico"></i>
  98. <span>
  99. 正在观看:
  100. {{
  101. liveUserList.filter((item) => item.id !== mySocketId).length
  102. }}
  103. </span>
  104. </span>
  105. </div>
  106. <div class="bottom">
  107. <n-button
  108. v-if="!roomLiving"
  109. type="info"
  110. size="small"
  111. @click="handleStartLive"
  112. >
  113. 开始直播
  114. </n-button>
  115. <n-button
  116. v-else
  117. type="error"
  118. size="small"
  119. @click="endLive"
  120. >
  121. 结束直播
  122. </n-button>
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. <div class="right">
  128. <div class="resource-card">
  129. <div class="title">素材列表</div>
  130. <div class="list">
  131. <div
  132. v-for="(item, index) in appStore.allTrack.filter(
  133. (item) => !item.hidden
  134. )"
  135. :key="index"
  136. class="item"
  137. >
  138. <span class="name">
  139. ({{ mediaTypeEnumMap[item.type] }}){{ item.mediaName }}
  140. </span>
  141. <div class="control">
  142. <div
  143. v-if="item.audio === 1"
  144. class="control-item"
  145. @click="handleChangeMuted(item)"
  146. >
  147. <n-popover
  148. placement="top"
  149. trigger="hover"
  150. :flip="false"
  151. >
  152. <template #trigger>
  153. <n-icon size="16">
  154. <VolumeMuteOutline v-if="item.muted"></VolumeMuteOutline>
  155. <VolumeHighOutline v-else></VolumeHighOutline>
  156. </n-icon>
  157. </template>
  158. <div class="slider">
  159. <n-slider
  160. :value="item.volume"
  161. :step="1"
  162. @update-value="(v) => updateVolume(item, v)"
  163. />
  164. </div>
  165. </n-popover>
  166. </div>
  167. <div
  168. class="control-item"
  169. @click="handleEdit(item)"
  170. >
  171. <n-icon size="16">
  172. <CreateOutline></CreateOutline>
  173. </n-icon>
  174. </div>
  175. <div
  176. class="control-item"
  177. @click="handleDel(item)"
  178. >
  179. <n-icon size="16">
  180. <Close></Close>
  181. </n-icon>
  182. </div>
  183. </div>
  184. </div>
  185. </div>
  186. <div class="bottom">
  187. <n-button
  188. size="small"
  189. type="primary"
  190. @click="showSelectMediaModalCpt = true"
  191. >
  192. 添加素材
  193. </n-button>
  194. </div>
  195. </div>
  196. <div class="danmu-card">
  197. <div class="title">弹幕互动</div>
  198. <div class="list-wrap">
  199. <div
  200. ref="danmuListRef"
  201. class="list"
  202. >
  203. <div
  204. v-for="(item, index) in damuList"
  205. :key="index"
  206. class="item"
  207. >
  208. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  209. <span class="name">
  210. {{ item.userInfo?.username || item.socket_id }}:
  211. </span>
  212. <span class="msg">{{ item.msg }}</span>
  213. </template>
  214. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  215. <span class="name system">系统通知:</span>
  216. <span class="msg">
  217. <span>{{ item.userInfo?.username || item.socket_id }}</span>
  218. <span>进入直播!</span>
  219. </span>
  220. </template>
  221. <template
  222. v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved"
  223. >
  224. <span class="name system">系统通知:</span>
  225. <span class="msg">
  226. <span>{{ item.userInfo?.username || item.socket_id }}</span>
  227. <span>离开直播!</span>
  228. </span>
  229. </template>
  230. </div>
  231. </div>
  232. </div>
  233. <div class="send-msg">
  234. <input
  235. v-model="danmuStr"
  236. class="ipt"
  237. @keydown="keydownDanmu"
  238. />
  239. <n-button
  240. type="info"
  241. size="small"
  242. @click="sendDanmu"
  243. >
  244. 发送
  245. </n-button>
  246. </div>
  247. </div>
  248. </div>
  249. <SelectMediaModalCpt
  250. v-if="showSelectMediaModalCpt"
  251. :all-media-type-list="allMediaTypeList"
  252. @close="showSelectMediaModalCpt = false"
  253. @ok="handleShowMediaModalCpt"
  254. ></SelectMediaModalCpt>
  255. <MediaModalCpt
  256. v-if="showMediaModalCpt"
  257. :is-edit="isEdit"
  258. :media-type="currentMediaType"
  259. :init-data="currentMediaData"
  260. @close="showMediaModalCpt = false"
  261. @add-ok="addMediaOk"
  262. @edit-ok="editMediaOk"
  263. ></MediaModalCpt>
  264. <OpenMicophoneTipCpt
  265. v-if="showOpenMicophoneTipCpt"
  266. @close="showOpenMicophoneTipCpt = false"
  267. ></OpenMicophoneTipCpt>
  268. </div>
  269. </template>
  270. <script lang="ts" setup>
  271. import {
  272. Close,
  273. CreateOutline,
  274. VolumeHighOutline,
  275. VolumeMuteOutline,
  276. } from '@vicons/ionicons5';
  277. import { fabric } from 'fabric';
  278. import {
  279. Raw,
  280. markRaw,
  281. onMounted,
  282. onUnmounted,
  283. reactive,
  284. ref,
  285. watch,
  286. } from 'vue';
  287. import { useRoute } from 'vue-router';
  288. import * as workerTimers from 'worker-timers';
  289. import { mediaTypeEnumMap } from '@/constant';
  290. import { usePush } from '@/hooks/use-push';
  291. import { useRTCParams } from '@/hooks/use-rtc-params';
  292. import { DanmuMsgTypeEnum, MediaTypeEnum } from '@/interface';
  293. import { AppRootState, useAppStore } from '@/store/app';
  294. import { useResourceCacheStore } from '@/store/cache';
  295. import { useUserStore } from '@/store/user';
  296. import {
  297. createVideo,
  298. formatDownTime,
  299. generateBase64,
  300. getRandomEnglishString,
  301. readFile,
  302. saveFile,
  303. } from '@/utils';
  304. import { NODE_ENV } from 'script/constant';
  305. import MediaModalCpt from './mediaModal/index.vue';
  306. import OpenMicophoneTipCpt from './openMicophoneTip/index.vue';
  307. import SelectMediaModalCpt from './selectMediaModal/index.vue';
  308. const route = useRoute();
  309. const userStore = useUserStore();
  310. const appStore = useAppStore();
  311. const resourceCacheStore = useResourceCacheStore();
  312. const { maxBitrate, maxFramerate, resolutionRatio, allMediaTypeList } =
  313. useRTCParams();
  314. const {
  315. confirmRoomName,
  316. startLive,
  317. endLive,
  318. sendDanmu,
  319. keydownDanmu,
  320. addTrack,
  321. delTrack,
  322. mySocketId,
  323. lastCoverImg,
  324. canvasVideoStream,
  325. roomLiving,
  326. currentResolutionRatio,
  327. currentMaxBitrate,
  328. currentMaxFramerate,
  329. danmuStr,
  330. roomName,
  331. damuList,
  332. liveUserList,
  333. } = usePush();
  334. const currentMediaType = ref(MediaTypeEnum.camera);
  335. const currentMediaData = ref<AppRootState['allTrack'][0]>();
  336. const showOpenMicophoneTipCpt = ref(false);
  337. const showSelectMediaModalCpt = ref(false);
  338. const showMediaModalCpt = ref(false);
  339. const isEdit = ref(false);
  340. const topRef = ref<HTMLDivElement>();
  341. const bottomRef = ref<HTMLDivElement>();
  342. const danmuListRef = ref<HTMLDivElement>();
  343. const containerRef = ref<HTMLDivElement>();
  344. const pushCanvasRef = ref<HTMLCanvasElement>();
  345. const webaudioVideo = ref<HTMLVideoElement>();
  346. const fabricCanvas = ref<fabric.Canvas>();
  347. const audioCtx = ref<AudioContext>();
  348. const startTime = ref(+new Date());
  349. const normalVolume = ref(60);
  350. // const startTime = ref(1692807352565); // 1693027352565
  351. const timeCanvasDom = ref<Raw<fabric.Text>[]>([]);
  352. const stopwatchCanvasDom = ref<Raw<fabric.Text>[]>([]);
  353. const wrapSize = reactive({
  354. width: 0,
  355. height: 0,
  356. });
  357. const workerTimerId = ref(-1);
  358. const bodyAppendChildElArr = ref<HTMLElement[]>([]);
  359. const liveType = Number(route.query.liveType);
  360. watch(
  361. () => damuList.value.length,
  362. () => {
  363. setTimeout(() => {
  364. if (danmuListRef.value) {
  365. danmuListRef.value.scrollTop = danmuListRef.value.scrollHeight;
  366. }
  367. }, 0);
  368. }
  369. );
  370. onMounted(() => {
  371. setTimeout(() => {
  372. scrollTo(0, 0);
  373. }, 100);
  374. initUserMedia();
  375. initCanvas();
  376. handleCache();
  377. });
  378. onUnmounted(() => {
  379. bodyAppendChildElArr.value.forEach((el) => {
  380. el.remove();
  381. });
  382. clearFrame();
  383. });
  384. function initUserMedia() {
  385. navigator.mediaDevices
  386. .getUserMedia({
  387. video: true,
  388. audio: false,
  389. })
  390. .then(() => {
  391. console.log('初始化获取摄像头成功');
  392. })
  393. .catch(() => {
  394. console.log('初始化获取摄像头失败');
  395. })
  396. .finally(() => {
  397. navigator.mediaDevices
  398. .getUserMedia({
  399. video: false,
  400. audio: true,
  401. })
  402. .then(() => {
  403. console.log('初始化获取麦克风成功');
  404. })
  405. .catch(() => {
  406. console.log('初始化获取麦克风失败');
  407. showOpenMicophoneTipCpt.value = true;
  408. });
  409. });
  410. }
  411. function renderAll() {
  412. timeCanvasDom.value.forEach((item) => {
  413. item.text = new Date().toLocaleString();
  414. });
  415. stopwatchCanvasDom.value.forEach((item) => {
  416. item.text = formatDownTime({
  417. endTime: +new Date(),
  418. startTime: startTime.value,
  419. showMs: true,
  420. });
  421. });
  422. fabricCanvas.value?.renderAll();
  423. }
  424. function clearFrame() {
  425. if (workerTimerId.value !== -1) {
  426. workerTimers.clearInterval(workerTimerId.value);
  427. }
  428. }
  429. function renderFrame() {
  430. const delay = 1000 / 60; // 16.666666666666668
  431. workerTimerId.value = workerTimers.setInterval(() => {
  432. renderAll();
  433. }, delay);
  434. console.log('workerTimerId.value', workerTimerId.value);
  435. }
  436. function handleMixedAudio() {
  437. console.log('handleMixedAudio');
  438. const allAudioTrack = appStore.allTrack.filter((item) => item.audio === 1);
  439. audioCtx.value = new AudioContext();
  440. const gainNode = audioCtx.value.createGain();
  441. allAudioTrack.forEach((item) => {
  442. if (!audioCtx.value || !item.stream) return;
  443. const audioInput = audioCtx.value.createMediaStreamSource(item.stream);
  444. audioInput.connect(gainNode);
  445. console.log('混流', item.stream?.id, item.stream);
  446. });
  447. const destination = audioCtx.value.createMediaStreamDestination();
  448. if (canvasVideoStream.value?.getAudioTracks()[0]) {
  449. canvasVideoStream.value.removeTrack(
  450. canvasVideoStream.value?.getAudioTracks()[0]
  451. );
  452. }
  453. canvasVideoStream.value?.addTrack(destination.stream.getAudioTracks()[0]);
  454. // streamTmp = destination.stream;
  455. if (canvasVideoStream.value?.getAudioTracks()[0]) {
  456. canvasVideoStream.value.removeTrack(
  457. canvasVideoStream.value?.getAudioTracks()[0]
  458. );
  459. canvasVideoStream.value?.addTrack(destination.stream.getAudioTracks()[0]);
  460. } else {
  461. canvasVideoStream.value?.addTrack(destination.stream.getAudioTracks()[0]);
  462. }
  463. console.log(
  464. canvasVideoStream.value?.getAudioTracks(),
  465. 'canvasVideoStreamcanvasVideoStream'
  466. );
  467. gainNode.connect(destination);
  468. if (webaudioVideo.value) {
  469. webaudioVideo.value.remove();
  470. }
  471. webaudioVideo.value = createVideo({ appendChild: true });
  472. webaudioVideo.value.className = 'web-audio-video';
  473. webaudioVideo.value.srcObject = destination.stream;
  474. }
  475. function handleStartLive() {
  476. handleMixedAudio();
  477. lastCoverImg.value = generateBase64(pushCanvasRef.value!);
  478. startLive({ type: liveType, receiver: mySocketId.value });
  479. }
  480. function handleScale({ width, height }: { width: number; height: number }) {
  481. const resolutionHeight = currentResolutionRatio.value;
  482. const resolutionWidth = currentResolutionRatio.value * appStore.videoRatio;
  483. let ratio = 1;
  484. if (width > resolutionWidth) {
  485. const r1 = resolutionWidth / width;
  486. ratio = r1;
  487. }
  488. if (height > resolutionHeight) {
  489. const r1 = resolutionHeight / height;
  490. if (ratio > r1) {
  491. ratio = r1;
  492. }
  493. }
  494. return ratio;
  495. }
  496. function autoCreateVideo({
  497. stream,
  498. id,
  499. rect,
  500. muted,
  501. }: {
  502. stream: MediaStream;
  503. id: string;
  504. rect?: { left: number; top: number };
  505. muted?: boolean;
  506. }) {
  507. console.warn('autoCreateVideo', id);
  508. const videoEl = createVideo({ appendChild: true });
  509. if (muted !== undefined) {
  510. videoEl.muted = muted;
  511. }
  512. videoEl.srcObject = stream;
  513. bodyAppendChildElArr.value.push(videoEl);
  514. return new Promise<{
  515. canvasDom: fabric.Image;
  516. videoEl: HTMLVideoElement;
  517. scale: number;
  518. }>((resolve) => {
  519. videoEl.onloadedmetadata = () => {
  520. const width = stream.getVideoTracks()[0].getSettings().width!;
  521. const height = stream.getVideoTracks()[0].getSettings().height!;
  522. const ratio = handleScale({ width, height });
  523. videoEl.width = width;
  524. videoEl.height = height;
  525. const canvasDom = markRaw(
  526. new fabric.Image(videoEl, {
  527. top: rect?.top || 0,
  528. left: rect?.left || 0,
  529. width,
  530. height,
  531. })
  532. );
  533. console.log(
  534. '初始化',
  535. ratio,
  536. canvasDom.width,
  537. canvasDom.height,
  538. width * ratio,
  539. height * ratio,
  540. canvasDom
  541. );
  542. handleMoving({ canvasDom, id });
  543. handleScaling({ canvasDom, id });
  544. canvasDom.scale(ratio / window.devicePixelRatio);
  545. fabricCanvas.value!.add(canvasDom);
  546. resolve({ canvasDom, scale: ratio, videoEl });
  547. };
  548. });
  549. }
  550. watch(
  551. () => currentResolutionRatio.value,
  552. (newHeight, oldHeight) => {
  553. changeCanvasAttr({ newHeight, oldHeight });
  554. }
  555. );
  556. // 容器宽高,1280*720,即720p
  557. // canvas容器宽高,2560*1440,即1440p
  558. // ======
  559. // 容器宽高,960*540,即540p
  560. // dom宽高,640*480
  561. // canvas容器宽高,960*540,即540p
  562. // 将dom绘制到容器里,此时dom的大小就是640*480
  563. // 需求,不管切换多少分辨率,我要看到的dom都是一样大小,即
  564. // 960*540时,dom是640*480
  565. // 1280*720时,dom不能是640*480了,因为这样他就会对比上一个分辨率的dom看起来小了,960/1280=0.75,540/720=0.75,
  566. // 其实就是分辨率变大了,我们就要将图片也变大,即图片的宽是640/0.75=853.4,高是480/0.75=640
  567. // 坐标变化,960*540时,dom坐标是100,100
  568. // 1280*720时,dom的坐标不能再是100,100了,否则对比上一个分辨率看起来偏
  569. function changeCanvasAttr({
  570. newHeight,
  571. oldHeight,
  572. }: {
  573. newHeight: number;
  574. oldHeight: number;
  575. }) {
  576. if (fabricCanvas.value) {
  577. const resolutionHeight =
  578. currentResolutionRatio.value / window.devicePixelRatio;
  579. const resolutionWidth =
  580. (currentResolutionRatio.value / window.devicePixelRatio) *
  581. appStore.videoRatio;
  582. fabricCanvas.value.setWidth(resolutionWidth);
  583. fabricCanvas.value.setHeight(resolutionHeight);
  584. appStore.allTrack.forEach((iten) => {
  585. console.log('当前类型', iten.type);
  586. const item = iten.canvasDom;
  587. if (item) {
  588. // 分辨率变小了,将图片变小
  589. if (newHeight < oldHeight) {
  590. const ratio2 = oldHeight / newHeight;
  591. item.left = item.left! / ratio2;
  592. item.top = item.top! / ratio2;
  593. } else {
  594. // 分辨率变大了,将图片变大
  595. const ratio2 = oldHeight / newHeight;
  596. item.left = item.left! / ratio2;
  597. item.top = item.top! / ratio2;
  598. }
  599. }
  600. });
  601. appStore.allTrack.forEach((iten) => {
  602. console.log('当前类型', iten.type);
  603. const item = iten.canvasDom;
  604. if (item) {
  605. // 分辨率变小了,将图片变小
  606. if (newHeight < oldHeight) {
  607. const ratio = newHeight / oldHeight;
  608. const ratio1 = (item.scaleX || 1) * ratio;
  609. item.scale(ratio1);
  610. } else {
  611. // 分辨率变大了,将图片变大
  612. const ratio = newHeight / oldHeight;
  613. const ratio1 = (item.scaleX || 1) * ratio;
  614. item.scale(ratio1);
  615. }
  616. }
  617. });
  618. changeCanvasStyle();
  619. }
  620. }
  621. function changeCanvasStyle() {
  622. // @ts-ignore
  623. fabricCanvas.value.wrapperEl.style.width = `${wrapSize.width}px`;
  624. // @ts-ignore
  625. fabricCanvas.value.wrapperEl.style.height = `${wrapSize.height}px`;
  626. // @ts-ignore
  627. fabricCanvas.value.lowerCanvasEl.style.width = `${wrapSize.width}px`;
  628. // @ts-ignore
  629. fabricCanvas.value.lowerCanvasEl.style.height = `${wrapSize.height}px`;
  630. // @ts-ignore
  631. fabricCanvas.value.upperCanvasEl.style.width = `${wrapSize.width}px`;
  632. // @ts-ignore
  633. fabricCanvas.value.upperCanvasEl.style.height = `${wrapSize.height}px`;
  634. }
  635. function initCanvas() {
  636. const resolutionHeight =
  637. currentResolutionRatio.value / window.devicePixelRatio;
  638. const resolutionWidth =
  639. (currentResolutionRatio.value / window.devicePixelRatio) *
  640. appStore.videoRatio;
  641. const wrapWidth = containerRef.value!.getBoundingClientRect().width;
  642. // const wrapWidth = 1920;
  643. const ratio = wrapWidth / resolutionWidth;
  644. const wrapHeight = resolutionHeight * ratio;
  645. // const wrapHeight = 1080;
  646. // lower-canvas: 实际的canvas画面,也就是pushCanvasRef
  647. // upper-canvas: 操作时候的canvas
  648. const ins = markRaw(new fabric.Canvas(pushCanvasRef.value!));
  649. ins.setWidth(resolutionWidth);
  650. ins.setHeight(resolutionHeight);
  651. console.log('initCanvas', { resolutionWidth, resolutionHeight });
  652. ins.setBackgroundColor('black', () => {
  653. console.log('setBackgroundColor回调');
  654. });
  655. wrapSize.width = wrapWidth;
  656. wrapSize.height = wrapHeight;
  657. fabricCanvas.value = ins;
  658. renderFrame();
  659. changeCanvasStyle();
  660. }
  661. /**
  662. * 1: {scaleX: 1, scaleY: 1}
  663. * 2: {scaleX: 0.5, scaleY: 0.5}
  664. * 3: {scaleX: 0.3333333333333333, scaleY: 0.3333333333333333}
  665. * 4: {scaleX: 0.25, scaleY: 0.25}
  666. */
  667. /**
  668. * 二倍屏即1px里面有2个像素;三倍屏1px里面有3个像素,以此类推
  669. * 一个图片,宽高都是100px
  670. * 一倍屏展示:100px等于100个像素,一比一展示
  671. * 二倍屏展示:100px等于100个像素,二比一展示,即在二倍屏的100px看起来会比一倍屏的100px小一倍
  672. * 如果需要在一杯和二倍屏幕的时候看的大小都一样:
  673. * 1,在二倍屏的时候,需要将100px放大一倍,即200px;
  674. * 2,在一倍屏的时候,需要将100px缩小一百,即50px;
  675. */
  676. function handleScaling({ canvasDom, id }) {
  677. canvasDom.on('scaling', () => {
  678. appStore.allTrack.forEach((item) => {
  679. if (id === item.id) {
  680. item.scaleInfo[window.devicePixelRatio] = {
  681. scaleX: canvasDom.scaleX || 1,
  682. scaleY: canvasDom.scaleY || 1,
  683. };
  684. Object.keys(item.scaleInfo).forEach((iten) => {
  685. if (window.devicePixelRatio !== Number(iten)) {
  686. if (window.devicePixelRatio > Number(iten)) {
  687. item.scaleInfo[iten] = {
  688. scaleX:
  689. item.scaleInfo[window.devicePixelRatio].scaleX *
  690. window.devicePixelRatio,
  691. scaleY:
  692. item.scaleInfo[window.devicePixelRatio].scaleY *
  693. window.devicePixelRatio,
  694. };
  695. } else {
  696. if (window.devicePixelRatio === 1) {
  697. item.scaleInfo[iten] = {
  698. scaleX: item.scaleInfo[1].scaleX / Number(iten),
  699. scaleY: item.scaleInfo[1].scaleY / Number(iten),
  700. };
  701. } else {
  702. item.scaleInfo[iten] = {
  703. scaleX: item.scaleInfo[1].scaleX * Number(iten),
  704. scaleY: item.scaleInfo[1].scaleY * Number(iten),
  705. };
  706. }
  707. }
  708. }
  709. });
  710. }
  711. });
  712. resourceCacheStore.setList(appStore.allTrack);
  713. });
  714. }
  715. function handleMoving({
  716. canvasDom,
  717. id,
  718. }: {
  719. canvasDom: fabric.Image | fabric.Text;
  720. id: string;
  721. }) {
  722. canvasDom.on('moving', () => {
  723. console.log(
  724. 'moving',
  725. canvasDom.width,
  726. canvasDom.height,
  727. canvasDom.scaleX,
  728. canvasDom.scaleY
  729. );
  730. appStore.allTrack.forEach((item) => {
  731. if (id === item.id) {
  732. item.rect = {
  733. top: (canvasDom.top || 0) * window.devicePixelRatio,
  734. left: (canvasDom.left || 0) * window.devicePixelRatio,
  735. };
  736. }
  737. });
  738. resourceCacheStore.setList(appStore.allTrack);
  739. });
  740. }
  741. async function handleCache() {
  742. const res: AppRootState['allTrack'] = [];
  743. const queue: any[] = [];
  744. resourceCacheStore.list.forEach((item) => {
  745. // @ts-ignore
  746. const obj: AppRootState['allTrack'][0] = {};
  747. obj.audio = item.audio;
  748. obj.video = item.video;
  749. obj.id = item.id;
  750. obj.deviceId = item.deviceId;
  751. obj.type = item.type;
  752. obj.hidden = item.hidden;
  753. obj.mediaName = item.mediaName;
  754. obj.muted = item.muted;
  755. obj.volume = item.volume;
  756. obj.rect = item.rect;
  757. obj.scaleInfo = item.scaleInfo;
  758. obj.stopwatchInfo = item.stopwatchInfo;
  759. async function handleMediaVideo() {
  760. const { code, file } = await readFile(item.id);
  761. if (code === 1 && file) {
  762. const url = URL.createObjectURL(file);
  763. const videoEl = createVideo({
  764. muted: item.muted ? item.muted : false,
  765. appendChild: true,
  766. });
  767. if (obj.volume !== undefined) {
  768. videoEl.volume = obj.volume / 100;
  769. }
  770. videoEl.src = url;
  771. bodyAppendChildElArr.value.push(videoEl);
  772. await new Promise((resolve) => {
  773. videoEl.onloadedmetadata = () => {
  774. const stream = videoEl
  775. // @ts-ignore
  776. .captureStream();
  777. const width = stream.getVideoTracks()[0].getSettings().width!;
  778. const height = stream.getVideoTracks()[0].getSettings().height!;
  779. videoEl.width = width;
  780. videoEl.height = height;
  781. const canvasDom = markRaw(
  782. new fabric.Image(videoEl, {
  783. top: (item.rect?.top || 0) / window.devicePixelRatio,
  784. left: (item.rect?.left || 0) / window.devicePixelRatio,
  785. width,
  786. height,
  787. })
  788. );
  789. handleMoving({ canvasDom, id: item.id });
  790. handleScaling({ canvasDom, id: item.id });
  791. canvasDom.scale(
  792. item.scaleInfo[window.devicePixelRatio].scaleX || 1
  793. );
  794. fabricCanvas.value!.add(canvasDom);
  795. obj.videoEl = videoEl;
  796. obj.canvasDom = canvasDom;
  797. resolve({ videoEl, canvasDom });
  798. };
  799. });
  800. const stream = videoEl
  801. // @ts-ignore
  802. .captureStream() as MediaStream;
  803. obj.stream = stream;
  804. obj.streamid = stream.id;
  805. obj.track = stream.getVideoTracks()[0];
  806. obj.trackid = stream.getVideoTracks()[0].id;
  807. } else {
  808. console.error('读取文件失败');
  809. }
  810. }
  811. async function handleImg() {
  812. const { code, file } = await readFile(item.id);
  813. if (code === 1 && file) {
  814. const imgEl = await new Promise<HTMLImageElement>((resolve) => {
  815. const reader = new FileReader();
  816. reader.addEventListener(
  817. 'load',
  818. function () {
  819. const img = document.createElement('img');
  820. img.src = reader.result as string;
  821. img.onload = () => {
  822. resolve(img);
  823. };
  824. },
  825. false
  826. );
  827. if (file) {
  828. reader.readAsDataURL(file);
  829. }
  830. });
  831. if (fabricCanvas.value) {
  832. const canvasDom = markRaw(
  833. new fabric.Image(imgEl, {
  834. top: (item.rect?.top || 0) / window.devicePixelRatio,
  835. left: (item.rect?.left || 0) / window.devicePixelRatio,
  836. width: imgEl.width,
  837. height: imgEl.height,
  838. })
  839. );
  840. handleMoving({ canvasDom, id: obj.id });
  841. handleScaling({ canvasDom, id: obj.id });
  842. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX || 1);
  843. fabricCanvas.value.add(canvasDom);
  844. obj.canvasDom = canvasDom;
  845. }
  846. } else {
  847. console.error('读取文件失败');
  848. }
  849. }
  850. async function handleScreen() {
  851. const event = await navigator.mediaDevices.getUserMedia({
  852. video: true,
  853. audio: { deviceId: obj.deviceId },
  854. });
  855. const videoEl = createVideo({ appendChild: true, muted: false });
  856. videoEl.srcObject = event;
  857. if (obj.volume !== undefined) {
  858. videoEl.volume = obj.volume / 100;
  859. }
  860. obj.videoEl = videoEl;
  861. }
  862. async function handleMicrophone() {
  863. const event = await navigator.mediaDevices.getUserMedia({
  864. video: false,
  865. audio: { deviceId: obj.deviceId },
  866. });
  867. const videoEl = createVideo({ appendChild: true, muted: false });
  868. videoEl.srcObject = event;
  869. if (obj.volume !== undefined) {
  870. videoEl.volume = obj.volume / 100;
  871. }
  872. obj.videoEl = videoEl;
  873. }
  874. async function handleCamera() {
  875. const event = await navigator.mediaDevices.getUserMedia({
  876. video: { deviceId: obj.deviceId },
  877. audio: false,
  878. });
  879. const videoEl = createVideo({ appendChild: true });
  880. videoEl.srcObject = event;
  881. await new Promise((resolve) => {
  882. videoEl.onloadedmetadata = () => {
  883. const stream = videoEl
  884. // @ts-ignore
  885. .captureStream();
  886. const width = stream.getVideoTracks()[0].getSettings().width!;
  887. const height = stream.getVideoTracks()[0].getSettings().height!;
  888. videoEl.width = width;
  889. videoEl.height = height;
  890. const canvasDom = markRaw(
  891. new fabric.Image(videoEl, {
  892. top: (item.rect?.top || 0) / window.devicePixelRatio,
  893. left: (item.rect?.left || 0) / window.devicePixelRatio,
  894. width,
  895. height,
  896. })
  897. );
  898. handleMoving({ canvasDom, id: item.id });
  899. handleScaling({ canvasDom, id: item.id });
  900. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX || 1);
  901. fabricCanvas.value!.add(canvasDom);
  902. obj.videoEl = videoEl;
  903. obj.canvasDom = canvasDom;
  904. resolve({ videoEl, canvasDom });
  905. };
  906. });
  907. }
  908. if (item.type === MediaTypeEnum.media && item.video === 1) {
  909. queue.push(handleMediaVideo());
  910. } else if (item.type === MediaTypeEnum.screen) {
  911. queue.push(handleScreen());
  912. } else if (item.type === MediaTypeEnum.camera) {
  913. queue.push(handleCamera());
  914. } else if (item.type === MediaTypeEnum.microphone) {
  915. queue.push(handleMicrophone());
  916. } else if (item.type === MediaTypeEnum.img) {
  917. queue.push(handleImg());
  918. } else if (item.type === MediaTypeEnum.txt) {
  919. obj.txtInfo = item.txtInfo;
  920. obj.scaleInfo = item.scaleInfo;
  921. if (fabricCanvas.value) {
  922. const canvasDom = markRaw(
  923. new fabric.Text(item.txtInfo?.txt || '', {
  924. top: (item.rect?.top || 0) / window.devicePixelRatio,
  925. left: (item.rect?.left || 0) / window.devicePixelRatio,
  926. fill: item.txtInfo?.color,
  927. })
  928. );
  929. handleMoving({ canvasDom, id: obj.id });
  930. handleScaling({ canvasDom, id: obj.id });
  931. // fabric.Text类型不能除以分辨率
  932. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  933. fabricCanvas.value.add(canvasDom);
  934. obj.canvasDom = canvasDom;
  935. }
  936. } else if (item.type === MediaTypeEnum.time) {
  937. obj.timeInfo = item.timeInfo;
  938. obj.scaleInfo = item.scaleInfo;
  939. if (fabricCanvas.value) {
  940. const canvasDom = markRaw(
  941. new fabric.Text(new Date().toLocaleString(), {
  942. top: (item.rect?.top || 0) / window.devicePixelRatio,
  943. left: (item.rect?.left || 0) / window.devicePixelRatio,
  944. fill: item.timeInfo?.color,
  945. })
  946. );
  947. timeCanvasDom.value.push(canvasDom);
  948. handleMoving({ canvasDom, id: obj.id });
  949. handleScaling({ canvasDom, id: obj.id });
  950. // fabric.Text类型不能除以分辨率
  951. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  952. fabricCanvas.value.add(canvasDom);
  953. obj.canvasDom = canvasDom;
  954. }
  955. } else if (item.type === MediaTypeEnum.stopwatch) {
  956. obj.stopwatchInfo = item.stopwatchInfo;
  957. obj.scaleInfo = item.scaleInfo;
  958. if (fabricCanvas.value) {
  959. const canvasDom = markRaw(
  960. new fabric.Text('00天00时00分00秒000毫秒', {
  961. top: (item.rect?.top || 0) / window.devicePixelRatio,
  962. left: (item.rect?.left || 0) / window.devicePixelRatio,
  963. fill: item.stopwatchInfo?.color,
  964. })
  965. );
  966. stopwatchCanvasDom.value.push(canvasDom);
  967. handleMoving({ canvasDom, id: obj.id });
  968. handleScaling({ canvasDom, id: obj.id });
  969. // fabric.Text类型不能除以分辨率
  970. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  971. fabricCanvas.value.add(canvasDom);
  972. obj.canvasDom = canvasDom;
  973. }
  974. }
  975. res.push(obj);
  976. });
  977. await Promise.all(queue);
  978. canvasVideoStream.value = pushCanvasRef.value!.captureStream();
  979. appStore.setAllTrack(res);
  980. }
  981. function handleShowMediaModalCpt(val: MediaTypeEnum) {
  982. isEdit.value = false;
  983. currentMediaData.value = undefined;
  984. showMediaModalCpt.value = true;
  985. showSelectMediaModalCpt.value = false;
  986. currentMediaType.value = val;
  987. }
  988. function handleEdit(item: AppRootState['allTrack'][0]) {
  989. console.log('handleEdit', item);
  990. currentMediaType.value = item.type;
  991. currentMediaData.value = item;
  992. isEdit.value = true;
  993. showMediaModalCpt.value = true;
  994. }
  995. function setScaleInfo({ track, canvasDom, scale = 1 }) {
  996. [1, 2, 3, 4].forEach((devicePixelRatio) => {
  997. track.scaleInfo[devicePixelRatio] = {
  998. scaleX: (1 / devicePixelRatio) * scale,
  999. scaleY: (1 / devicePixelRatio) * scale,
  1000. };
  1001. });
  1002. if (window.devicePixelRatio !== 1) {
  1003. const ratio = (1 / window.devicePixelRatio) * scale;
  1004. canvasDom.scale(ratio);
  1005. track.scaleInfo[window.devicePixelRatio] = {
  1006. scaleX: ratio,
  1007. scaleY: ratio,
  1008. };
  1009. }
  1010. }
  1011. async function addMediaOk(val: AppRootState['allTrack'][0]) {
  1012. if (!audioCtx.value) {
  1013. audioCtx.value = new AudioContext();
  1014. }
  1015. showMediaModalCpt.value = false;
  1016. if (val.type === MediaTypeEnum.screen) {
  1017. const event = await navigator.mediaDevices.getDisplayMedia({
  1018. video: {
  1019. deviceId: val.deviceId,
  1020. // displaySurface: 'monitor', // browser默认标签页;window默认窗口;monitor默认整个屏幕
  1021. },
  1022. audio: true,
  1023. });
  1024. const videoTrack: AppRootState['allTrack'][0] = {
  1025. id: getRandomEnglishString(8),
  1026. audio: 2,
  1027. video: 1,
  1028. mediaName: val.mediaName,
  1029. type: MediaTypeEnum.screen,
  1030. track: event.getVideoTracks()[0],
  1031. trackid: event.getVideoTracks()[0].id,
  1032. stream: event,
  1033. streamid: event.id,
  1034. hidden: false,
  1035. muted: false,
  1036. scaleInfo: {},
  1037. };
  1038. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1039. stream: event,
  1040. id: videoTrack.id,
  1041. });
  1042. console.log(scale, 'scalescale');
  1043. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1044. videoTrack.videoEl = videoEl;
  1045. // @ts-ignore
  1046. videoTrack.canvasDom = canvasDom;
  1047. const audio = event.getAudioTracks();
  1048. if (audio.length) {
  1049. videoTrack.audio = 1;
  1050. videoTrack.volume = normalVolume.value;
  1051. const audioTrack: AppRootState['allTrack'][0] = {
  1052. id: videoTrack.id,
  1053. audio: 1,
  1054. video: 2,
  1055. mediaName: val.mediaName,
  1056. type: MediaTypeEnum.screen,
  1057. track: event.getAudioTracks()[0],
  1058. trackid: event.getAudioTracks()[0].id,
  1059. stream: event,
  1060. streamid: event.id,
  1061. hidden: true,
  1062. muted: false,
  1063. volume: videoTrack.volume,
  1064. scaleInfo: {},
  1065. };
  1066. const res = [...appStore.allTrack, videoTrack, audioTrack];
  1067. appStore.setAllTrack(res);
  1068. resourceCacheStore.setList(res);
  1069. handleMixedAudio();
  1070. // @ts-ignore
  1071. addTrack(videoTrack);
  1072. // @ts-ignore
  1073. addTrack(audioTrack);
  1074. } else {
  1075. const res = [...appStore.allTrack, videoTrack];
  1076. appStore.setAllTrack(res);
  1077. resourceCacheStore.setList(res);
  1078. // @ts-ignore
  1079. addTrack(videoTrack);
  1080. }
  1081. console.log('获取窗口成功');
  1082. } else if (val.type === MediaTypeEnum.camera) {
  1083. const event = await navigator.mediaDevices.getUserMedia({
  1084. video: {
  1085. deviceId: val.deviceId,
  1086. },
  1087. audio: false,
  1088. });
  1089. const videoTrack: AppRootState['allTrack'][0] = {
  1090. id: getRandomEnglishString(8),
  1091. deviceId: val.deviceId,
  1092. audio: 2,
  1093. video: 1,
  1094. mediaName: val.mediaName,
  1095. type: MediaTypeEnum.camera,
  1096. track: event.getVideoTracks()[0],
  1097. trackid: event.getVideoTracks()[0].id,
  1098. stream: event,
  1099. streamid: event.id,
  1100. hidden: false,
  1101. muted: false,
  1102. scaleInfo: {},
  1103. };
  1104. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1105. stream: event,
  1106. id: videoTrack.id,
  1107. });
  1108. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1109. videoTrack.videoEl = videoEl;
  1110. // @ts-ignore
  1111. videoTrack.canvasDom = canvasDom;
  1112. const res = [...appStore.allTrack, videoTrack];
  1113. appStore.setAllTrack(res);
  1114. resourceCacheStore.setList(res);
  1115. // @ts-ignore
  1116. addTrack(videoTrack);
  1117. console.log('获取摄像头成功');
  1118. } else if (val.type === MediaTypeEnum.microphone) {
  1119. const event = await navigator.mediaDevices.getUserMedia({
  1120. video: false,
  1121. audio: { deviceId: val.deviceId },
  1122. });
  1123. const microphoneVideoTrack: AppRootState['allTrack'][0] = {
  1124. id: getRandomEnglishString(8),
  1125. deviceId: val.deviceId,
  1126. audio: 1,
  1127. video: 2,
  1128. mediaName: val.mediaName,
  1129. type: MediaTypeEnum.microphone,
  1130. track: event.getAudioTracks()[0],
  1131. trackid: event.getAudioTracks()[0].id,
  1132. stream: event,
  1133. streamid: event.id,
  1134. hidden: false,
  1135. muted: false,
  1136. volume: 60,
  1137. scaleInfo: {},
  1138. };
  1139. const videoEl = createVideo({ appendChild: true, muted: false });
  1140. videoEl.srcObject = event;
  1141. microphoneVideoTrack.videoEl = videoEl;
  1142. const res = [...appStore.allTrack, microphoneVideoTrack];
  1143. appStore.setAllTrack(res);
  1144. resourceCacheStore.setList(res);
  1145. handleMixedAudio();
  1146. // @ts-ignore
  1147. addTrack(microphoneVideoTrack);
  1148. console.log('获取麦克风成功');
  1149. } else if (val.type === MediaTypeEnum.txt) {
  1150. const txtTrack: AppRootState['allTrack'][0] = {
  1151. id: getRandomEnglishString(8),
  1152. audio: 2,
  1153. video: 1,
  1154. mediaName: val.mediaName,
  1155. type: MediaTypeEnum.txt,
  1156. track: undefined,
  1157. trackid: undefined,
  1158. stream: undefined,
  1159. streamid: undefined,
  1160. hidden: false,
  1161. muted: false,
  1162. scaleInfo: {},
  1163. };
  1164. if (fabricCanvas.value) {
  1165. console.log('val.txtInfo?.txt ', val.txtInfo?.txt);
  1166. const canvasDom = markRaw(
  1167. new fabric.Text(val.txtInfo?.txt || '', {
  1168. top: 0,
  1169. left: 0,
  1170. fill: val.txtInfo?.color,
  1171. })
  1172. );
  1173. handleMoving({ canvasDom, id: txtTrack.id });
  1174. handleScaling({ canvasDom, id: txtTrack.id });
  1175. txtTrack.txtInfo = val.txtInfo;
  1176. if (window.devicePixelRatio !== 1) {
  1177. const ratio = 1 / window.devicePixelRatio;
  1178. canvasDom.scale(ratio);
  1179. txtTrack.scaleInfo[window.devicePixelRatio] = {
  1180. scaleX: ratio,
  1181. scaleY: ratio,
  1182. };
  1183. } else {
  1184. txtTrack.scaleInfo[window.devicePixelRatio] = { scaleX: 1, scaleY: 1 };
  1185. }
  1186. // @ts-ignore
  1187. txtTrack.canvasDom = canvasDom;
  1188. fabricCanvas.value.add(canvasDom);
  1189. }
  1190. const res = [...appStore.allTrack, txtTrack];
  1191. // @ts-ignore
  1192. appStore.setAllTrack(res);
  1193. // @ts-ignore
  1194. resourceCacheStore.setList(res);
  1195. // @ts-ignore
  1196. addTrack(txtTrack);
  1197. console.log('获取文字成功', fabricCanvas.value);
  1198. } else if (val.type === MediaTypeEnum.time) {
  1199. const timeTrack: AppRootState['allTrack'][0] = {
  1200. id: getRandomEnglishString(8),
  1201. audio: 2,
  1202. video: 1,
  1203. mediaName: val.mediaName,
  1204. type: MediaTypeEnum.time,
  1205. track: undefined,
  1206. trackid: undefined,
  1207. stream: undefined,
  1208. streamid: undefined,
  1209. hidden: false,
  1210. muted: false,
  1211. scaleInfo: {},
  1212. };
  1213. if (fabricCanvas.value) {
  1214. const canvasDom = markRaw(
  1215. new fabric.Text(new Date().toLocaleString(), {
  1216. top: 0,
  1217. left: 0,
  1218. fill: val.timeInfo?.color,
  1219. })
  1220. );
  1221. setScaleInfo({ canvasDom, track: timeTrack });
  1222. timeCanvasDom.value.push(canvasDom);
  1223. handleMoving({ canvasDom, id: timeTrack.id });
  1224. handleScaling({ canvasDom, id: timeTrack.id });
  1225. timeTrack.timeInfo = val.timeInfo;
  1226. // @ts-ignore
  1227. timeTrack.canvasDom = canvasDom;
  1228. fabricCanvas.value.add(canvasDom);
  1229. }
  1230. const res = [...appStore.allTrack, timeTrack];
  1231. // @ts-ignore
  1232. appStore.setAllTrack(res);
  1233. // @ts-ignore
  1234. resourceCacheStore.setList(res);
  1235. // @ts-ignore
  1236. addTrack(timeTrack);
  1237. console.log('获取时间成功', fabricCanvas.value);
  1238. } else if (val.type === MediaTypeEnum.stopwatch) {
  1239. const stopwatchTrack: AppRootState['allTrack'][0] = {
  1240. id: getRandomEnglishString(8),
  1241. audio: 2,
  1242. video: 1,
  1243. mediaName: val.mediaName,
  1244. type: MediaTypeEnum.stopwatch,
  1245. track: undefined,
  1246. trackid: undefined,
  1247. stream: undefined,
  1248. streamid: undefined,
  1249. hidden: false,
  1250. muted: false,
  1251. scaleInfo: {},
  1252. };
  1253. if (fabricCanvas.value) {
  1254. const canvasDom = markRaw(
  1255. new fabric.Text('00天00时00分00秒000毫秒', {
  1256. top: 0,
  1257. left: 0,
  1258. fill: val.stopwatchInfo?.color,
  1259. // editable: true,
  1260. })
  1261. );
  1262. setScaleInfo({ canvasDom, track: stopwatchTrack });
  1263. stopwatchCanvasDom.value.push(canvasDom);
  1264. handleMoving({ canvasDom, id: stopwatchTrack.id });
  1265. handleScaling({ canvasDom, id: stopwatchTrack.id });
  1266. stopwatchTrack.stopwatchInfo = val.stopwatchInfo;
  1267. // @ts-ignore
  1268. stopwatchTrack.canvasDom = canvasDom;
  1269. fabricCanvas.value.add(canvasDom);
  1270. }
  1271. const res = [...appStore.allTrack, stopwatchTrack];
  1272. // @ts-ignore
  1273. appStore.setAllTrack(res);
  1274. // @ts-ignore
  1275. resourceCacheStore.setList(res);
  1276. // @ts-ignore
  1277. addTrack(stopwatchTrack);
  1278. console.log('获取秒表成功', fabricCanvas.value);
  1279. } else if (val.type === MediaTypeEnum.img) {
  1280. const imgTrack: AppRootState['allTrack'][0] = {
  1281. id: getRandomEnglishString(8),
  1282. audio: 2,
  1283. video: 1,
  1284. mediaName: val.mediaName,
  1285. type: MediaTypeEnum.img,
  1286. track: undefined,
  1287. trackid: undefined,
  1288. stream: undefined,
  1289. streamid: undefined,
  1290. hidden: false,
  1291. muted: false,
  1292. scaleInfo: {},
  1293. };
  1294. if (fabricCanvas.value) {
  1295. if (!val.imgInfo) return;
  1296. const file = val.imgInfo[0].file!;
  1297. const { code } = await saveFile({ file, fileName: imgTrack.id });
  1298. if (code !== 1) return;
  1299. const imgEl = await new Promise<HTMLImageElement>((resolve) => {
  1300. const reader = new FileReader();
  1301. reader.addEventListener(
  1302. 'load',
  1303. function () {
  1304. const img = document.createElement('img');
  1305. img.src = reader.result as string;
  1306. img.onload = () => {
  1307. resolve(img);
  1308. };
  1309. },
  1310. false
  1311. );
  1312. if (file) {
  1313. reader.readAsDataURL(file);
  1314. }
  1315. });
  1316. const canvasDom = markRaw(
  1317. new fabric.Image(imgEl, {
  1318. top: 0,
  1319. left: 0,
  1320. width: imgEl.width,
  1321. height: imgEl.height,
  1322. })
  1323. );
  1324. const scale = handleScale({ width: imgEl.width, height: imgEl.height });
  1325. setScaleInfo({ canvasDom, track: imgTrack, scale });
  1326. handleMoving({ canvasDom, id: imgTrack.id });
  1327. handleScaling({ canvasDom, id: imgTrack.id });
  1328. // @ts-ignore
  1329. imgTrack.canvasDom = canvasDom;
  1330. fabricCanvas.value.add(canvasDom);
  1331. }
  1332. const res = [...appStore.allTrack, imgTrack];
  1333. // @ts-ignore
  1334. appStore.setAllTrack(res);
  1335. // @ts-ignore
  1336. resourceCacheStore.setList(res);
  1337. // @ts-ignore
  1338. addTrack(imgTrack);
  1339. console.log('获取图片成功', fabricCanvas.value);
  1340. } else if (val.type === MediaTypeEnum.media) {
  1341. const mediaVideoTrack: AppRootState['allTrack'][0] = {
  1342. id: getRandomEnglishString(8),
  1343. audio: 2,
  1344. video: 1,
  1345. mediaName: val.mediaName,
  1346. type: MediaTypeEnum.media,
  1347. track: undefined,
  1348. trackid: undefined,
  1349. stream: undefined,
  1350. streamid: undefined,
  1351. hidden: false,
  1352. muted: false,
  1353. scaleInfo: {},
  1354. };
  1355. if (fabricCanvas.value) {
  1356. if (!val.mediaInfo) return;
  1357. const file = val.mediaInfo[0].file!;
  1358. const { code } = await saveFile({ file, fileName: mediaVideoTrack.id });
  1359. if (code !== 1) return;
  1360. const url = URL.createObjectURL(file);
  1361. const videoEl = createVideo({ muted: false, appendChild: true });
  1362. videoEl.src = url;
  1363. videoEl.muted = false;
  1364. bodyAppendChildElArr.value.push(videoEl);
  1365. const videoRes = await new Promise<HTMLVideoElement>((resolve) => {
  1366. videoEl.onloadedmetadata = () => {
  1367. resolve(videoEl);
  1368. };
  1369. });
  1370. // @ts-ignore
  1371. const stream = videoRes.captureStream();
  1372. const { canvasDom, scale } = await autoCreateVideo({
  1373. stream,
  1374. id: mediaVideoTrack.id,
  1375. });
  1376. setScaleInfo({ canvasDom, track: mediaVideoTrack, scale });
  1377. mediaVideoTrack.videoEl = videoEl;
  1378. // @ts-ignore
  1379. mediaVideoTrack.canvasDom = canvasDom;
  1380. if (stream.getAudioTracks()[0]) {
  1381. console.log('视频有音频', stream.getAudioTracks()[0]);
  1382. mediaVideoTrack.audio = 1;
  1383. mediaVideoTrack.volume = normalVolume.value;
  1384. const audioTrack: AppRootState['allTrack'][0] = {
  1385. id: mediaVideoTrack.id,
  1386. audio: 1,
  1387. video: 2,
  1388. mediaName: val.mediaName,
  1389. type: MediaTypeEnum.media,
  1390. track: stream.getAudioTracks()[0],
  1391. trackid: stream.getAudioTracks()[0].id,
  1392. stream,
  1393. streamid: stream.id,
  1394. hidden: true,
  1395. muted: false,
  1396. volume: mediaVideoTrack.volume,
  1397. scaleInfo: {},
  1398. };
  1399. // @ts-ignore
  1400. const res = [...appStore.allTrack, audioTrack];
  1401. appStore.setAllTrack(res);
  1402. resourceCacheStore.setList(res);
  1403. handleMixedAudio();
  1404. // @ts-ignore
  1405. addTrack(audioTrack);
  1406. }
  1407. }
  1408. const res = [...appStore.allTrack, mediaVideoTrack];
  1409. // @ts-ignore
  1410. appStore.setAllTrack(res);
  1411. // @ts-ignore
  1412. resourceCacheStore.setList(res);
  1413. // @ts-ignore
  1414. addTrack(mediaVideoTrack);
  1415. console.log('获取视频成功', fabricCanvas.value);
  1416. }
  1417. }
  1418. function editMediaOk(val: AppRootState['allTrack'][0]) {
  1419. showMediaModalCpt.value = false;
  1420. const res = appStore.allTrack.map((item) => {
  1421. if (item.id === val.id) {
  1422. item.mediaName = val.mediaName;
  1423. item.timeInfo = val.timeInfo;
  1424. item.stopwatchInfo = val.stopwatchInfo;
  1425. item.txtInfo = val.txtInfo;
  1426. if (
  1427. [
  1428. MediaTypeEnum.txt,
  1429. MediaTypeEnum.time,
  1430. MediaTypeEnum.stopwatch,
  1431. ].includes(val.type)
  1432. ) {
  1433. if (item.canvasDom) {
  1434. // @ts-ignore
  1435. item.canvasDom.set(
  1436. 'fill',
  1437. val.txtInfo?.color ||
  1438. val.timeInfo?.color ||
  1439. val.stopwatchInfo?.color
  1440. );
  1441. }
  1442. }
  1443. }
  1444. return item;
  1445. });
  1446. appStore.setAllTrack(res);
  1447. resourceCacheStore.setList(res);
  1448. }
  1449. function updateVolume(item: AppRootState['allTrack'][0], v) {
  1450. console.log(item, item.volume, v);
  1451. const res = appStore.allTrack.map((iten) => {
  1452. if (iten.id === item.id) {
  1453. if (item.volume !== undefined) {
  1454. iten.volume = v;
  1455. if (iten.videoEl) {
  1456. iten.videoEl.volume = v / 100;
  1457. }
  1458. }
  1459. }
  1460. return iten;
  1461. });
  1462. appStore.setAllTrack(res);
  1463. resourceCacheStore.setList(res);
  1464. }
  1465. function handleChangeMuted(item: AppRootState['allTrack'][0]) {
  1466. if (item.videoEl) {
  1467. const res = !item.videoEl.muted;
  1468. item.videoEl.muted = res;
  1469. item.muted = res;
  1470. resourceCacheStore.setList(appStore.allTrack);
  1471. }
  1472. }
  1473. function handleDel(item: AppRootState['allTrack'][0]) {
  1474. console.log('handleDel', item);
  1475. if (item.canvasDom !== undefined) {
  1476. // @ts-ignore
  1477. fabricCanvas.value?.remove(item.canvasDom);
  1478. item.videoEl?.remove();
  1479. }
  1480. const res = appStore.allTrack.filter((iten) => iten.id !== item.id);
  1481. appStore.setAllTrack(res);
  1482. resourceCacheStore.setList(res);
  1483. delTrack(item);
  1484. }
  1485. function handleStartMedia(item: { type: MediaTypeEnum; txt: string }) {
  1486. currentMediaType.value = item.type;
  1487. showMediaModalCpt.value = true;
  1488. }
  1489. </script>
  1490. <style lang="scss" scoped>
  1491. .slider {
  1492. width: 80px;
  1493. }
  1494. .push-wrap {
  1495. display: flex;
  1496. justify-content: space-between;
  1497. margin: 15px auto 0;
  1498. width: $w-1250;
  1499. .left {
  1500. position: relative;
  1501. display: inline-block;
  1502. overflow: hidden;
  1503. box-sizing: border-box;
  1504. width: $w-960;
  1505. height: 100%;
  1506. border-radius: 6px;
  1507. background-color: white;
  1508. color: #9499a0;
  1509. vertical-align: top;
  1510. .container {
  1511. position: relative;
  1512. overflow: hidden;
  1513. height: 100%;
  1514. background-color: rgba($color: #000000, $alpha: 0.5);
  1515. line-height: 0;
  1516. .add-wrap {
  1517. position: absolute;
  1518. top: 50%;
  1519. left: 50%;
  1520. display: flex;
  1521. align-items: center;
  1522. justify-content: space-around;
  1523. padding: 10px 20px;
  1524. width: 50%;
  1525. border-radius: 6px;
  1526. background-color: white;
  1527. transform: translate(-50%, -50%);
  1528. }
  1529. }
  1530. .room-control {
  1531. display: flex;
  1532. justify-content: space-between;
  1533. padding: 20px;
  1534. background-color: papayawhip;
  1535. .info {
  1536. display: flex;
  1537. align-items: center;
  1538. .avatar {
  1539. margin-right: 20px;
  1540. width: 55px;
  1541. height: 55px;
  1542. border-radius: 50%;
  1543. background-position: center center;
  1544. background-size: cover;
  1545. background-repeat: no-repeat;
  1546. }
  1547. .detail {
  1548. display: flex;
  1549. flex-direction: column;
  1550. flex-shrink: 0;
  1551. width: 200px;
  1552. text-align: initial;
  1553. .top {
  1554. margin-bottom: 10px;
  1555. color: #18191c;
  1556. }
  1557. .bottom {
  1558. font-size: 14px;
  1559. }
  1560. }
  1561. }
  1562. .rtc {
  1563. display: flex;
  1564. align-items: center;
  1565. flex: 1;
  1566. font-size: 14px;
  1567. .item {
  1568. display: flex;
  1569. align-items: center;
  1570. flex: 1;
  1571. .txt {
  1572. flex-shrink: 0;
  1573. width: 80px;
  1574. }
  1575. .down {
  1576. width: 90px;
  1577. user-select: none;
  1578. }
  1579. }
  1580. }
  1581. .other {
  1582. display: flex;
  1583. flex-direction: column;
  1584. justify-content: center;
  1585. font-size: 12px;
  1586. .top {
  1587. }
  1588. .bottom {
  1589. margin-top: 10px;
  1590. }
  1591. }
  1592. }
  1593. }
  1594. .right {
  1595. display: flex;
  1596. flex-direction: column;
  1597. justify-content: space-between;
  1598. box-sizing: border-box;
  1599. margin-left: 10px;
  1600. width: $w-250;
  1601. border-radius: 6px;
  1602. background-color: white;
  1603. color: #9499a0;
  1604. .resource-card {
  1605. position: relative;
  1606. box-sizing: border-box;
  1607. margin-bottom: 10px;
  1608. padding: 10px;
  1609. width: 100%;
  1610. height: 290px;
  1611. border-radius: 6px;
  1612. background-color: papayawhip;
  1613. .title {
  1614. text-align: initial;
  1615. }
  1616. .item {
  1617. display: flex;
  1618. align-items: center;
  1619. justify-content: space-between;
  1620. margin: 5px 0;
  1621. font-size: 14px;
  1622. // &:hover {
  1623. // .control {
  1624. // display: flex;
  1625. // align-items: center;
  1626. // }
  1627. // }
  1628. .control {
  1629. display: flex;
  1630. align-items: center;
  1631. .control-item {
  1632. cursor: pointer;
  1633. &:not(:last-child) {
  1634. margin-right: 6px;
  1635. }
  1636. }
  1637. }
  1638. }
  1639. .bottom {
  1640. position: absolute;
  1641. bottom: 0;
  1642. left: 0;
  1643. padding: 10px;
  1644. }
  1645. }
  1646. .danmu-card {
  1647. position: relative;
  1648. flex: 1;
  1649. box-sizing: border-box;
  1650. padding: 10px;
  1651. width: 100%;
  1652. border-radius: 6px;
  1653. background-color: papayawhip;
  1654. text-align: initial;
  1655. .title {
  1656. margin-bottom: 10px;
  1657. }
  1658. .list {
  1659. overflow: scroll;
  1660. height: 360px;
  1661. @extend %hideScrollbar;
  1662. .item {
  1663. margin-bottom: 10px;
  1664. font-size: 12px;
  1665. .name {
  1666. color: #9499a0;
  1667. }
  1668. .msg {
  1669. color: #61666d;
  1670. }
  1671. }
  1672. }
  1673. .send-msg {
  1674. position: absolute;
  1675. bottom: 10px;
  1676. left: 50%;
  1677. display: flex;
  1678. align-items: center;
  1679. box-sizing: border-box;
  1680. width: calc(100% - 20px);
  1681. transform: translateX(-50%);
  1682. .ipt {
  1683. display: block;
  1684. box-sizing: border-box;
  1685. margin: 0 auto;
  1686. margin-right: 10px;
  1687. padding: 10px;
  1688. width: 80%;
  1689. height: 30px;
  1690. outline: none;
  1691. border: 1px solid hsla(0, 0%, 60%, 0.2);
  1692. border-radius: 4px;
  1693. background-color: #f1f2f3;
  1694. font-size: 14px;
  1695. }
  1696. }
  1697. }
  1698. }
  1699. }
  1700. // 屏幕宽度大于1500的时候
  1701. @media screen and (min-width: $w-1500) {
  1702. .push-wrap {
  1703. width: $w-1475;
  1704. .left {
  1705. width: $w-1150;
  1706. }
  1707. .right {
  1708. width: $w-300;
  1709. }
  1710. }
  1711. }
  1712. </style>