canvas.uvue 4.4 KB

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