index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view class="container">
  3. <page-title>图片预览</page-title>
  4. <view class="main-canvas">
  5. <canvas :style="{width:canvasWidth,height:canvasHeight}" canvas-id="myCanvas"></canvas>
  6. <image :src='waterMark'></image>
  7. </view>
  8. </view>
  9. </template>
  10. <script setup>
  11. import {
  12. ref
  13. } from 'vue'
  14. import {
  15. onLoad,
  16. onUnload
  17. } from "@dcloudio/uni-app"
  18. let canvasWidth = ref(0);
  19. let canvasHeight = ref(0);
  20. let waterMark = ref('')
  21. function createCanvas(options) {
  22. let width = 0;
  23. let height = 0;
  24. uni.getSystemInfo({
  25. success: res => {
  26. width = res.windowWidth * 0.92
  27. }
  28. })
  29. uni.getImageInfo({
  30. src: options.url,
  31. success: res => {
  32. height = res.height / res.width * width;
  33. canvasWidth.value = width + 'px';
  34. canvasHeight.value = height + 'px';
  35. const ctx = uni.createCanvasContext('myCanvas');
  36. ctx.draw(false, () => {
  37. setTimeout(() => {
  38. ctx.drawImage(res.path, 0, 0, width, height);
  39. ctx.clearRect(0, 0, width, height);
  40. ctx.textAlign = 'left';
  41. ctx.textBaseline = 'top';
  42. ctx.font = '12px Microsoft Yahei'
  43. ctx.fillStyle = 'rgba(255, 255, 255, 0.5)'
  44. ctx.fillText(options.time, width - 140, height - 20, res.width)
  45. uni.canvasToTempFilePath({
  46. canvasId: "myCanvas",
  47. success: res => {
  48. // let p = plus.io.convertLocalFileSystemURL(res.tempFilePath);
  49. // console.log(p);
  50. waterMark.value = 'file:///' + p
  51. }
  52. })
  53. }, 500)
  54. });
  55. }
  56. })
  57. }
  58. onLoad(options => {
  59. createCanvas(options)
  60. })
  61. onUnload(() => {})
  62. </script>
  63. <style lang="scss" scoped>
  64. .main-canvas {
  65. position: absolute;
  66. top: 14%;
  67. left: 4%;
  68. width: 92%;
  69. height: 86%;
  70. }
  71. </style>