u-signature.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <view class="u-signature">
  3. <view class="u-signature__canvas-wrap">
  4. <canvas
  5. class="u-signature__canvas"
  6. :canvas-id="canvasId"
  7. :disable-scroll="true"
  8. @touchstart="touchStart"
  9. @touchmove="touchMove"
  10. @touchend="touchEnd"
  11. :style="{
  12. width: canvasWidth + 'px',
  13. height: canvasHeight + 'px',
  14. background: bgColor
  15. }"
  16. ></canvas>
  17. </view>
  18. <view v-if="showToolbar" class="u-signature__toolbar">
  19. <view class="u-signature__toolbar-icons u-flex u-flex-x">
  20. <view class="u-signature__toolbar-icon" @click="undo">
  21. <up-icon name="arrow-left" size="22" :color="pathStack.length === 0 ? '#ccc' : '#999'"></up-icon>
  22. </view>
  23. <view class="u-signature__toolbar-icon" @click="clear">
  24. <up-icon name="trash" size="25" color="#999"></up-icon>
  25. </view>
  26. <view class="u-signature__toolbar-icon" @click="toggleBrushSettings">
  27. <up-icon name="edit-pen" size="25" color="#999"></up-icon>
  28. </view>
  29. <view class="u-signature__toolbar-icon" @click="toggleColorSettings">
  30. <up-icon name="grid" size="24" color="#999"></up-icon>
  31. </view>
  32. <view class="u-signature__toolbar-icon" @click="exportSignature">
  33. <up-icon name="checkmark" size="25" :color="isEmpty ? '#ccc' : '#999'"></up-icon>
  34. </view>
  35. </view>
  36. <!-- 笔画设置 -->
  37. <view v-if="showBrushSettings" class="u-signature__brush-settings">
  38. <view class="u-signature__progress">
  39. <text class="u-signature__progress-label">笔画大小:</text>
  40. <up-slider
  41. v-model="lineWidth"
  42. :min="1"
  43. :max="20"
  44. :step="1"
  45. @show-value="true"
  46. :value-show="(lineWidth)"
  47. ></up-slider>
  48. </view>
  49. </view>
  50. <!-- 颜色设置 -->
  51. <view v-if="showColorSettings" class="u-signature__color-settings">
  52. <view class="u-signature__color-picker">
  53. <text class="u-signature__color-label">笔画颜色:</text>
  54. <view class="u-signature__colors">
  55. <view
  56. v-for="(color, index) in presetColors"
  57. :key="index"
  58. class="u-signature__color-item"
  59. :class="{'u-signature__color-item--active': lineColor === color}"
  60. :style="{ backgroundColor: color }"
  61. @click="selectColor(color)"
  62. ></view>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. export default {
  71. name: 'u-signature',
  72. props: {
  73. // 画布宽度
  74. width: {
  75. type: [String, Number],
  76. default: 300
  77. },
  78. // 画布高度
  79. height: {
  80. type: [String, Number],
  81. default: 200
  82. },
  83. // 背景颜色
  84. bgColor: {
  85. type: String,
  86. default: '#ffffff'
  87. },
  88. // 默认笔画颜色
  89. color: {
  90. type: String,
  91. default: '#000000'
  92. },
  93. // 默认笔画粗细
  94. thickness: {
  95. type: [String, Number],
  96. default: 3
  97. },
  98. // 是否显示工具栏
  99. showToolbar: {
  100. type: Boolean,
  101. default: true
  102. }
  103. },
  104. data() {
  105. return {
  106. canvasId: 'u-signature-' + Math.random().toString(36).substr(2, 9),
  107. canvasWidth: 300,
  108. canvasHeight: 200,
  109. lineColor: '#000000',
  110. lineWidth: 3,
  111. isDrawing: false,
  112. pathStack: [], // 存储绘制路径用于回退
  113. currentPath: [], // 当前绘制路径
  114. ctx: null,
  115. isEmpty: true,
  116. presetColors: [
  117. '#000000', // 黑色
  118. '#ff0000', // 红色
  119. '#00ff00', // 绿色
  120. '#0000ff', // 蓝色
  121. '#ffff00', // 黄色
  122. '#00ffff', // 青色
  123. '#ff00ff', // 紫色
  124. '#ffffff' // 白色
  125. ],
  126. showBrushSettings: false,
  127. showColorSettings: false,
  128. lastPoint: null // 保存上一个点的坐标
  129. }
  130. },
  131. mounted() {
  132. this.initCanvas()
  133. },
  134. watch: {
  135. width: {
  136. handler(newVal) {
  137. this.canvasWidth = Number(newVal)
  138. },
  139. immediate: true
  140. },
  141. height: {
  142. handler(newVal) {
  143. this.canvasHeight = Number(newVal)
  144. },
  145. immediate: true
  146. },
  147. color: {
  148. handler(newVal) {
  149. this.lineColor = newVal
  150. },
  151. immediate: true
  152. },
  153. thickness: {
  154. handler(newVal) {
  155. this.lineWidth = Number(newVal)
  156. },
  157. immediate: true
  158. }
  159. },
  160. methods: {
  161. initCanvas() {
  162. // #ifndef APP-NVUE
  163. const ctx = uni.createCanvasContext(this.canvasId, this)
  164. this.ctx = ctx
  165. this.clearCanvas()
  166. // #endif
  167. // #ifdef APP-NVUE
  168. // NVUE环境下的处理
  169. // #endif
  170. },
  171. touchStart(e) {
  172. if (!this.ctx) return
  173. this.isDrawing = true
  174. this.isEmpty = false
  175. this.currentPath = []
  176. const { x, y } = this.getCanvasPoint(e)
  177. this.ctx.beginPath()
  178. this.ctx.moveTo(x, y)
  179. this.ctx.setLineCap('round')
  180. this.ctx.setLineJoin('round')
  181. this.ctx.setStrokeStyle(this.lineColor)
  182. this.ctx.setLineWidth(this.lineWidth)
  183. // 记录起始点
  184. this.currentPath.push({
  185. x,
  186. y,
  187. type: 'start',
  188. color: this.lineColor,
  189. width: this.lineWidth
  190. })
  191. // 保存上一个点
  192. this.lastPoint = { x, y }
  193. // 阻止默认事件以提高性能
  194. e.preventDefault()
  195. },
  196. touchMove(e) {
  197. if (!this.isDrawing || !this.ctx) return
  198. // 阻止默认事件以提高性能
  199. e.preventDefault()
  200. const { x, y } = this.getCanvasPoint(e)
  201. // 使用更密集的点采样确保线条连贯性
  202. if (this.lastPoint) {
  203. // 计算两点间距离
  204. const distance = Math.sqrt(Math.pow(x - this.lastPoint.x, 2) + Math.pow(y - this.lastPoint.y, 2))
  205. // 根据距离确定插值点数量,确保点间距不超过1像素以获得更平滑的线条
  206. const steps = Math.max(1, Math.floor(distance / 1))
  207. // 在两点间插入插值点
  208. for (let i = 1; i <= steps; i++) {
  209. const t = i / steps
  210. const midX = this.lastPoint.x + (x - this.lastPoint.x) * t
  211. const midY = this.lastPoint.y + (y - this.lastPoint.y) * t
  212. this.ctx.lineTo(midX, midY)
  213. this.ctx.stroke()
  214. this.currentPath.push({
  215. x: midX,
  216. y: midY,
  217. type: 'move'
  218. })
  219. }
  220. } else {
  221. this.ctx.lineTo(x, y)
  222. this.ctx.stroke()
  223. this.currentPath.push({
  224. x,
  225. y,
  226. type: 'move'
  227. })
  228. }
  229. this.ctx.draw(true)
  230. // 更新上一个点
  231. this.lastPoint = { x, y }
  232. },
  233. touchEnd(e) {
  234. if (!this.isDrawing || !this.ctx) return
  235. this.isDrawing = false
  236. this.ctx.closePath()
  237. this.lastPoint = null
  238. // 将当前路径加入栈中用于回退
  239. if (this.currentPath.length > 0) {
  240. this.pathStack.push([...this.currentPath])
  241. }
  242. },
  243. // 获取相对于canvas的坐标点
  244. getCanvasPoint(e) {
  245. const touch = e.touches[0]
  246. const rect = uni.createSelectorQuery().in(this).select('.u-signature__canvas').boundingClientRect()
  247. return new Promise((resolve) => {
  248. rect.boundingClientRect(data => {
  249. const x = touch.x - data.left
  250. const y = touch.y - data.top
  251. resolve({ x, y })
  252. }).exec()
  253. })
  254. },
  255. // 同步获取canvas坐标点(兼容处理)
  256. getCanvasPoint(e) {
  257. const touch = e.touches[0]
  258. const canvas = uni.createSelectorQuery().in(this).select('.u-signature__canvas')
  259. // 返回一个包含坐标的对象
  260. return {
  261. x: touch.x,
  262. y: touch.y
  263. }
  264. },
  265. // 选择颜色
  266. selectColor(color) {
  267. this.lineColor = color
  268. },
  269. // 回退操作
  270. undo() {
  271. if (this.pathStack.length === 0) return
  272. // 弹出最后一个路径
  273. this.pathStack.pop()
  274. // 重新绘制
  275. this.redraw()
  276. },
  277. // 重新绘制所有路径
  278. redraw() {
  279. this.clearCanvas()
  280. if (this.pathStack.length === 0) {
  281. this.isEmpty = true
  282. return
  283. }
  284. this.isEmpty = false
  285. // #ifndef APP-NVUE
  286. this.pathStack.forEach(path => {
  287. if (path.length === 0) return
  288. this.ctx.beginPath()
  289. this.ctx.setLineCap('round')
  290. this.ctx.setLineJoin('round')
  291. let lastPoint = null
  292. path.forEach((point, index) => {
  293. if (index === 0 && point.type === 'start') {
  294. // 设置起始点样式
  295. this.ctx.setStrokeStyle(point.color)
  296. this.ctx.setLineWidth(point.width)
  297. this.ctx.moveTo(point.x, point.y)
  298. lastPoint = { x: point.x, y: point.y }
  299. } else if (point.type === 'move') {
  300. this.ctx.lineTo(point.x, point.y)
  301. lastPoint = { x: point.x, y: point.y }
  302. }
  303. })
  304. this.ctx.stroke()
  305. this.ctx.draw(true)
  306. })
  307. // #endif
  308. },
  309. // 清空画布
  310. clear() {
  311. this.pathStack = []
  312. this.currentPath = []
  313. this.isEmpty = true
  314. this.lastPoint = null
  315. this.clearCanvas()
  316. },
  317. // 清空画布内容
  318. clearCanvas() {
  319. if (!this.ctx) return
  320. // #ifndef APP-NVUE
  321. this.ctx.setFillStyle(this.bgColor)
  322. this.ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight)
  323. this.ctx.draw()
  324. // #endif
  325. },
  326. // 导出签名图片
  327. exportSignature() {
  328. if (this.isEmpty) return
  329. // #ifndef APP-NVUE
  330. uni.canvasToTempFilePath({
  331. canvasId: this.canvasId,
  332. fileType: 'png',
  333. quality: 1,
  334. success: (res) => {
  335. this.$emit('confirm', res.tempFilePath)
  336. },
  337. fail: (err) => {
  338. this.$emit('error', err)
  339. }
  340. }, this)
  341. // #endif
  342. // #ifdef APP-NVUE
  343. // NVUE环境下可能需要特殊处理
  344. // #endif
  345. },
  346. // 切换笔画设置显示
  347. toggleBrushSettings() {
  348. this.showBrushSettings = !this.showBrushSettings;
  349. if (this.showBrushSettings) {
  350. this.showColorSettings = false;
  351. }
  352. },
  353. // 切换颜色设置显示
  354. toggleColorSettings() {
  355. this.showColorSettings = !this.showColorSettings;
  356. if (this.showColorSettings) {
  357. this.showBrushSettings = false;
  358. }
  359. },
  360. }
  361. }
  362. </script>
  363. <style lang="scss" scoped>
  364. .u-signature {
  365. display: flex;
  366. flex-direction: column;
  367. &__canvas-wrap {
  368. border: 1px solid #e0e0e0;
  369. border-radius: 4px;
  370. overflow: hidden;
  371. }
  372. &__canvas {
  373. width: 100%;
  374. height: 100%;
  375. }
  376. &__toolbar {
  377. margin-top: 5px;
  378. background-color: #fff;
  379. }
  380. &__toolbar-icons {
  381. display: flex;
  382. justify-content: space-between;
  383. align-items: center;
  384. padding: 1px 0;
  385. // border: 1px solid #e0e0e0;
  386. border-radius: 4px;
  387. }
  388. &__toolbar-icon {
  389. padding: 5px;
  390. }
  391. &__brush-settings,
  392. &__color-settings {
  393. margin-top: 15px;
  394. padding: 1px;
  395. // border: 1px solid #e0e0e0;
  396. border-radius: 4px;
  397. }
  398. &__progress {
  399. &-label {
  400. display: block;
  401. margin-bottom: 10px;
  402. font-size: 14px;
  403. color: #999;
  404. }
  405. }
  406. &__color-picker {
  407. margin-bottom: 10px;
  408. }
  409. &__color-label {
  410. display: block;
  411. margin-bottom: 10px;
  412. font-size: 14px;
  413. color: #999;
  414. }
  415. &__colors {
  416. display: flex;
  417. flex-direction: row;
  418. flex-wrap: wrap;
  419. gap: 10px;
  420. }
  421. &__color-item {
  422. width: 30px;
  423. height: 30px;
  424. border-radius: 50%;
  425. border: 2px solid #f0f0f0;
  426. cursor: pointer;
  427. &--active {
  428. border-color: #2979ff;
  429. transform: scale(1.1);
  430. }
  431. }
  432. &__actions {
  433. display: flex;
  434. flex-direction: row;
  435. gap: 10px;
  436. justify-content: center;
  437. }
  438. }
  439. </style>