context.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. export const uniContext = (ctx) => {
  2. ctx.uniDrawImage = ctx.drawImage
  3. ctx.drawImage = (image,...agrs) => {
  4. ctx.uniDrawImage(image.src, ...agrs)
  5. }
  6. return ctx
  7. }
  8. class Image {
  9. constructor() {
  10. this.currentSrc = null
  11. this.naturalHeight = 0
  12. this.naturalWidth = 0
  13. this.width = 0
  14. this.height = 0
  15. this.tagName = 'IMG'
  16. }
  17. set src(src) {
  18. this.currentSrc = src
  19. uni.getImageInfo({
  20. src,
  21. success: (res) => {
  22. this.naturalWidth = this.width = res.width
  23. this.naturalHeight = this.height = res.height
  24. this.onload()
  25. },
  26. fail: () => {
  27. this.onerror()
  28. }
  29. })
  30. }
  31. get src() {
  32. return this.currentSrc
  33. }
  34. }
  35. export const createImage = () => {
  36. return new Image()
  37. }
  38. export function useCurrentPage() {
  39. const pages = getCurrentPages();
  40. return pages[pages.length - 1];
  41. }
  42. export const toDataURL = (canvasId, context, options = {}) => {
  43. // #ifdef MP-QQ
  44. // context = context.$scope
  45. // #endif
  46. // #ifdef MP-ALIPAY
  47. context = ''
  48. // #endif
  49. return new Promise((resolve, reject) => {
  50. uni.canvasToTempFilePath({
  51. ...options,
  52. canvasId,
  53. success: (res) => {
  54. resolve(res.tempFilePath)
  55. },
  56. fail: (err) => {
  57. reject(err)
  58. }
  59. }, context)
  60. })
  61. }