You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

superpose-img.vue 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!--
  2. Copyright 2020-2021 Huawei Technologies Co., Ltd.All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. -->
  13. <template>
  14. <div class="cl-superpose-image"
  15. :style="{'height': `${containerSize}px`,'width': `${containerSize}px`}">
  16. <img :src="backgroundImg"
  17. class="second-level"
  18. v-show="ifSuperpose"
  19. v-if="backgroundReady"
  20. @error="backgroundError()"
  21. :style="{'position': 'absolute', 'top':`${backTop}px`,'left':`${backLeft}px`,
  22. 'height': `${imageHeight}px`, 'width':`${imageWidth}px`}">
  23. <img v-if="targetReady"
  24. :src="targetImg"
  25. :style="{'position': 'absolute', 'top': `${targetTop}px`, 'left':`${targetLeft}px`,
  26. 'height': `${imageHeight}px`, 'width':`${imageWidth}px`}"
  27. class="first-level"
  28. :class="!ifSuperpose?'overlay-background':''"
  29. @error="targetError()">
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. props: [
  35. 'backgroundImg', // The background at the second level
  36. 'targetImg', // The target image at the first level
  37. 'ifSuperpose', // If show the canvas of background
  38. 'containerSize', // The width and height of container
  39. ],
  40. data() {
  41. return {
  42. targetReady: true, // The state of target image
  43. backgroundReady: true, // The state of background image
  44. targetTop: 0, // top of target
  45. targetLeft: 0, // left of target
  46. imageHeight: 0, // height of image
  47. imageWidth: 0, // width of image
  48. backTop: 0, // top of background
  49. backLeft: 0, // left of background
  50. };
  51. },
  52. created() {
  53. this.calImageSize();
  54. },
  55. methods: {
  56. /**
  57. * The logic of cal image size
  58. */
  59. calImageSize() {
  60. const backgroundTemp = new Image();
  61. backgroundTemp.src = this.backgroundImg;
  62. backgroundTemp.onload = () => {
  63. if (backgroundTemp.width > backgroundTemp.height) {
  64. this.imageWidth = this.containerSize;
  65. this.imageHeight =
  66. this.containerSize * (backgroundTemp.height / backgroundTemp.width);
  67. this.backTop = this.containerSize / 2 - this.imageHeight / 2;
  68. this.targetTop = this.backTop;
  69. } else if (backgroundTemp.width < backgroundTemp.height) {
  70. this.imageHeight = this.containerSize;
  71. this.imageWidth =
  72. this.containerSize * (backgroundTemp.width / backgroundTemp.height);
  73. this.backLeft = this.containerSize / 2 - this.imageWidth / 2;
  74. this.targetLeft = this.backLeft;
  75. } else {
  76. this.imageWidth = this.containerSize;
  77. this.imageHeight = this.containerSize;
  78. }
  79. };
  80. },
  81. /**
  82. * The logic that is executed when target image loading failed
  83. */
  84. targetError() {
  85. this.targetReady = false;
  86. },
  87. /**
  88. * The logic that is executed when background image loading failed
  89. */
  90. backgroundError() {
  91. this.backgroundReady = false;
  92. },
  93. },
  94. };
  95. </script>
  96. <style scoped>
  97. .cl-superpose-image {
  98. position: relative;
  99. overflow: hidden;
  100. }
  101. .cl-superpose-image .overlay-background {
  102. background: #371956;
  103. }
  104. </style>