• canvas画点、线


    效果图如下:

    1. <template>
    2. <canvas ref="canvas" class="canvas" width="800" height="600">canvas>
    3. <hr />
    4. <button id="boldBtn" type="button" ref="boldBtn" @click="boldBtn_click">
    5. 粗线条
    6. button>
    7. <button id="thinBtn" type="button" ref="thinBtn" @click="thinBtn_click">
    8. 细线条
    9. button>
    10. <button id="saveBtn" type="button" ref="saveBtn" @click="saveBtn_click">
    11. 保存签名
    12. button>
    13. <input
    14. type="color"
    15. name=""
    16. id="color"
    17. value=""
    18. ref="color"
    19. @input="colorChnage"
    20. />
    21. <button id="clearBtn" ref="clearBtn" @click="clearBtn_click">橡皮擦button>
    22. <button id="nullBtn" ref="nullBtn" @click="nullBtn_click">清空画布button>
    23. template>
    24. <script setup lang="ts">
    25. import { ref, onMounted, reactive, Ref } from 'vue'
    26. //1.获取画布和上下文对象
    27. const canvas = ref<HTMLCanvasElement | null>(null)
    28. let ctx = reactive(null)
    29. //2.获取输入框和按钮
    30. //设置画笔的粗细:粗
    31. const boldBtn = ref()
    32. //设置画笔的粗细:细
    33. const thinBtn = ref()
    34. //保存签名
    35. const saveBtn = ref()
    36. //签名颜色选择
    37. const color = ref()
    38. //橡皮擦按钮
    39. const clearBtn = ref()
    40. //清空画布
    41. const nullBtn = ref()
    42. //设置允许绘制的变量
    43. let isDraw = false
    44. const initContext = () => {
    45. if (canvas.value?.getContext) {
    46. ctx = canvas.value.getContext('2d')
    47. //画笔线条圆润
    48. ctx.lineJoin = 'round'
    49. ctx.lineCap = 'round'
    50. }
    51. }
    52. const mouseListner = () => {
    53. //获取鼠标落下点的位置,将画笔移动到该处
    54. canvas.value!.onmousedown = (e) => {
    55. isDraw = true
    56. ctx.beginPath()
    57. let x = e.pageX - canvas.value!.offsetLeft
    58. let y = e.pageY - canvas.value!.offsetTop
    59. ctx.moveTo(x, y)
    60. //console.log(x, y)
    61. }
    62. canvas.value!.onmousemove = (e) => {
    63. if (isDraw) {
    64. let x = e.pageX - canvas.value!.offsetLeft
    65. let y = e.pageY - canvas.value!.offsetTop
    66. ctx.lineTo(x, y)
    67. ctx.stroke()
    68. }
    69. }
    70. //鼠标离开画布结束绘画
    71. canvas.value!.onmouseleave = () => {
    72. isDraw = false
    73. ctx.closePath()
    74. }
    75. //鼠标弹起结束绘画
    76. canvas.value!.onmouseup = () => {
    77. isDraw = false
    78. ctx.closePath()
    79. }
    80. }
    81. const boldBtn_click = () => {
    82. ctx.globalCompositeOperation = 'source-over'
    83. ctx.lineWidth = 20
    84. addClassStyle(boldBtn.value)
    85. }
    86. const thinBtn_click = () => {
    87. ctx.globalCompositeOperation = 'source-over'
    88. ctx.lineWidth = 2
    89. addClassStyle(thinBtn.value)
    90. }
    91. const saveBtn_click = () => {
    92. let urlData: string | undefined = canvas.value!.toDataURL()
    93. // let image: HTMLImageElement | undefined = new Image()
    94. // image.src = urlData
    95. // document.body.appendChild(image)
    96. let downloadA = document.createElement('a')
    97. downloadA.setAttribute('download', '炫酷签名')
    98. downloadA.href = urlData
    99. downloadA.click()
    100. }
    101. const clearBtn_click = () => {
    102. ctx.globalCompositeOperation = 'destination-out'
    103. ctx.lineWidth = 30
    104. addClassStyle(clearBtn.value)
    105. }
    106. const nullBtn_click = () => {
    107. ctx.clearRect(0, 0, 800, 600)
    108. addClassStyle(nullBtn.value)
    109. }
    110. const addClassStyle = (thisBtn: any) => {
    111. boldBtn.value.classList.remove('active')
    112. thinBtn.value.classList.remove('active')
    113. clearBtn.value.classList.remove('active')
    114. nullBtn.value.classList.remove('active')
    115. thisBtn.classList.add('active')
    116. }
    117. const colorChnage = () => {
    118. //console.log(color.value.value)
    119. ctx.strokeStyle = color.value.value
    120. }
    121. onMounted(() => {
    122. initContext()
    123. mouseListner()
    124. })
    125. script>
    126. <style lang="less" scoped>
    127. .canvas {
    128. border: 1px solid black;
    129. z-index: 10;
    130. }
    131. button.active {
    132. color: #fff;
    133. background-color: orange;
    134. }
    135. style>

  • 相关阅读:
    Vulnhub | DC: 4 |【实战】
    CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
    我的Vue之旅 07 Axios + Golang + Sqlite3 实现简单评论机制
    CAD中如何绑定外部参照和revit中链接CAD功能
    【学习挑战赛】经典算法之折半查找
    goctp 委托追单
    腾讯云centos7.6安装jdk
    SpringCloud 之OpenFeign 自定义配置和使用/自定义拦截器
    Day813.什么时候需要分表分库 -Java 性能调优实战
    Docker DeviceMapper占用空间太大解决方案
  • 原文地址:https://blog.csdn.net/sinat_36194710/article/details/134454518