getCanvas.ts 894 B

12345678910111213141516171819202122232425262728293031
  1. // @ts-nocheck
  2. import type { ComponentInternalInstance } from './vue'
  3. import { getRect } from '@/uni_modules/lime-shared/getRect'
  4. import { canIUseCanvas2d } from '@/uni_modules/lime-shared/canIUseCanvas2d'
  5. export let isCanvas2d = canIUseCanvas2d()
  6. export async function getCanvas(canvasId: string, options: {context: ComponentInternalInstance}) {
  7. let { context } = options
  8. // #ifdef MP || VUE2
  9. if (context.proxy) context = context.proxy
  10. // #endif
  11. return getRect('#' + canvasId, {context, type: isCanvas2d ? 'fields': 'boundingClientRect'}).then(({node}) => {
  12. if(node){
  13. return node
  14. } else {
  15. isCanvas2d = false
  16. const ctx = uni.createCanvasContext(canvasId, context)
  17. return {
  18. getContext(type: string) {
  19. if(type == '2d') {
  20. return ctx
  21. }
  22. },
  23. }
  24. // #ifdef H5
  25. // canvas.value = context.proxy.$el.querySelector('#'+ canvasId)
  26. // #endif
  27. }
  28. })
  29. }