screen.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var _request = require('../../../utils/request.js')
  2. var canvas = {}
  3. var width = 0
  4. var height = 0
  5. var ctx
  6. var img
  7. var imgInfo = {}
  8. var touchX = 0
  9. var touchY = 0
  10. var ix = 50
  11. var iy = 200
  12. Page({
  13. data: {
  14. },
  15. onLoad: function (options) {
  16. var info = wx.getSystemInfoSync()
  17. console.log(info)
  18. width = info.windowWidth
  19. height = info.windowHeight
  20. ctx = wx.createCanvasContext('myCanvas')
  21. this.draw()
  22. ctx.draw()
  23. },
  24. draw: function () {
  25. ctx.save()
  26. ctx.setFillStyle('#0b0b0b')
  27. ctx.setFontSize(this.dpi(28))
  28. ctx.fillText('明明什么都没有做', this.dpi(75), this.dpi(42))
  29. ctx.setFontSize(this.dpi(26))
  30. ctx.fillText('就已经', this.dpi(35), this.dpi(120))
  31. ctx.fillText('了', this.dpi(275), this.dpi(120))
  32. ctx.restore()
  33. },
  34. dpi: function (val) {
  35. return val * width / 375
  36. },
  37. ming: function () {
  38. wx.navigateTo({
  39. url: '/pages/start/start'
  40. })
  41. },
  42. shang: function () {
  43. var that = this
  44. wx.chooseImage({
  45. count: 1,
  46. success: function (val) {
  47. console.log(val)
  48. img = val.tempFilePaths[0]
  49. that.chooseImage()
  50. }
  51. })
  52. },
  53. chooseImage: function (val) {
  54. var that = this
  55. wx.getImageInfo({
  56. src: img,
  57. success: function (res) {
  58. console.log(res)
  59. imgInfo = res
  60. that.drawImage()
  61. }
  62. })
  63. // ctx.drawImage(sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
  64. },
  65. drawImage: function () {
  66. var w = 275
  67. var h = w * imgInfo.height / imgInfo.width
  68. console.log(h)
  69. console.log(img)
  70. console.log(String(h))
  71. ctx.drawImage(String(img), this.dpi(ix), this.dpi(iy), this.dpi(w), this.dpi(h))
  72. this.draw()
  73. ctx.draw()
  74. },
  75. save: function () {
  76. var that = this
  77. wx.canvasToTempFilePath({
  78. destWidth: width * 2,
  79. destHeight: height * 2,
  80. canvasId: 'myCanvas',
  81. fileType: 'jpg',
  82. success: function (res) {
  83. that.saveImage(res.tempFilePath)
  84. }
  85. })
  86. },
  87. saveImage: function (val) {
  88. wx.saveImageToPhotosAlbum({
  89. filePath: val,
  90. success(res) {
  91. wx.showToast({
  92. title: '已保存相册',
  93. icon: 'success',
  94. duration: 2000
  95. })
  96. }
  97. })
  98. },
  99. move: function (res) {
  100. console.log(res)
  101. if (img) {
  102. if (res.touches.length === 1) {
  103. var x = res.touches[0].x - touchX
  104. var y = res.touches[0].y - touchY
  105. ix = ix + x
  106. iy = iy + y
  107. touchX = res.touches[0].x
  108. touchY = res.touches[0].y
  109. this.drawImage()
  110. }
  111. }
  112. },
  113. start: function (val) {
  114. touchX = val.changedTouches[0].x
  115. touchY = val.changedTouches[0].y
  116. },
  117. onShareAppMessage: function (val) {
  118. if (val.from === 'button') {
  119. }
  120. return {
  121. title: '"明明什么都没有做"壁纸生成工具',
  122. path: '/pages/activity/screen/screen',
  123. imageUrl: 'https://fohow.oss-cn-shenzhen.aliyuncs.com/xcx/share/mingming.jpg',
  124. success: function(res) {
  125. },
  126. fail: function(res) {
  127. }
  128. }
  129. },
  130. })