index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // @ts-nocheck
  2. // #ifndef UNI-APP-X && APP
  3. import type { ComponentInternalInstance } from '@/uni_modules/lime-shared/vue'
  4. import { getRect } from '@/uni_modules/lime-shared/getRect'
  5. import { canIUseCanvas2d } from '@/uni_modules/lime-shared/canIUseCanvas2d'
  6. export const isCanvas2d = canIUseCanvas2d()
  7. // #endif
  8. export function createCanvas(canvasId : string, component : ComponentInternalInstance) {
  9. // #ifdef UNI-APP-X
  10. uni.createCanvasContextAsync({
  11. canvasId,
  12. component,
  13. success(context : CanvasContext) {
  14. },
  15. fail(error : UniError) {
  16. }
  17. })
  18. // #endif
  19. // #ifndef UNI-APP-X
  20. const isCanvas2d = canIUseCanvas2d()
  21. getRect('#' + canvasId, context, isCanvas2d).then(res => {
  22. if (res.node) {
  23. res.node.width = res.width
  24. res.node.height = res.height
  25. return res.node
  26. } else {
  27. const ctx = uni.createCanvasContext(canvasId, context)
  28. if (!ctx._drawImage) {
  29. ctx._drawImage = ctx.drawImage
  30. ctx.drawImage = function (...args) {
  31. const { path } = args.shift()
  32. ctx._drawImage(path, ...args)
  33. }
  34. }
  35. if (!ctx.getImageData) {
  36. ctx.getImageData = function () {
  37. return new Promise((resolve, reject) => {
  38. uni.canvasGetImageData({
  39. canvasId,
  40. x: parseInt(arguments[0]),
  41. y: parseInt(arguments[1]),
  42. width: parseInt(arguments[2]),
  43. height: parseInt(arguments[3]),
  44. success(res) {
  45. resolve(res)
  46. },
  47. fail(err) {
  48. reject(err)
  49. }
  50. }, context)
  51. })
  52. }
  53. return {
  54. getContext(type: string) {
  55. if(type == '2d') {
  56. return ctx
  57. }
  58. },
  59. width: res.width,
  60. height: res.height,
  61. createImage
  62. }
  63. }
  64. }
  65. })
  66. // #endif
  67. }