CanvasContext

    示例

    跳转编辑工具

    在开发者工具中打开

    扫码体验

    代码示例

    CanvasContext - 图2

    代码示例

    • SWAN
    • JS
    • CSS
    • JS

    1. canvas.arc = function (context) {
    2. context.beginPath();
    3. context.arc(100, 75, 50, 0, 2 * Math.PI);
    4. context.setFillStyle('blue');
    5. context.fill();
    6. context.draw();
    7. };
    8. canvas.beginPath = function (context) {
    9. context.beginPath();
    10. context.setLineWidth(5);
    11. context.setStrokeStyle('#ff0000');
    12. context.moveTo(0, 75);
    13. context.lineTo(250, 75);
    14. context.stroke();
    15. context.beginPath();
    16. context.setStrokeStyle('#0000ff');
    17. context.moveTo(50, 0);
    18. context.lineTo(150, 130);
    19. context.stroke();
    20. };
    21. canvas.bezierCurveTo = function (context) {
    22. context.beginPath();
    23. context.moveTo(20, 20);
    24. context.bezierCurveTo(20, 100, 200, 100, 200, 20);
    25. context.setStrokeStyle('black');
    26. context.stroke();
    27. };
    28. canvas.clearRect = function (context) {
    29. context.setFillStyle('blue');
    30. context.fillRect(0, 0, 250, 150);
    31. context.clearRect(20, 20, 150, 75);
    32. context.draw();
    33. };
    34. canvas.clip = function (context) {
    35. context.beginPath();
    36. context.rect(50, 20, 200, 120);
    37. context.stroke();
    38. context.clip();
    39. context.fillRect(0, 0, 150, 100);
    40. };
    41. canvas.closePath = function (context) {
    42. context.beginPath();
    43. context.moveTo(20, 20);
    44. context.lineTo(20, 100);
    45. context.lineTo(70, 100);
    46. context.closePath();
    47. context.stroke();
    48. };
    49. canvas.createCircularGradient = function (context) {
    50. // Create circular gradient
    51. const grd = context.createCircularGradient(75, 50, 50);
    52. grd.addColorStop(0, 'red');
    53. grd.addColorStop(1, 'blue');
    54. // Fill with gradient
    55. context.setFillStyle(grd);
    56. context.fillRect(30, 30, 150, 80);
    57. };
    58. canvas.createLinearGradient = function (context) {
    59. const grd = context.createLinearGradient(0, 0, 200, 0);
    60. grd.addColorStop(0, 'blue');
    61. grd.addColorStop(1, 'red');
    62. // Fill with gradient
    63. context.setFillStyle(grd);
    64. context.fillRect(30, 30, 150, 80);
    65. };
    66. canvas.drawImage = function (context) {
    67. context.drawImage('https://b.bdstatic.com/searchbox/icms/searchbox/img/api_logo.png', 0, 0);
    68. };
    69. canvas.fill = function (context) {
    70. context.beginPath();
    71. context.moveTo(100, 100);
    72. context.lineTo(10, 100);
    73. context.lineTo(10, 10);
    74. context.fill();
    75. };
    76. canvas.fillRect = function (context) {
    77. context.beginPath();
    78. context.setFillStyle('blue');
    79. context.fillRect(30, 30, 150, 75);
    80. };
    81. canvas.fillText = function (context) {
    82. context.setFontSize(20);
    83. context.fillText('Hello', 20, 20);
    84. context.fillText('World', 100, 100);
    85. };
    86. canvas.font = function (context) {
    87. context.font = '50px oblique bold Microsoft YaiHei';
    88. context.fillText('微软雅黑', 90, 90);
    89. context.font = '50px 楷体';
    90. context.fillText('楷体', 90, 200);
    91. };
    92. canvas.lineTo = function (context) {
    93. context.beginPath();
    94. context.moveTo(10, 10);
    95. context.rect(10, 10, 100, 50);
    96. context.lineTo(110, 60);
    97. context.stroke();
    98. };
    99. canvas.moveTo = function (context) {
    100. context.moveTo(10, 10);
    101. context.lineTo(100, 10);
    102. context.moveTo(10, 100);
    103. context.stroke();
    104. };
    105. canvas.quadraticCurveTo = function (context) {
    106. context.beginPath();
    107. context.moveTo(20, 20);
    108. context.stroke();
    109. };
    110. canvas.rect = function (context) {
    111. context.beginPath();
    112. context.rect(30, 30, 150, 75);
    113. context.stroke();
    114. };
    115. canvas.rotate = function (context) {
    116. context.beginPath();
    117. context.strokeRect(100, 10, 150, 100);
    118. context.rotate(10 * Math.PI / 180);
    119. context.strokeRect(100, 10, 150, 100);
    120. context.rotate(10 * Math.PI / 180);
    121. context.strokeRect(100, 10, 150, 100);
    122. context.fill();
    123. };
    124. canvas.saveAndRestore = function (context) {
    125. context.beginPath();
    126. context.setFillStyle('red');
    127. context.save();
    128. context.setFillStyle('blue');
    129. context.fillRect(10, 10, 150, 100);
    130. context.restore();
    131. context.fillRect(50, 50, 150, 100);
    132. context.stroke();
    133. };
    134. canvas.scale = function (context) {
    135. context.beginPath();
    136. context.rect(10, 10, 25, 15);
    137. context.stroke();
    138. context.scale(2, 2);
    139. context.beginPath();
    140. context.rect(10, 10, 25, 15);
    141. context.stroke();
    142. context.scale(2, 2);
    143. context.beginPath();
    144. context.rect(10, 10, 25, 15);
    145. context.stroke();
    146. };
    147. canvas.setFillStyle = function (context) {
    148. ['blue', '#ffff00', 'rgba(255,255,0, 0.3)'].forEach(function (item, index) {
    149. context.setFillStyle(item);
    150. context.beginPath();
    151. context.rect(0 + 75 * index, 0, 50, 50);
    152. context.fill();
    153. });
    154. };
    155. canvas.setFontSize = function (context) {
    156. context.setFontSize(20);
    157. context.fillText('20', 20, 20);
    158. context.setFontSize(30);
    159. context.fillText('30', 40, 40);
    160. context.setFontSize(40);
    161. context.fillText('40', 60, 60);
    162. context.setFontSize(50);
    163. context.fillText('50', 90, 90);
    164. };
    165. canvas.setGlobalAlpha = function (context) {
    166. context.setFillStyle('red');
    167. context.fillRect(10, 10, 150, 100);
    168. context.setGlobalAlpha(0.2);
    169. context.setFillStyle('blue');
    170. context.fillRect(50, 50, 150, 100);
    171. context.setFillStyle('yellow');
    172. context.fillRect(100, 100, 150, 100);
    173. context.draw();
    174. };
    175. canvas.setLineCap = function (context) {
    176. context.beginPath();
    177. context.moveTo(20, 10);
    178. context.lineTo(200, 10);
    179. context.stroke();
    180. ['butt', 'round', 'square'].forEach(function (item, index) {
    181. context.beginPath();
    182. context.setLineCap(item);
    183. context.setLineWidth(10);
    184. context.moveTo(20 + 20 * index, 20 + 20 * index);
    185. context.lineTo(200, 20 + 20 * index);
    186. context.stroke();
    187. });
    188. };
    189. canvas.setLineDash = function (context) {
    190. context.setLineDash([10, 20], 5);
    191. context.beginPath();
    192. context.moveTo(0, 100);
    193. context.lineTo(400, 100);
    194. context.stroke();
    195. };
    196. canvas.setLineJoin = function (context) {
    197. context.beginPath();
    198. context.moveTo(10, 10);
    199. context.lineTo(90, 50);
    200. context.lineTo(10, 90);
    201. context.stroke();
    202. ['bevel', 'round', 'miter'].forEach(function (item, index) {
    203. context.beginPath();
    204. context.setLineJoin(item);
    205. context.setLineWidth(10);
    206. context.moveTo(30 + 80 * index, 10);
    207. context.lineTo(120 + 80 * index, 50);
    208. context.lineTo(30 + 80 * index, 90);
    209. context.stroke();
    210. });
    211. };
    212. canvas.setLineWidth = function (context) {
    213. context.beginPath();
    214. context.setLineWidth(item);
    215. context.moveTo(20 + 10 * index, 20 + 20 * index);
    216. context.stroke();
    217. });
    218. };
    219. canvas.setMiterLimit = function (context) {
    220. [1, 2, 3, 4].forEach(function (item, index) {
    221. context.beginPath();
    222. context.setMiterLimit(item);
    223. context.setLineWidth(10);
    224. context.setLineJoin('miter');
    225. context.moveTo(50 + 40 * index, 10);
    226. context.lineTo(140 + 40 * index, 50);
    227. context.lineTo(50 + 40 * index, 90);
    228. context.stroke();
    229. });
    230. };
    231. canvas.setShadow = function (context) {
    232. context.beginPath();
    233. context.setFillStyle('blue');
    234. context.setShadow(15, 15, 15, 'red');
    235. context.rect(30, 30, 150, 75);
    236. context.fill();
    237. };
    238. canvas.setStrokeStyle = function (context) {
    239. context.setStrokeStyle('blue');
    240. context.strokeRect(30, 30, 150, 75);
    241. };
    242. canvas.setTextAlign = function (context) {
    243. context.setStrokeStyle('red');
    244. context.moveTo(150, 20);
    245. context.lineTo(150, 170);
    246. context.stroke();
    247. context.setFontSize(15);
    248. context.setTextAlign('left');
    249. context.fillText('textAlign=left', 150, 60);
    250. context.setTextAlign('center');
    251. context.fillText('textAlign=center', 150, 80);
    252. context.setTextAlign('right');
    253. context.fillText('textAlign=right', 150, 100);
    254. };
    255. canvas.setTextBaseline = function (context) {
    256. context.setStrokeStyle('red');
    257. context.moveTo(5, 75);
    258. context.lineTo(295, 75);
    259. context.stroke();
    260. context.setFontSize(20);
    261. context.setTextBaseline('top');
    262. context.fillText('top', 5, 75);
    263. context.setTextBaseline('middle');
    264. context.fillText('middle', 50, 75);
    265. context.setTextBaseline('bottom');
    266. context.fillText('bottom', 120, 75);
    267. context.setTextBaseline('normal');
    268. context.fillText('normal', 200, 75);
    269. };
    270. canvas.setTransform = function (context) {
    271. context.setFillStyle('blue');
    272. context.fillRect(30, 30, 150, 75);
    273. context.setTransform(1, 0.5, -0.5, 1, 30, 10);
    274. context.setFillStyle('red');
    275. context.fillRect(30, 30, 150, 75);
    276. context.setTransform(1, 0.5, -0.5, 1, 30, 10);
    277. };
    278. canvas.stroke = function (context) {
    279. context.beginPath();
    280. context.moveTo(100, 100);
    281. context.lineTo(10, 100);
    282. context.lineTo(10, 10);
    283. context.stroke();
    284. };
    285. canvas.strokeRect = function (context) {
    286. context.setStrokeStyle('blue');
    287. context.strokeRect(30, 30, 150, 75);
    288. };
    289. canvas.translate = function (context) {
    290. context.strokeRect(10, 10, 150, 100);
    291. context.translate(20, 20);
    292. context.strokeRect(10, 10, 150, 100);
    293. context.translate(20, 20);
    294. context.strokeRect(10, 10, 150, 100);
    295. };
    296. canvas.addColorStop = function (context) {
    297. const grd = context.createLinearGradient(30, 10, 120, 10);
    298. grd.addColorStop(0, 'red');
    299. grd.addColorStop(0.16, 'orange');
    300. grd.addColorStop(0.33, 'yellow');
    301. grd.addColorStop(0.5, 'green');
    302. grd.addColorStop(0.66, 'cyan');
    303. grd.addColorStop(0.83, 'blue');
    304. grd.addColorStop(1, 'purple');
    305. context.setFillStyle(grd);
    306. context.fillRect(30, 30, 150, 80);
    307. };
    308. canvas.reset = function (context) {
    309. context.beginPath();
    310. context.setFillStyle('#000000');
    311. context.setStrokeStyle('#000000');
    312. context.setFontSize(10);
    313. context.setShadow(0, 0, 0, 'rgba(0, 0, 0, 0)');
    314. context.setLineCap('butt');
    315. context.setLineJoin('miter');
    316. context.setLineWidth(1);
    317. context.setMiterLimit(10);
    318. };