w-compress.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <view class="compress__canvas">
  3. <!-- #ifndef H5 -->
  4. <canvas canvas-id="compress_canvas" :style="{width: width + 'px', height: height + 'px'}"></canvas>
  5. <!-- #endif -->
  6. </view>
  7. </template>
  8. <script>
  9. /**
  10. * 使用方法
  11. * import WCompress from '@/components/w-compress/compress.js'
  12. * components: { WCompress }
  13. * <w-compress ref='wCompress' />
  14. * this.$refs.wCompress.start(res.tempFilePaths[0]).then().catch()
  15. * this.$refs.wCompress.start(res.tempFilePaths).then().catch()
  16. */
  17. import compress from './compress.js'
  18. export default {
  19. name: 'wCompress',
  20. data() {
  21. return {
  22. width: 0,
  23. height: 0
  24. }
  25. },
  26. methods: {
  27. start(imgUrl, options={}) {
  28. return new Promise(async (resolve, reject) => {
  29. if(imgUrl instanceof Array) {
  30. try{
  31. let arr = []
  32. for(let i=0; i<imgUrl.length; i++) {
  33. let url = await compress(imgUrl[i], this, options)
  34. arr.push(url)
  35. }
  36. resolve(arr)
  37. }catch(e){
  38. reject(e)
  39. }
  40. /* let arr = []
  41. arr = imgUrl.map(async c => {
  42. return await compress(c, this, options)
  43. })
  44. resolve(arr) */
  45. /* let arr = imgUrl.map(c => {
  46. return compress(c, this, options)
  47. })
  48. Promise.all(arr)
  49. .then(resolve)
  50. .catch(reject) */
  51. } else {
  52. compress(imgUrl, this, options)
  53. .then(resolve)
  54. .catch(reject)
  55. }
  56. })
  57. }
  58. }
  59. }
  60. </script>
  61. <style>
  62. .compress__canvas {
  63. position: absolute;
  64. left: 10000px;
  65. visibility: hidden;
  66. height: 0;
  67. overflow: hidden;
  68. }
  69. </style>