vue 移動(dòng)端 canvas 觸摸板

需求

通過(guò)canvas來(lái)實(shí)現(xiàn)觸摸板來(lái)寫(xiě)下一些題目的解題步驟,然后使用設(shè)備是手機(jī)跟pad

代碼

vue

<template>
  <canvas id="canvas" @touchstart="touchstart" @touchend="touchend" @touchmove="touchmove"></canvas>
</template>

js

export default {
  data() {
    return {
      ctx: '',
      point: {
        x: 0,
        y: 0
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    /**
     * @description: canvas 初始化
     * @param {type}
     * @return:
     */
    init() {
      const canvas = document.getElementById('canvas')
      this.ctx = canvas.getContext('2d')
      this.ctx.strokeStyle = '#474E60'
      this.ctx.lineWidth = 1
    },
    /**
     * @description: 獲取相對(duì)坐標(biāo)
     * @param {type}
     * @return:
     */
    absolutePoint(event) {
      const touch = event.targetTouches[0]
      const canvas = document.getElementById('canvas')
      const react = canvas.getBoundingClientRect()
      this.point = { x: touch.pageX - react.left, y: touch.pageY - react.top }
    },
    /**
     * @description: 繪制
     * @param {type}
     * @return:
     */
    draw(event) {
      this.ctx.lineTo(this.point.x, this.point.y)
      this.ctx.stroke()
    },
    /**
     * @description: 開(kāi)始觸摸
     * @param {type}
     * @return:
     */
    touchstart(event) {
      this.absolutePoint(event)
      this.ctx.moveTo(this.point.x, this.point.y)
    },
    /**
     * @description: 觸摸結(jié)束
     * @param {type}
     * @return:
     */
    touchend(event) {},
    /**
     * @description: 觸摸移動(dòng)
     * @param {type}
     * @return:
     */
    touchmove(event) {
      this.absolutePoint(event)
      this.draw(event)
    }
  }
}

效果圖

old.gif

問(wèn)題

觸摸坐標(biāo)跟畫(huà)圖坐標(biāo)不符礼殊,然后線條模糊

解決方案

在init方法里邊重置canvas的高寬

    /**
     * @description: canvas 初始化
     * @param {type}
     * @return:
     */
    init() {
      const canvas = document.getElementById('canvas')
      const width = canvas.offsetWidth // ++
      const height = canvas.offsetHeight // ++
      this.ctx = canvas.getContext('2d')
      canvas.width = width // ++
      canvas.height = height // ++
      this.ctx.strokeStyle = '#474E60'
      this.ctx.lineWidth = 1
    },

問(wèn)題原因

canvas 的寬高屬性是必須通過(guò)屬性設(shè)置的果正,因?yàn)樾枨笤颍业腸anvas通過(guò)父元素進(jìn)行100%設(shè)置,通過(guò)重置canvas高寬可以解決問(wèn)題帜消。

完整實(shí)例

new.gif

完整代碼

<template>
  <canvas id="canvas" @touchstart="touchstart" @touchend="touchend" @touchmove="touchmove"></canvas>
</template>

<script>
export default {
  data() {
    return {
      ctx: '',
      point: {
        x: 0,
        y: 0
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    /**
     * @description: canvas 初始化
     * @param {type}
     * @return:
     */
    init() {
      const canvas = document.getElementById('canvas')
      const width = canvas.offsetWidth
      const height = canvas.offsetHeight
      this.ctx = canvas.getContext('2d')
      canvas.width = width
      canvas.height = height
      this.ctx.strokeStyle = '#474E60'
      this.ctx.lineWidth = 1
    },
    /**
     * @description: 獲取相對(duì)坐標(biāo)
     * @param {type}
     * @return:
     */
    absolutePoint(event) {
      const touch = event.targetTouches[0]
      const canvas = document.getElementById('canvas')
      const react = canvas.getBoundingClientRect()
      this.point = { x: touch.pageX - react.left, y: touch.pageY - react.top }
    },
    /**
     * @description: 繪制
     * @param {type}
     * @return:
     */
    draw(event) {
      this.ctx.lineTo(this.point.x, this.point.y)
      this.ctx.stroke()
    },
    /**
     * @description: 開(kāi)始觸摸
     * @param {type}
     * @return:
     */
    touchstart(event) {
      this.absolutePoint(event)
      this.ctx.moveTo(this.point.x, this.point.y)
    },
    /**
     * @description: 觸摸結(jié)束
     * @param {type}
     * @return:
     */
    touchend(event) {},
    /**
     * @description: 觸摸移動(dòng)
     * @param {type}
     * @return:
     */
    touchmove(event) {
      this.absolutePoint(event)
      this.draw(event)
    }
  }
}
</script>

ps: 實(shí)例是以組件形式存在

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市浓体,隨后出現(xiàn)的幾起案子泡挺,更是在濱河造成了極大的恐慌,老刑警劉巖命浴,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件娄猫,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡生闲,警方通過(guò)查閱死者的電腦和手機(jī)媳溺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)碍讯,“玉大人悬蔽,你說(shuō)我怎么就攤上這事∽叫耍” “怎么了蝎困?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵录语,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我禾乘,道長(zhǎng)澎埠,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任盖袭,我火速辦了婚禮失暂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鳄虱。我一直安慰自己弟塞,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布拙已。 她就那樣靜靜地躺著决记,像睡著了一般。 火紅的嫁衣襯著肌膚如雪倍踪。 梳的紋絲不亂的頭發(fā)上系宫,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音建车,去河邊找鬼扩借。 笑死,一個(gè)胖子當(dāng)著我的面吹牛缤至,可吹牛的內(nèi)容都是我干的潮罪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼领斥,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼嫉到!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起月洛,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤何恶,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后嚼黔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體细层,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年唬涧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了今艺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡爵卒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出撵彻,到底是詐尸還是另有隱情钓株,我是刑警寧澤实牡,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站轴合,受9級(jí)特大地震影響创坞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜受葛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一题涨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧总滩,春花似錦纲堵、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至冈涧,卻和暖如春茂附,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背督弓。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工营曼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人愚隧。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓蒂阱,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親奸攻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蒜危,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容