element-draw.uvue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <view class="drawing" id="draw-text-view"></view>
  7. <view class="drawing" id="draw-line-view"></view>
  8. <view class="drawing" id="draw-circle-view"></view>
  9. <view class="drawing" id="draw-dash-line"></view>
  10. <view class="drawing" id="draw-house"></view>
  11. <view class="drawing" id="draw-style"></view>
  12. <view class="drawing" id="draw-odd"></view>
  13. <view class="drawing" id="draw-arcto"></view>
  14. </view>
  15. <!-- #ifdef APP -->
  16. </scroll-view>
  17. <!-- #endif -->
  18. </template>
  19. <script>
  20. var y = 160
  21. export default {
  22. data() {
  23. return {
  24. texts: [
  25. 'HBuilderX,轻巧、极速,极客编辑器',
  26. 'uni-app x,终极跨平台方案',
  27. 'uniCloud,js serverless云服务',
  28. 'uts,大一统语言',
  29. 'uniMPSdk,让你的App具备小程序能力',
  30. 'uni-admin,开源、现成的全端管理后台',
  31. 'uni-id,开源、全端的账户中心',
  32. 'uni-pay,开源、云端一体、全平台的支付',
  33. 'uni-ai,聚合ai能力',
  34. 'uni-cms,开源、云端一体、全平台的内容管理平台',
  35. 'uni-im,开源、云端一体、全平台的im即时消息',
  36. 'uni统计,开源、完善、全平台的统计报表',
  37. '......'
  38. ] as string[]
  39. }
  40. },
  41. onShow() {
  42. },
  43. onReady() {
  44. this.drawText()
  45. this.drawLines()
  46. this.drawCircles()
  47. this.drawStar()
  48. this.drawhouse()
  49. this.drawPoint()
  50. this.drawRect()
  51. this.drawArcTo()
  52. },
  53. onUnload() {
  54. y = 160
  55. },
  56. methods: {
  57. drawText() {
  58. let element = uni.getElementById('draw-text-view')!
  59. let ctx = element.getDrawableContext()!
  60. let width = element.getBoundingClientRect().width
  61. ctx.reset()
  62. ctx.font = "15px Arial"
  63. ctx.textAlign = "center"
  64. for (var i = 0; i < this.texts.length; i++) {
  65. let value = this.texts[i]
  66. if (i % 2 == 0) {
  67. ctx.fillText(value, width / 2, (20 * (i + 1)))
  68. } else {
  69. ctx.lineWidth = 0.5
  70. ctx.strokeText(value, width / 2, (20 * (i + 1)))
  71. }
  72. }
  73. ctx.update()
  74. },
  75. drawLines() {
  76. let ctx = uni.getElementById('draw-line-view')!.getDrawableContext()!
  77. ctx.reset()
  78. ctx.lineWidth = 10;
  79. ["round", "bevel", "miter"].forEach((join, i) => {
  80. ctx.lineJoin = join;
  81. ctx.beginPath();
  82. ctx.moveTo(5, 10 + i * 40);
  83. ctx.lineTo(50, 50 + i * 40);
  84. ctx.lineTo(90, 10 + i * 40);
  85. ctx.lineTo(130, 50 + i * 40);
  86. ctx.lineTo(170, 10 + i * 40);
  87. ctx.stroke();
  88. });
  89. ctx.lineWidth = 1
  90. var space = 170
  91. ctx.strokeStyle = '#09f';
  92. ctx.beginPath();
  93. ctx.moveTo(10 + space, 10);
  94. ctx.lineTo(140 + space, 10);
  95. ctx.moveTo(10 + space, 140);
  96. ctx.lineTo(140 + space, 140);
  97. ctx.stroke();
  98. // Draw lines
  99. ctx.strokeStyle = 'black';
  100. ['butt', 'round', 'square'].forEach((lineCap, i) => {
  101. ctx.lineWidth = 15;
  102. ctx.lineCap = lineCap;
  103. ctx.beginPath();
  104. ctx.moveTo(25 + space + i * 50, 10);
  105. ctx.lineTo(25 + space + i * 50, 140);
  106. ctx.stroke();
  107. });
  108. ctx.lineWidth = 1;
  109. this.drawDashedLine([], ctx);
  110. this.drawDashedLine([2, 2], ctx);
  111. this.drawDashedLine([10, 10], ctx);
  112. this.drawDashedLine([20, 5], ctx);
  113. this.drawDashedLine([15, 3, 3, 3], ctx);
  114. this.drawDashedLine([20, 3, 3, 3, 3, 3, 3, 3], ctx);
  115. ctx.lineDashOffset = 18;
  116. this.drawDashedLine([12, 3, 3], ctx);
  117. ctx.lineDashOffset = 0
  118. ctx.setLineDash([0])
  119. ctx.update()
  120. },
  121. drawDashedLine(pattern : Array<number>, ctx : DrawableContext) {
  122. ctx.beginPath();
  123. ctx.setLineDash(pattern);
  124. ctx.moveTo(0, y);
  125. ctx.lineTo(300, y);
  126. ctx.stroke();
  127. y += 15;
  128. },
  129. drawCircles() {
  130. let ctx = uni.getElementById('draw-circle-view')!.getDrawableContext()!
  131. ctx.reset()
  132. // Draw shapes
  133. for (var i = 0; i < 4; i++) {
  134. for (var j = 0; j < 3; j++) {
  135. ctx.beginPath();
  136. var x = 25 + j * 50; // x coordinate
  137. var y = 25 + i * 50; // y coordinate
  138. var radius = 20; // Arc radius
  139. var startAngle = 0; // Starting point on circle
  140. var endAngle = Math.PI + (Math.PI * j) / 2; // End point on circle
  141. var clockwise = i % 2 == 0 ? false : true; // clockwise or anticlockwise
  142. ctx.arc(x, y, radius, startAngle, endAngle, clockwise);
  143. if (i > 1) {
  144. ctx.fill();
  145. } else {
  146. ctx.stroke();
  147. }
  148. }
  149. }
  150. ctx.update()
  151. },
  152. drawStar() {
  153. let ctx = uni.getElementById('draw-dash-line')!.getDrawableContext()!
  154. ctx.reset()
  155. ctx.beginPath();
  156. var horn = 5; // 画5个角
  157. var angle = 360 / horn; // 五个角的度数
  158. // 两个圆的半径
  159. var R = 50;
  160. var r = 20;
  161. // 坐标
  162. var x = 100;
  163. var y = 100;
  164. // #ifdef APP-HARMONY
  165. //TODO 鸿蒙首次调用lineTo并不是起始点,这里暂时先调用一次moveTo
  166. ctx.moveTo(Math.cos((18 + 0 * angle) / 180.0 * Math.PI) * R + x, -Math.sin((18 + 0 * angle) / 180.0 * Math.PI) * R + y);
  167. // #endif
  168. for (var i = 0; i < horn; i++) {
  169. // 角度转弧度:角度/180*Math.PI
  170. // 外圆顶点坐标
  171. ctx.lineTo(Math.cos((18 + i * angle) / 180.0 * Math.PI) * R + x, -Math.sin((18 + i * angle) / 180.0 * Math.PI) * R + y);
  172. // 內圆顶点坐标
  173. ctx.lineTo(Math.cos((54 + i * angle) / 180.0 * Math.PI) * r + x, -Math.sin((54 + i * angle) / 180.0 * Math.PI) * r + y);
  174. }
  175. // closePath:关闭路径,将路径的终点与起点相连
  176. ctx.closePath();
  177. ctx.lineWidth = 3;
  178. ctx.fillStyle = '#E4EF00';
  179. ctx.strokeStyle = "red";
  180. ctx.fill();
  181. ctx.stroke();
  182. ctx.lineWidth = 10;
  183. ctx.beginPath()
  184. ctx.moveTo(170, 100)
  185. ctx.lineTo(255, 15)
  186. ctx.lineTo(340, 100)
  187. ctx.closePath()
  188. ctx.fill()
  189. ctx.strokeStyle = "blue"
  190. ctx.stroke()
  191. ctx.beginPath()
  192. ctx.moveTo(170, 145)
  193. ctx.lineTo(255, 45)
  194. ctx.lineTo(340, 145)
  195. ctx.closePath()
  196. ctx.fill()
  197. ctx.strokeStyle = "gray"
  198. ctx.stroke()
  199. // 未设置beginPath,导致上下表现一致,与前端一致
  200. ctx.moveTo(170, 190)
  201. ctx.lineTo(255, 90)
  202. ctx.lineTo(340, 190)
  203. ctx.closePath()
  204. ctx.fillStyle = "orange"
  205. ctx.fill()
  206. ctx.strokeStyle = "khaki"
  207. ctx.stroke()
  208. ctx.update()
  209. },
  210. hex(num : number) : string {
  211. if (num == 0) {
  212. return "00"
  213. }
  214. let hexChars = "0123456789ABCDEF";
  215. let result = "";
  216. while (num > 0) {
  217. let remainder = Math.floor(num) % 16;
  218. result = hexChars[remainder] + result;
  219. num = Math.floor(Math.floor(num) / 16);
  220. }
  221. if (result.length == 1) {
  222. return "0" + result
  223. }
  224. return result
  225. },
  226. drawhouse() {
  227. let ctx = uni.getElementById('draw-house')!.getDrawableContext()!
  228. ctx.reset()
  229. ctx.lineWidth = 10;
  230. // Wall
  231. ctx.strokeRect(75, 140, 150, 110);
  232. // Door
  233. ctx.fillRect(130, 190, 40, 60);
  234. // Roof
  235. ctx.beginPath();
  236. ctx.moveTo(50, 140);
  237. ctx.lineTo(150, 60);
  238. ctx.lineTo(250, 140);
  239. ctx.closePath();
  240. ctx.stroke();
  241. ctx.update()
  242. },
  243. drawPoint() {
  244. let ctx = uni.getElementById('draw-style')!.getDrawableContext()!
  245. ctx.reset()
  246. for (let i = 0; i < 6; i++) {
  247. for (let j = 0; j < 6; j++) {
  248. ctx.strokeStyle = `rgb(0,${Math.floor(255 - 42.5 * i)},${Math.floor(255 - 42.5 * j)})`;
  249. ctx.beginPath();
  250. ctx.arc(12.5 + j * 25, 12.5 + i * 25, 10, 0, Math.PI * 2, true);
  251. ctx.stroke();
  252. }
  253. }
  254. for (let i = 0; i < 6; i++) {
  255. for (let j = 0; j < 6; j++) {
  256. ctx.fillStyle = `rgb(${Math.floor(255 - 42.5 * i)},${Math.floor(255 - 42.5 * j)},0)`;
  257. ctx.fillRect(180 + j * 25, i * 25, 25, 25);
  258. }
  259. }
  260. ctx.update()
  261. },
  262. drawRect() {
  263. let ctx = uni.getElementById('draw-odd')!.getDrawableContext()!
  264. ctx.reset()
  265. // Create path
  266. ctx.moveTo(30, 90);
  267. ctx.lineTo(110, 20);
  268. ctx.lineTo(240, 130);
  269. ctx.lineTo(60, 130);
  270. ctx.lineTo(190, 20);
  271. ctx.lineTo(270, 90);
  272. ctx.closePath();
  273. // Fill path
  274. ctx.fillStyle = "green";
  275. ctx.fill("evenodd");
  276. ctx.update()
  277. },
  278. drawArcTo() {
  279. let ctx = uni.getElementById('draw-arcto')!.getDrawableContext()!
  280. ctx.reset()
  281. ctx.beginPath();
  282. ctx.moveTo(50, 20);
  283. ctx.bezierCurveTo(230, 30, 150, 60, 50, 100);
  284. ctx.stroke();
  285. ctx.fillStyle = "blue";
  286. // start point
  287. ctx.fillRect(50, 20, 10, 10);
  288. // end point
  289. ctx.fillRect(50, 100, 10, 10);
  290. ctx.fillStyle = "red";
  291. // control point one
  292. ctx.fillRect(230, 30, 10, 10);
  293. // control point two
  294. ctx.fillRect(150, 70, 10, 10);
  295. ctx.update()
  296. }
  297. }
  298. }
  299. </script>
  300. <style>
  301. .drawing {
  302. height: 275px;
  303. background-color: lightgray;
  304. margin-bottom: 15px;
  305. }
  306. </style>