canvas.uvue 4.5 KB

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