index.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <div
  3. class="svga-container"
  4. ref="svga-container"
  5. v-bind="$props"
  6. v-on="$listeners"
  7. >
  8. <canvas ref="svga-canvas"></canvas>
  9. </div>
  10. </template>
  11. <script>
  12. import { Parser, Player } from 'svga'
  13. import { getPureNumber } from '@/utils'
  14. export default {
  15. name: 'AdSvga',
  16. props: {
  17. // 文件地址
  18. svgaLink: {
  19. required: true,
  20. type: String
  21. }
  22. },
  23. mounted() {
  24. console.log('svag moi')
  25. try {
  26. this.init()
  27. } catch (e) {
  28. this.$emit('error')
  29. }
  30. },
  31. methods: {
  32. async init() {
  33. const parser = new Parser()
  34. const svga = await parser.load(this.svgaLink)
  35. const player = new Player(this.$refs['svga-canvas'])
  36. const scaleX =
  37. getPureNumber(this.$refs['svga-container'].style.width) /
  38. svga.size.width
  39. const scaleY =
  40. getPureNumber(this.$refs['svga-container'].style.height) /
  41. svga.size.height
  42. const scaleRatio = Number((scaleX > scaleY ? scaleX : scaleY).toFixed(3))
  43. this.$refs['svga-canvas'].style[
  44. '-webkit-transform'
  45. ] = `scale(${scaleRatio}, ${scaleRatio})`
  46. this.$refs[
  47. 'svga-canvas'
  48. ].style.transform = `scale(${scaleRatio}, ${scaleRatio})`
  49. await player.mount(svga)
  50. player.start()
  51. this.$emit('load')
  52. }
  53. }
  54. }
  55. </script>
  56. <style lang="scss" scoped>
  57. .svga-container {
  58. width: 100%;
  59. height: 100%;
  60. canvas {
  61. transform-origin: 0 0;
  62. }
  63. }
  64. </style>