index.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { getRangeRandom } from 'billd-utils';
  2. export const sum = (a, b) => {
  3. return a + b;
  4. };
  5. /**
  6. * @description 获取随机字符串(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
  7. * @example: getRandomString(4) ===> abd3
  8. * @param {number} length
  9. * @return {*}
  10. */
  11. export const getRandomString = (length: number): string => {
  12. const str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  13. let res = '';
  14. for (let i = 0; i < length; i += 1) {
  15. res += str.charAt(getRangeRandom(0, str.length - 1));
  16. }
  17. return res;
  18. };
  19. export function videoToCanvas(data: {
  20. videoEl: HTMLVideoElement;
  21. targetEl: Element;
  22. width: number;
  23. height: number;
  24. }) {
  25. const { videoEl, targetEl, width, height } = data;
  26. if (!videoEl || !targetEl) {
  27. return;
  28. }
  29. const canvas = document.createElement('canvas');
  30. canvas.width = width;
  31. canvas.height = height;
  32. const ctx = canvas.getContext('2d')!;
  33. let timer;
  34. function drawCanvas() {
  35. ctx.drawImage(videoEl, 0, 0, width, height);
  36. timer = requestAnimationFrame(drawCanvas);
  37. }
  38. function stopDrawing() {
  39. cancelAnimationFrame(timer);
  40. }
  41. targetEl.appendChild(canvas);
  42. // document.body.appendChild(videoEl);
  43. // targetEl.parentNode?.replaceChild(canvas, targetEl);
  44. drawCanvas();
  45. return { drawCanvas, stopDrawing };
  46. }