成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

利用vue制作在線涂鴉板

nemo / 2906人閱讀

摘要:撤銷清空等操作撤銷前進(jìn)清空清空前后數(shù)據(jù)恢復(fù)到默認(rèn)數(shù)據(jù)地址查看代碼

效果展示

Canvas API簡(jiǎn)介 調(diào)用方法

getImageData() 返回ImageData對(duì)象,該對(duì)象為畫布上指定的矩形復(fù)制像素?cái)?shù)據(jù)

putImageData() 把圖像數(shù)據(jù)(從指定的 ImageData 對(duì)象)放回畫布上

clearRect() 在給定的矩形內(nèi)清除指定的像素

toDataURL() 返回canvas圖像的URL

lineTo() 添加一個(gè)新點(diǎn),創(chuàng)建從該點(diǎn)到最后指定點(diǎn)的線條

stroke() 繪制已定義的路徑

beginPath() 起始一條路徑,或重置當(dāng)前路徑

moveTo() 把路徑移動(dòng)到畫布中的指定點(diǎn),不創(chuàng)建線條

調(diào)用屬性

strokeStyle 設(shè)置或返回用于筆觸的顏色、漸變或模式

shadowBlur 設(shè)置或返回用于陰影的模糊級(jí)別

shadowColor 設(shè)置或返回用于陰影的顏色

lineWidth 設(shè)置或返回當(dāng)前的線條寬度

更多API請(qǐng)參考 canvas基本使用

功能需求說(shuō)明

基礎(chǔ)線條繪制功能

筆觸顏色修改

筆刷粗細(xì)調(diào)整

撤回、前進(jìn)、情況功能

生成圖片

初始化數(shù)據(jù)

colors: 筆觸顏色列表

brushs: 筆刷對(duì)應(yīng)的粗細(xì)

context: canvas context

imgUrl: 用于存放保存圖片的地址

canvasMoveUse: 是否允許執(zhí)行move時(shí)候繪制線條

preDrawAry: 存儲(chǔ)當(dāng)前表面狀態(tài)數(shù)組-上一步

nextDrawAry: 存儲(chǔ)當(dāng)前表面狀態(tài)數(shù)組-下一步

middleAry: 中間數(shù)組

lineWidth: 線條寬度

lineColor: 線條顏色

shadowBlur: 陰影

data() {
  return {
    colors: ["#fef4ac","#0018ba","#ffc200","#f32f15","#cccccc","#5ab639"],
    brushs: [{
            className: "small fa fa-paint-brush",
            lineWidth: 3
          },{
            className: "middle fa fa-paint-brush",
            lineWidth: 6
          },{
            className: "big fa fa-paint-brush",
            lineWidth: 12
          }],
    context: {},
    imgUrl: [],
    canvasMoveUse: true,
    preDrawAry: [],
    nextDrawAry: [],
    middleAry: [],
    config: {
      lineWidth: 1,
      lineColor: "#f2849e",
      shadowBlur: 2
    }
  }
}
設(shè)置繪畫配置
  setCanvasStyle() {
    this.context.lineWidth = this.config.lineWidth
    this.context.shadowBlur = this.config.shadowBlur
    this.context.shadowColor = this.config.lineColor
    this.context.strokeStyle = this.config.lineColor
  }

