u-signature.vue 11 KB

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