canvas.uvue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <cl-page>
  3. <cl-canvas
  4. v-if="width > 0"
  5. ref="canvasRef"
  6. canvas-id="test"
  7. :height="height"
  8. :width="width"
  9. @load="onCanvasLoad"
  10. ></cl-canvas>
  11. <cl-footer>
  12. <!-- #ifdef H5 -->
  13. <cl-button type="primary" @click="previewImage">预览图片</cl-button>
  14. <!-- #endif -->
  15. <!-- #ifndef H5 -->
  16. <cl-button type="primary" @click="saveImage">保存图片</cl-button>
  17. <!-- #endif -->
  18. </cl-footer>
  19. </cl-page>
  20. </template>
  21. <script lang="ts" setup>
  22. import { Canvas } from "@/uni_modules/cool-canvas";
  23. import { useUi } from "@/uni_modules/cool-ui";
  24. import { ref } from "vue";
  25. const ui = useUi();
  26. const canvasRef = ref<ClCanvasComponentPublicInstance | null>(null);
  27. // 初始化为 0,避免页面未完全渲染时 getWindowInfo 获取到的高度或宽度不准确(如已知固定高宽可直接赋值)
  28. const height = ref(0);
  29. const width = ref(0);
  30. function onCanvasLoad(canvas: Canvas) {
  31. canvas
  32. .image({
  33. x: 0,
  34. y: 0,
  35. width: width.value,
  36. height: height.value,
  37. url: "/static/demo/canvas/bg.png"
  38. })
  39. .image({
  40. x: 0,
  41. y: 0,
  42. width: width.value,
  43. height: height.value,
  44. url: "/static/demo/canvas/light.png"
  45. })
  46. .image({
  47. x: (width.value - 226) / 2,
  48. y: 60,
  49. width: 226,
  50. height: 77,
  51. url: "/static/demo/canvas/text-yqhy.png"
  52. })
  53. .image({
  54. x: (width.value - 325) / 2,
  55. y: 125,
  56. width: 325,
  57. height: 77,
  58. url: "/static/demo/canvas/text-dezk.png"
  59. })
  60. .image({
  61. x: (width.value - 196) / 2,
  62. y: 190,
  63. width: 196,
  64. height: 62,
  65. url: "/static/demo/canvas/text-xrfl.png"
  66. })
  67. .image({
  68. x: (width.value - 374) / 2 - 1,
  69. y: 500,
  70. width: 374,
  71. height: 220,
  72. url: "/static/demo/canvas/rp-t.png"
  73. })
  74. .image({
  75. x: (width.value - 327) / 2,
  76. y: 280,
  77. width: 327,
  78. height: 430,
  79. url: "/static/demo/canvas/bg-content.png"
  80. })
  81. .image({
  82. x: 30,
  83. y: 240,
  84. width: 114,
  85. height: 120,
  86. url: "/static/demo/canvas/gold-l.png"
  87. })
  88. .image({
  89. x: width.value - 106 - 50,
  90. y: 240,
  91. width: 106,
  92. height: 107,
  93. url: "/static/demo/canvas/gold-r.png"
  94. })
  95. .image({
  96. x: (width.value - 350) / 2,
  97. y: 595,
  98. width: 350,
  99. height: 122,
  100. url: "/static/demo/canvas/rp-b.png"
  101. })
  102. .image({
  103. x: (width.value - 208) / 2,
  104. y: 631,
  105. width: 208,
  106. height: 89,
  107. url: "/static/demo/canvas/invite-btn.png"
  108. })
  109. .div({
  110. x: (width.value - 276) / 2,
  111. y: 335,
  112. width: 276,
  113. height: 210,
  114. backgroundColor: "#fff",
  115. radius: 10
  116. })
  117. .text({
  118. x: 0,
  119. y: 350,
  120. width: width.value,
  121. content: "新人专享",
  122. color: "#F73035",
  123. textAlign: "center",
  124. fontSize: 28,
  125. fontWeight: "bold"
  126. })
  127. .text({
  128. x: 0,
  129. y: 390,
  130. width: width.value,
  131. content: "限时领券30元",
  132. color: "#F73035",
  133. textAlign: "center",
  134. fontSize: 16
  135. })
  136. .image({
  137. x: (width.value - 246) / 2,
  138. y: 432,
  139. width: 246,
  140. height: 98,
  141. url: "/static/demo/canvas/coupon.png"
  142. })
  143. .text({
  144. x: (width.value - 246) / 2,
  145. y: 435,
  146. content: "领券",
  147. width: 34,
  148. color: "#fff",
  149. fontSize: 11,
  150. textAlign: "center"
  151. })
  152. .text({
  153. x: (width.value - 246) / 2,
  154. y: 454,
  155. width: 86,
  156. content: "80",
  157. color: "#604E44",
  158. fontSize: 46,
  159. textAlign: "center",
  160. fontWeight: "bold"
  161. })
  162. .text({
  163. x: (width.value - 246) / 2 + 86,
  164. y: 459,
  165. width: 246 - 86,
  166. content: "新人专享优惠券",
  167. color: "#604E44",
  168. fontSize: 18,
  169. textAlign: "center"
  170. })
  171. .text({
  172. x: (width.value - 246) / 2 + 86,
  173. y: 489,
  174. width: 246 - 86,
  175. content: "2021.04.30-2021.06.30",
  176. color: "#604E44",
  177. fontSize: 12,
  178. textAlign: "center"
  179. })
  180. .text({
  181. x: 0,
  182. y: 560,
  183. width: width.value,
  184. content: "邀请好友,双方均可获得20元优惠券",
  185. color: "#756056",
  186. fontSize: 15,
  187. textAlign: "center"
  188. })
  189. .text({
  190. x: 0,
  191. y: 300,
  192. width: width.value,
  193. content: "~ 专属新人福利 ~",
  194. color: "#956056",
  195. textAlign: "center",
  196. opacity: 0.7
  197. })
  198. .draw();
  199. }
  200. function previewImage() {
  201. canvasRef.value!.previewImage();
  202. }
  203. function saveImage() {
  204. canvasRef.value!.saveImage();
  205. }
  206. onReady(() => {
  207. // 同上
  208. height.value = uni.getWindowInfo().windowHeight;
  209. width.value = uni.getWindowInfo().windowWidth;
  210. ui.showConfirm({
  211. title: "提示",
  212. message: "本页面内容由 canvas 渲染生成,是否立即预览图片效果?",
  213. confirmText: "预览",
  214. callback(action) {
  215. if (action == "confirm") {
  216. canvasRef.value!.previewImage();
  217. }
  218. }
  219. });
  220. });
  221. </script>