微信小程序 绘图 canvas

这里将介绍下微信小程序 绘图 canvas

上代码:1.

< html] view plain copy 1.
2.
3.

2.

css] view plain copy 1.Page {

  1. background: #cfeeff;
    3.}
    4..canvas{
  2. width: 100%;
  3. height: 680rpx;
    7.}

3

[plain] view plain copy 1.{

  1. “navigationBarTitleText”: “画板”,
  2. “navigationBarBackgroundColor”: “#eee200”
    4.}

4

[javascript] view plain copy 1.// canvas 全局配置
2.var context = null;// 使用 wx.createContext 获取绘图上下文 context
3.var isButtonDown = false;
4.var arrx = [];
5.var arry = [];
6.var arrz = [];
7.var canvasw = 0;
8.var canvash = 0;
9.//获取系统信息
10.wx.getSystemInfo({

  1. success: function (res) {
  2. canvasw = res.windowWidth;//设备宽度
  3. canvash = res.windowHeight;
  4. }
    15.});
    16.//注册页面
    17.Page({
  5. canvasIdErrorCallback: function (e) {
  6. console.error(e.detail.errMsg)
  7. },
  8. canvasStart: function (event) {
  9. isButtonDown = true;
  10. arrz.push(0);
  11. arrx.push(event.changedTouches[0].x);
  12. arry.push(event.changedTouches[0].y);
  13. //context.moveTo(event.changedTouches[0].x, event.changedTouches[0].y);
  14. },
  15. canvasMove: function (event) {
  16. if (isButtonDown) {
  17. arrz.push(1);
  18. arrx.push(event.changedTouches[0].x);
  19. arry.push(event.changedTouches[0].y);
  20. // context.lineTo(event.changedTouches[0].x, event.changedTouches[0].y);
  21. // context.stroke();
  22. // context.draw()
  23. };
  24. for (var i = 0; i < arrx.length; i++) {
  25. if (arrz[i] == 0) {
  26. context.moveTo(arrx[i], arry[i])
  27. } else {
  28. context.lineTo(arrx[i], arry[i])
  29. };
  30. };
  31. context.clearRect(0, 0, canvasw, canvash);
  32. context.stroke();
  33. context.draw(true);
  34. },
  35. canvasEnd: function (event) {
  36. isButtonDown = false;
  37. },
  38. cleardraw: function () {
  39. //清除画布
  40. arrx = [];
  41. arry = [];
  42. arrz = [];
  43. context.clearRect(0, 0, canvasw, canvash);
  44. context.draw(true);
  45. },
  46. getimg: function () {
  47. if (arrx.length == 0) {
  48. wx.showModal({
  49. title: ‘提示’,
  50. content: ‘签名内容不能为空!’,
  51. showCancel: false
  52. });
  53. return false;
  54. };
  55. //生成图片
  56. wx.canvasToTempFilePath({
  57. canvasId: ‘canvas’,
  58. success: function (res) {
  59. console.log(res.tempFilePath);
  60. //存入服务器
  61. wx.uploadFile({
  62. url: ‘a.php’, //接口地址
  63. filePath: res.tempFilePath,
  64. name: ‘file’,
  65. formData: { //HTTP 请求中其他额外的 form data
  66. ‘user’: ‘test’
  67. },
  68. success: function (res) {
  69. console.log(res);
  70. },
  71. fail: function (res) {
  72. console.log(res);
  73. },
  74. complete: function (res) {
  75. }
  76. });
  77. }
  78. })
  79. },
  80. /**
    • 页面的初始数据
  81. */
  82. data: {
  83. src: “”
  84. },
  85. /**
    • 生命周期函数–监听页面加载
  86. */
  87. onLoad: function (options) {
  88. // 使用 wx.createContext 获取绘图上下文 context
  89. context = wx.createCanvasContext(‘canvas’);
  90. context.beginPath()
  91. context.setStrokeStyle(‘#ffff00’);
  92. context.setLineWidth(10);
  93. context.setLineCap(‘round’);
  94. context.setLineJoin(‘round’);
  95. },
  96. /**
    • 生命周期函数–监听页面初次渲染完成
  97. */
  98. onReady: function () {
  99. },
  100. /**
    • 生命周期函数–监听页面显示
  101. */
  102. onShow: function () {
  103. },
  104. /**
    • 生命周期函数–监听页面隐藏
  105. */
  106. onHide: function () {
  107. },
  108. /**
    • 生命周期函数–监听页面卸载
  109. */
  110. onUnload: function () {
  111. },
  112. /**
    • 页面相关事件处理函数–监听用户下拉动作
  113. */
  114. onPullDownRefresh: function () {
  115. },
  116. /**
    • 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. },
  120. /**
    • 用户点击右上角分享
  121. */
  122. onShareAppMessage: function () {
  123. }
    162.})

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