aboutsummaryrefslogtreecommitdiff
path: root/src/components/still-image/still-image.js
blob: 56fd2fd98b212a56142b56269452c694326b447b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const StillImage = {
  props: [
    'src',
    'referrerpolicy',
    'mimetype',
    'imageLoadError',
    'imageLoadHandler',
    'alt',
    'height',
    'width',
    'dataSrc',
    'loading'
  ],
  data () {
    return {
      // for lazy loading, see loadLazy()
      realSrc: this.src,
      stopGifs: this.$store.getters.mergedConfig.stopGifs
    }
  },
  computed: {
    animated () {
      if (!this.realSrc) {
        return false
      }

      return this.stopGifs && (this.mimetype === 'image/gif' || this.realSrc.endsWith('.gif'))
    },
    style () {
      const appendPx = (str) => /\d$/.test(str) ? str + 'px' : str
      return {
        height: this.height ? appendPx(this.height) : null,
        width: this.width ? appendPx(this.width) : null
      }
    }
  },
  methods: {
    loadLazy () {
      if (this.dataSrc) {
        this.realSrc = this.dataSrc
      }
    },
    onLoad () {
      if (!this.realSrc) {
        return
      }
      const image = this.$refs.src
      if (!image) return
      this.imageLoadHandler && this.imageLoadHandler(image)
      const canvas = this.$refs.canvas
      if (!canvas) return
      const width = image.naturalWidth
      const height = image.naturalHeight
      canvas.width = width
      canvas.height = height
      canvas.getContext('2d').drawImage(image, 0, 0, width, height)
    },
    onError () {
      this.imageLoadError && this.imageLoadError()
    }
  },
  watch: {
    src () {
      this.realSrc = this.src
    },
    dataSrc () {
      this.$el.removeAttribute('data-loaded')
    }
  }
}

export default StillImage