筆觸顏色及粗細(xì)相關(guān)設(shè)置(點(diǎn)擊修改config數(shù)據(jù)):


  • 
     
    畫筆的移動(dòng)操作
    // 當(dāng)在屏幕中移動(dòng)時(shí)即開始繪制準(zhǔn)備
    beginPath(e){
      const canvas = document.querySelector("#canvas")
      if (e.target !== canvas) {
        this.context.beginPath()
      }
    }
    // 在canvas中鼠標(biāo)按下
     canvasDown(e) {
      // 讓move方法可用
      this.canvasMoveUse = true
      // client是基于整個(gè)頁(yè)面的坐標(biāo)
      // offset是cavas距離頂部以及左邊的距離
      const canvasX = e.clientX - e.target.parentNode.offsetLeft
      const canvasY = e.clientY - e.target.parentNode.offsetTop
      // 設(shè)置canvas的配置
      this.setCanvasStyle()
      //清除子路徑
      this.context.beginPath()
      // 移動(dòng)的起點(diǎn)
      this.context.moveTo(canvasX, canvasY)
      //當(dāng)前繪圖表面狀態(tài)
      const preData = this.context.getImageData(0, 0, 600, 400)
      //當(dāng)前繪圖表面進(jìn)棧
      // 按下相當(dāng)于新的操作的開始,所以把當(dāng)前記錄數(shù)據(jù)放到prev中
      this.preDrawAry.push(preData)
    },
    // canvas中鼠標(biāo)移動(dòng)
    canvasMove(e) {
      if(this.canvasMoveUse) {
        // 只有允許移動(dòng)時(shí)調(diào)用
        const t = e.target
        let canvasX
        let canvasY
        // 由于手機(jī)端和pc端獲取頁(yè)面坐標(biāo)方式不同,所以需要做出判斷
        if(this.isPc()){
          canvasX = e.clientX - t.parentNode.offsetLeft
          canvasY = e.clientY - t.parentNode.offsetTop
        }else {
          canvasX = e.changedTouches[0].clientX - t.parentNode.offsetLeft
          canvasY = e.changedTouches[0].clientY - t.parentNode.offsetTop
        }
        // 連接到移動(dòng)的位置并上色
        this.context.lineTo(canvasX, canvasY)
        this.context.stroke()
      }
    },
    // canvas中鼠標(biāo)放開
    canvasUp(e){
      const preData = this.context.getImageData(0, 0, 600, 400)
      if (!this.nextDrawAry.length) {
        // 在沒有撤銷過的情況下,將當(dāng)前數(shù)據(jù)放入prev
        //當(dāng)前繪圖表面進(jìn)棧
        this.middleAry.push(preData)
      } else {
        // 在撤銷的情況下,將在后面步驟的數(shù)據(jù)情況記錄
        this.middleAry = []
        this.middleAry = this.middleAry.concat(this.preDrawAry)
        this.middleAry.push(preData)
        this.nextDrawAry = []
      }
      // 設(shè)置move時(shí)不可繪制
      this.canvasMoveUse = false
    }

    為了保證移動(dòng)端的可用性,加入touchstart等。

    撤銷清空等操作
    // 撤銷
    if (this.preDrawAry.length) {
      const popData = this.preDrawAry.pop()
      const midData = this.middleAry[this.preDrawAry.length + 1]
      this.nextDrawAry.push(midData)
      this.context.putImageData(popData, 0, 0)
    }
    // 前進(jìn)
    if (this.nextDrawAry.length) {
      const popData = this.nextDrawAry.pop()
      const midData = this.middleAry[this.middleAry.length - this.nextDrawAry.length - 2]
      this.preDrawAry.push(midData)
      this.context.putImageData(popData, 0, 0)
    }
    // 清空
    this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height)
    // 清空前后數(shù)據(jù)
    this.preDrawAry = []
    this.nextDrawAry = []
    // middleAry恢復(fù)到默認(rèn)數(shù)據(jù)
    this.middleAry = [this.middleAry[0]]

    demo地址

    查看代碼

    文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

    轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/81082.html

    相關(guān)文章

    • 前端文檔收集

      摘要:系列種優(yōu)化頁(yè)面加載速度的方法隨筆分類中個(gè)最重要的技術(shù)點(diǎn)常用整理網(wǎng)頁(yè)性能管理詳解離線緩存簡(jiǎn)介系列編寫高性能有趣的原生數(shù)組函數(shù)數(shù)據(jù)訪問性能優(yōu)化方案實(shí)現(xiàn)的大排序算法一怪對(duì)象常用方法函數(shù)收集數(shù)組的操作面向?qū)ο蠛驮屠^承中關(guān)鍵詞的優(yōu)雅解釋淺談系列 H5系列 10種優(yōu)化頁(yè)面加載速度的方法 隨筆分類 - HTML5 HTML5中40個(gè)最重要的技術(shù)點(diǎn) 常用meta整理 網(wǎng)頁(yè)性能管理詳解 HTML5 ...

      jsbintask 評(píng)論0 收藏0
    • 前端文檔收集

      摘要:系列種優(yōu)化頁(yè)面加載速度的方法隨筆分類中個(gè)最重要的技術(shù)點(diǎn)常用整理網(wǎng)頁(yè)性能管理詳解離線緩存簡(jiǎn)介系列編寫高性能有趣的原生數(shù)組函數(shù)數(shù)據(jù)訪問性能優(yōu)化方案實(shí)現(xiàn)的大排序算法一怪對(duì)象常用方法函數(shù)收集數(shù)組的操作面向?qū)ο蠛驮屠^承中關(guān)鍵詞的優(yōu)雅解釋淺談系列 H5系列 10種優(yōu)化頁(yè)面加載速度的方法 隨筆分類 - HTML5 HTML5中40個(gè)最重要的技術(shù)點(diǎn) 常用meta整理 網(wǎng)頁(yè)性能管理詳解 HTML5 ...

      muddyway 評(píng)論0 收藏0
    • STM32CubeMX學(xué)習(xí)教程之硬件I2C讀取光照度

      摘要:使用庫(kù)讀寫環(huán)境光照度傳感器本文將教大家如何快速使用庫(kù)讀取光照度數(shù)據(jù)。五實(shí)驗(yàn)樣機(jī)測(cè)試展示通過之前配置好的面板,通過涂鴉智能進(jìn)行配網(wǎng)實(shí)時(shí)采集光照度傳感器的數(shù)據(jù)。 使用STM32 HAL庫(kù)讀寫環(huán)境光照度傳感器(BH1750) 本文將教大家如何快速使用STM32HAL庫(kù)讀取光照度數(shù)據(jù)。 實(shí)現(xiàn)功能:通...

      tinylcy 評(píng)論0 收藏0
    • Android:涂鴉tuytaSDK實(shí)現(xiàn)邏輯

      摘要:開啟掃描時(shí)需要設(shè)備處于配網(wǎng)狀態(tài)一分類配網(wǎng)子設(shè)備可以通過使用手機(jī)藍(lán)牙直接掃描獲取設(shè)備到設(shè)備基礎(chǔ)信息,再使用配網(wǎng)接口實(shí)現(xiàn)設(shè)備的本地配網(wǎng)。 ? (一)分類 ? (二)設(shè)備配置 ? (三)設(shè)備管理 ? ? 設(shè)備管理,大體分為兩類,mesh 和 其他 ? ? 獲取設(shè)備列表,給涂鴉sdk發(fā)送當(dāng)前房間id...

      MorePainMoreGain 評(píng)論0 收藏0

    發(fā)表評(píng)論

    0條評(píng)論

    nemo

    |高級(jí)講師

    TA的文章

    閱讀更多
    最新活動(dòng)
    閱讀需要支付1元查看
    <