u-signature.vue 11 KB

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