canvas.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-common-mt">
  5. <canvas class="canvas-element" canvas-id="canvas" id="canvas"></canvas>
  6. <scroll-view class="canvas-buttons" scroll-y="true">
  7. <block v-for="(name, index) in names" :key="index">
  8. <button class="canvas-button" @click="handleCanvasButton(name)">{{name}}</button>
  9. </block>
  10. <button class="canvas-button" @click="toTempFilePath" type="primary">toTempFilePath</button>
  11. </scroll-view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. var context = null;
  17. export default {
  18. data() {
  19. return {
  20. title: 'createContext',
  21. names: ["rotate", "scale", "reset", "translate", "save", "restore", "drawImage", "fillText", "fill",
  22. "stroke", "clearRect", "beginPath", "closePath", "moveTo", "lineTo", "rect", "arc",
  23. "quadraticCurveTo", "bezierCurveTo", "setFillStyle", "setStrokeStyle", "setGlobalAlpha",
  24. "setShadow", "setFontSize", "setLineCap", "setLineJoin", "setLineWidth", "setMiterLimit"
  25. ]
  26. }
  27. },
  28. onReady: function() {
  29. context = uni.createCanvasContext('canvas',this)
  30. },
  31. methods: {
  32. toTempFilePath: function() {
  33. uni.canvasToTempFilePath({
  34. canvasId: 'canvas',
  35. success: function(res) {
  36. console.log(res.tempFilePath)
  37. },
  38. fail: function(err) {
  39. console.error(JSON.stringify(err))
  40. }
  41. })
  42. },
  43. handleCanvasButton: function(name) {
  44. this[name] && this[name]();
  45. },
  46. rotate: function() {
  47. context.beginPath()
  48. context.rotate(10 * Math.PI / 180)
  49. context.rect(225, 75, 20, 10)
  50. context.fill()
  51. context.draw()
  52. },
  53. scale: function() {
  54. context.beginPath()
  55. context.rect(25, 25, 50, 50)
  56. context.stroke()
  57. context.scale(2, 2)
  58. context.beginPath()
  59. context.rect(25, 25, 50, 50)
  60. context.stroke()
  61. context.draw()
  62. },
  63. reset: function() {
  64. context.beginPath()
  65. context.setFillStyle('#000000')
  66. context.setStrokeStyle('#000000')
  67. context.setFontSize(10)
  68. context.setGlobalAlpha(1)
  69. context.setShadow(0, 0, 0, 'rgba(0, 0, 0, 0)')
  70. context.setLineCap('butt')
  71. context.setLineJoin('miter')
  72. context.setLineWidth(1)
  73. context.setMiterLimit(10)
  74. context.draw()
  75. },
  76. translate: function() {
  77. context.beginPath()
  78. context.rect(10, 10, 100, 50)
  79. context.fill()
  80. context.translate(70, 70)
  81. context.beginPath()
  82. context.fill()
  83. context.draw()
  84. },
  85. save: function() {
  86. context.beginPath()
  87. context.setStrokeStyle('#00ff00')
  88. context.save()
  89. context.scale(2, 2)
  90. context.setStrokeStyle('#ff0000')
  91. context.rect(0, 0, 100, 100)
  92. context.stroke()
  93. context.restore()
  94. context.rect(0, 0, 50, 50)
  95. context.stroke()
  96. context.draw()
  97. },
  98. restore: function() {
  99. [3, 2, 1].forEach(function(item) {
  100. context.beginPath()
  101. context.save()
  102. context.scale(item, item)
  103. context.rect(10, 10, 100, 100)
  104. context.stroke()
  105. context.restore()
  106. });
  107. context.draw()
  108. },
  109. drawImage: function() {
  110. // #ifdef APP-PLUS
  111. context.drawImage('../../../static/app-plus/uni@2x.png', 0, 0)
  112. // #endif
  113. // #ifndef APP-PLUS
  114. context.drawImage('../../../static/uni.png', 0, 0)
  115. // #endif
  116. context.draw()
  117. },
  118. fillText: function() {
  119. context.setStrokeStyle('#ff0000')
  120. context.beginPath()
  121. context.moveTo(0, 10)
  122. context.lineTo(300, 10)
  123. context.stroke()
  124. // context.save()
  125. // context.scale(1.5, 1.5)
  126. // context.translate(20, 20)
  127. context.setFontSize(10)
  128. context.fillText('Hello World', 0, 30)
  129. context.setFontSize(20)
  130. context.fillText('Hello World', 100, 30)
  131. // context.restore()
  132. context.beginPath()
  133. context.moveTo(0, 30)
  134. context.lineTo(300, 30)
  135. context.stroke()
  136. context.draw()
  137. },
  138. fill: function() {
  139. context.beginPath()
  140. context.rect(20, 20, 150, 100)
  141. context.setStrokeStyle('#00ff00')
  142. context.fill()
  143. context.draw()
  144. },
  145. stroke: function() {
  146. context.beginPath()
  147. context.moveTo(20, 20)
  148. context.lineTo(20, 100)
  149. context.lineTo(70, 100)
  150. context.setStrokeStyle('#00ff00')
  151. context.stroke()
  152. context.draw()
  153. },
  154. clearRect: function() {
  155. context.setFillStyle('#ff0000')
  156. context.beginPath()
  157. context.rect(0, 0, 300, 150)
  158. context.fill()
  159. context.clearRect(20, 20, 100, 50)
  160. context.draw()
  161. },
  162. beginPath: function() {
  163. context.beginPath()
  164. context.setLineWidth(5)
  165. context.setStrokeStyle('#ff0000')
  166. context.moveTo(0, 75)
  167. context.lineTo(250, 75)
  168. context.stroke()
  169. context.beginPath()
  170. context.setStrokeStyle('#0000ff')
  171. context.moveTo(50, 0)
  172. context.lineTo(150, 130)
  173. context.stroke()
  174. context.draw()
  175. },
  176. closePath: function() {
  177. context.beginPath()
  178. context.setLineWidth(1)
  179. context.moveTo(20, 20)
  180. context.lineTo(20, 100)
  181. context.lineTo(70, 100)
  182. context.closePath()
  183. context.stroke()
  184. context.draw()
  185. },
  186. moveTo: function() {
  187. context.beginPath()
  188. context.moveTo(0, 0)
  189. context.lineTo(300, 150)
  190. context.stroke()
  191. context.draw()
  192. },
  193. lineTo: function() {
  194. context.beginPath()
  195. context.moveTo(20, 20)
  196. context.lineTo(20, 100)
  197. context.lineTo(70, 100)
  198. context.stroke()
  199. context.draw()
  200. },
  201. rect: function() {
  202. context.beginPath()
  203. context.rect(20, 20, 150, 100)
  204. context.stroke()
  205. context.draw()
  206. },
  207. arc: function() {
  208. context.beginPath()
  209. context.setLineWidth(2)
  210. context.arc(75, 75, 50, 0, Math.PI * 2, true)
  211. context.moveTo(110, 75)
  212. context.arc(75, 75, 35, 0, Math.PI, false)
  213. context.moveTo(65, 65)
  214. context.arc(60, 65, 5, 0, Math.PI * 2, true)
  215. context.moveTo(95, 65)
  216. context.arc(90, 65, 5, 0, Math.PI * 2, true)
  217. context.stroke()
  218. context.draw()
  219. },
  220. quadraticCurveTo: function() {
  221. context.beginPath()
  222. context.moveTo(20, 20)
  223. context.quadraticCurveTo(20, 100, 200, 20)
  224. context.stroke()
  225. context.draw()
  226. },
  227. bezierCurveTo: function() {
  228. context.beginPath()
  229. context.moveTo(20, 20)
  230. context.bezierCurveTo(20, 100, 200, 100, 200, 20)
  231. context.stroke()
  232. context.draw()
  233. },
  234. setFillStyle: function() {
  235. ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function(item, index) {
  236. context.setFillStyle(item)
  237. context.beginPath()
  238. context.rect(0 + 75 * index, 0, 50, 50)
  239. context.fill()
  240. })
  241. context.draw()
  242. },
  243. setStrokeStyle: function() {
  244. ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function(item, index) {
  245. context.setStrokeStyle(item)
  246. context.beginPath()
  247. context.rect(0 + 75 * index, 0, 50, 50)
  248. context.stroke()
  249. })
  250. context.draw()
  251. },
  252. setGlobalAlpha: function() {
  253. context.setFillStyle('#000000');
  254. [1, 0.5, 0.1].forEach(function(item, index) {
  255. context.setGlobalAlpha(item)
  256. context.beginPath()
  257. context.rect(0 + 75 * index, 0, 50, 50)
  258. context.fill()
  259. })
  260. context.draw()
  261. context.setGlobalAlpha(1)
  262. },
  263. setShadow: function() {
  264. context.beginPath()
  265. context.setShadow(10, 10, 10, 'rgba(0, 0, 0, 199)')
  266. context.rect(10, 10, 100, 100)
  267. context.fill()
  268. context.draw()
  269. },
  270. setFontSize: function() {
  271. [10, 20, 30, 40].forEach(function(item, index) {
  272. context.setFontSize(item)
  273. context.fillText('Hello, world', 20, 20 + 40 * index)
  274. })
  275. context.draw()
  276. },
  277. setLineCap: function() {
  278. context.setLineWidth(10);
  279. ['butt', 'round', 'square'].forEach(function(item, index) {
  280. context.beginPath()
  281. context.setLineCap(item)
  282. context.moveTo(20, 20 + 20 * index)
  283. context.lineTo(100, 20 + 20 * index)
  284. context.stroke()
  285. })
  286. context.draw()
  287. },
  288. setLineJoin: function() {
  289. context.setLineWidth(10);
  290. ['bevel', 'round', 'miter'].forEach(function(item, index) {
  291. context.beginPath()
  292. context.setLineJoin(item)
  293. context.moveTo(20 + 80 * index, 20)
  294. context.lineTo(100 + 80 * index, 50)
  295. context.lineTo(20 + 80 * index, 100)
  296. context.stroke()
  297. })
  298. context.draw()
  299. },
  300. setLineWidth: function() {
  301. [2, 4, 6, 8, 10].forEach(function(item, index) {
  302. context.beginPath()
  303. context.setLineWidth(item)
  304. context.moveTo(20, 20 + 20 * index)
  305. context.lineTo(100, 20 + 20 * index)
  306. context.stroke()
  307. })
  308. context.draw()
  309. },
  310. setMiterLimit: function() {
  311. context.setLineWidth(4);
  312. [2, 4, 6, 8, 10].forEach(function(item, index) {
  313. context.beginPath()
  314. context.setMiterLimit(item)
  315. context.moveTo(20 + 80 * index, 20)
  316. context.lineTo(100 + 80 * index, 50)
  317. context.lineTo(20 + 80 * index, 100)
  318. context.stroke()
  319. })
  320. context.draw()
  321. }
  322. }
  323. }
  324. </script>
  325. <style>
  326. .canvas-element-wrapper {
  327. display: block;
  328. margin-bottom: 100rpx;
  329. }
  330. .canvas-element {
  331. width: 100%;
  332. height: 500rpx;
  333. background-color: #ffffff;
  334. }
  335. .canvas-buttons {
  336. padding: 30rpx 50rpx 10rpx;
  337. width: 100%;
  338. height: 360rpx;
  339. box-sizing: border-box;
  340. }
  341. .canvas-button {
  342. float: left;
  343. display: inline-flex;
  344. align-items: center;
  345. justify-content: center;
  346. height: 40px;
  347. line-height: 1;
  348. width: 300rpx;
  349. margin: 15rpx 12rpx;
  350. }
  351. </style>