亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > HTML5 > 正文

詳解Canvas事件綁定

2024-09-05 07:19:22
字體:
來源:轉載
供稿:網友

眾所周知canvas是位圖,在位圖里我們可以在里面畫各種東西,可以是圖片,可以是線條等等。那我們想給canvas里的某一張圖片添加一個點擊事件該怎么做到。而js只能監聽到canvas的事件,很明顯這個圖片是不存在與dom里面的圖片只是畫在了canvas里而已。下面我就來簡單的實現一個canvas內部各個圖片的事件綁定。

我先來講下實現原理:其實就是canvas綁定相關事件,在通過記錄圖片所在canvas的坐標,判斷事件作用于哪個圖片中。這樣講是不是感覺跟事件代理有點相似咧。不過實現起來還是有稍許復雜的。

ps:下面的代碼我是用ts寫的,大家當es6看就好了,稍有不同的可以查看

typescript的文檔 (typescript真的很好用,建議大家多多了解)。

1、建立圖片和canvas之間的聯系(這里我用色塊來代替圖片)

這里要色塊和canvas建立一定的聯系,而不是單純的渲染。還要記錄色塊所在坐標、寬高。我們先一步一步來實現

首先寫基本的html頁面創建一個canvas:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>canvas事件</title>    <style>        html, body {            height: 100%;            background: #eee;        }        canvas {            background: #fff;            display: block;            margin: 0 auto;        }    </style></head><body>    <canvas width="500" height="500" id="canvas"></canvas></body>

下一步,我們要定一個Canvas的類,這個類應該要有些什么功能呢?

  • 要有對應的canvas。
  • 裝色塊數據的容器。
  • 有添加色塊的方法。
  • 渲染色塊的方法。
  • 渲染所有色塊的方法。

因為色塊也有自己的一些參數,為了方便拓展,我們也為色塊定一個類,這類需要的功能有:

寬、高、顏色、坐標(x,y),還有Canvas實例;初步就定這幾個吧

ok開始寫

// Canvas類class Canvas {  blockList: Block[]  ctx: any  canvas: any  createBlock (option) {        option.Canvas = this    this.blockList.push(new Block(option))    this.painting()  }  rendering (block) {               // 渲染色塊函數    this.ctx.fillStyle = block.color    this.ctx.fillRect(block.x, block.y, block.w, block.h)  }    painting () {                           // 將容器里的色塊全部渲染到canvas    // 清空畫布(渲染之前應該將老的清空)    this.ctx.fillStyle = '#fff'    this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)        this.blockList.forEach(ele => {            this.rendering(ele)        })    }  constructor (ele) {               // 初始化函數(輸入的是canvas)    // 設置canvas    this.canvas = ele    this.ctx = this.canvas.getContext('2d')    // 色塊容器    this.blockList = []  }}class Block {  w: number  h: number  x: number  y: number  color: string  Canvas: Canvas  hierarchy: number    constructor ({ w, h, x, y, color, Canvas }) {     // 初始化設置色塊相關屬性        this.w = w        this.h = h        this.x = x        this.y = y        this.color = color        this.Canvas = Canvas    }}

下面運行一波試試

// 創建Canvas實例,并添加藍色個寬高100px,位置(100,100)、(300,100)紅色和藍色的色塊  var canvas = new Canvas(document.getElementById('canvas'))    canvas.createBlock({                    // 紅色        x: 100,        y: 100,        w: 100,        h: 100,        color: '#f00'    })    canvas.createBlock({                    // 藍色        x: 100,        y: 100,        w: 300,        h: 100,        color: '#00f'    })

運行結果如下:

 

2、給色塊添加點擊事件

這里并不能直接給色塊添加點擊事件的,所以要通過坐標的方式判斷目前點擊的是哪個色塊。

  • 先給canvas添加點擊事件。
  • 判斷色塊區域。
  • 執行相應事件。
class Block {    // ...省略部分代碼    checkBoundary (x, y) {              // 判斷邊界方法        return x > this.x && x < (this.x + this.w) && y > this.y && y < (this.y + this.h)    }    mousedownEvent () {                     // 點擊事件        console.log(`點擊了顏色為${this.color}的色塊`)    }}class Canvas {    // ...省略部分代碼    constructor (ele) {        this.canvas = ele        this.ctx = this.canvas.getContext('2d')        this.blockList = []        // 事件綁定(這里有一個要注意的,我這里用了bind方法,是為了將“mousedownEvent”方法內的this指向切換到Canvas)        this.canvas.addEventListener('click', this.mousedownEvent.bind(this))   // 點擊事件    }    mousedownEvent () {                 // 點擊事件        const x = e.offsetX        const y = e.offsetY        // 這里將點擊的坐標傳給所有色塊,根據邊界判斷方法判斷是否在點擊在內部。是的話執行色塊的事件方法。        this.blockList.forEach(ele => {            if (ele.checkBoundary(x, y)) ele.mousedownEvent(e)        })    }}

到這里為止已經實現了對不同canvas內不同色塊綁定對應的點擊事件。不過這個點擊事件是不完美的,因為目前為止我們還沒有引入層級的概念,就是說兩個色塊重疊部分點擊的話,全部都會觸發。所以我們還要給色塊加入層級的屬性。實現一個點擊某一個色塊改色塊的層級就會提升到最高。

class Block {    // ...省略部分代碼    constructor ({ w, h, x, y, color, Canvas, hierarchy }) {     // 初始化設置色塊相關屬性        this.w = w        this.h = h        this.x = x        this.y = y        this.color = color        this.Canvas = Canvas        this.hierarchy = 0    }}class Canvas {    // ...省略部分代碼    constructor (ele) {        this.canvas = ele        this.ctx = this.canvas.getContext('2d')        this.blockList = []        // 事件綁定(這里有一個要注意的,我這里用了bind方法,是為了將“mousedownEvent”方法內的this指向切換到Canvas)        this.canvas.addEventListener('click', this.mousedownEvent.bind(this))   // 點擊事件        this.nowBlock = null                // 當前選中的色塊    }    createBlock (option) {          // 創建色塊函數(這里的Block是色塊的類)      option.Canvas = this        // 創建最新的色塊的層級應該是最高的        option.hierarchy = this.blockList.length    this.blockList.push(new Block(option))    this.rendering()    }      mousedownEvent (e) {                  // 點擊事件    const x = e.offsetX    const y = e.offsetY    // 獲取點中里層級最高的色塊        this.nowBlock = (this.blockList.filter(ele => ele.checkBoundary(x, y))).pop()        // 如果沒有捕獲的色塊直接退出        if (!this.nowBlock) return    // 將點擊到的色塊層級提高到最高    this.nowBlock.hierarchy = this.blockList.length    // 重新排序(從小到大)    this.blockList.sort((a, b) => a.hierarchy - b.hierarchy)    // 在重新從0開始分配層級    this.blockList.forEach((ele, idx) => ele.hierarchy = idx)    // 重新倒序排序后再重新渲染。    this.painting()    this.nowBlock.mousedownEvent(e)     // 只觸發選中的色塊的事件  }}// 這里我們還得加入第三塊色塊與紅色色塊重疊的色塊canvas.createBlock({  x: 150,  y: 150,  w: 100,  h: 100,  color: '#0f0'})

Canvas中“mousedownEvent”方法內的代碼是有點復雜的,主要是有點繞。

  1. 首先是this.nowBlock = (this.blockList.filter(ele => ele.checkBoundary(x, y))).pop()這段代碼是怎么獲取到點擊到的色塊中層級最高的色塊。這里因為我們每次添加色塊都是設置了最高層級的,所以“blockList”內的色塊都是按層級從小到大排序的。所以我們取最后一個就可以了。
  2. 第二步就是將拿到的色塊的層級提升到最高。
  3. 第三步就是從小到大重新排列色塊。
  4. 因為第二步的時候我們修改了選中色塊的層級,導致所有色塊的層級不是連續的,為了避免層級不可控,我們還得重新定義層級。
  5. 重新渲染色塊到canvas中,因為“blockList”內的色塊是排好序的,所以按順序渲染即可。

運行后的效果就是下面這樣了:

3、實現對不同色塊進行拖拽

在上面我們已經實現了獲取不同的色塊,并修改它的層級。下面我們要實現色塊的拖拽,主要就是獲取鼠標移動過程中和一開始點擊下去時位置坐標的變化。這個原理和普通的dom拖拽實現原理一樣。

獲取點擊色塊的點,距離色塊左邊和上邊的距離(disX, disY)。

鼠標移動時,用鼠標當前距離canvas左邊和上邊的距離減去(disX, disY)這里就是色塊的x,y坐標了。

class Block {  // ...省略部分代碼  mousedownEvent (e: MouseEvent) {    /* 這里 disX和disY的計算方式: e.offsetX獲取到的是鼠標點擊距離canvas左邊的距離,this.x是色塊距離canvas左邊的距離。e.offsetX-this.x就是色塊左邊的距離。這應該很好理解了 */    const disX = e.offsetX - this.x // 點擊時距離色塊左邊的距離        const disY = e.offsetY - this.y // 點擊時距離色塊上邊的距離                // 綁定鼠標滑動事件;這里mouseEvent.offsetX同樣是鼠標距離canvas左側的距離,mouseEvent.offsetX - disX就是色塊的x坐標了。同理y也是這樣算的。最后在重新渲染就好了。    document.onmousemove = (mouseEvent) => {      this.x = mouseEvent.offsetX - disX      this.y = mouseEvent.offsetY - disY      this.Canvas.painting()        }        // 鼠標松開則清空所有事件    document.onmouseup = () => {      document.onmousemove = document.onmousedown = null    }    // console.log(`點擊了顏色為${this.color}的色塊22`)  }}

效果如下:

下面貼上完整的代碼(html和調用的方法就不放了)這個例子只是簡單實現給canvas內的內容綁定事件,大家可以實現復雜一點的,例如把色塊換成圖片,除了拖拽還以給圖片縮放,旋轉,刪除等等。

class Canvas {  blockList: Block[]  ctx: any  canvas: any  nowBlock: Block  createBlock (option) {    option.hierarchy = this.blockList.length    option.Canvas = this    this.blockList.push(new Block(option))    this.painting()  }  rendering (block) {    this.ctx.fillStyle = block.color    this.ctx.fillRect(block.x, block.y, block.w, block.h)  }  painting () {    // 清空畫布    this.ctx.fillStyle = '#fff'    this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)    this.blockList.forEach(ele => {      this.rendering(ele)    })  }  mousedownEvent (e: MouseEvent) {                  // 點擊事件    const x = e.offsetX    const y = e.offsetY    // 獲取點中里層級最高的色塊    this.nowBlock = (this.blockList.filter(ele => ele.checkBoundary(x, y))).pop()    // 如果沒有捕獲的色塊直接退出        if (!this.nowBlock) return    // 將點擊到的色塊層級提高到最高    this.nowBlock.hierarchy = this.blockList.length    // 重新排序(從小到大)    this.blockList.sort((a, b) => a.hierarchy - b.hierarchy)    // 在重新從0開始分配層級    this.blockList.forEach((ele, idx) => ele.hierarchy = idx)    // 重新倒序排序后再重新渲染。    this.painting()    this.nowBlock.mousedownEvent(e)    // this.blockList.forEach(ele => {    //   if (ele.checkBoundary(x, y)) ele.clickEvent(e)    // })  }  constructor (ele) {    this.canvas = ele    this.ctx = this.canvas.getContext('2d')    this.blockList = []    // 事件綁定    this.canvas.addEventListener('mousedown', this.mousedownEvent.bind(this))  }}class Block {  w: number  h: number  x: number  y: number  color: string  Canvas: Canvas  hierarchy: number  constructor ({ w, h, x, y, color, Canvas, hierarchy }) {    this.w = w    this.h = h    this.x = x    this.y = y    this.color = color    this.Canvas = Canvas    this.hierarchy = hierarchy  }  checkBoundary (x, y) {    return x > this.x && x < (this.x + this.w) && y > this.y && y < (this.y + this.h)  }  mousedownEvent (e: MouseEvent) {    const disX = e.offsetX - this.x    const disY = e.offsetY - this.y    document.onmousemove = (mouseEvent) => {      this.x = mouseEvent.offsetX - disX      this.y = mouseEvent.offsetY - disY      this.Canvas.painting()    }    document.onmouseup = () => {      document.onmousemove = document.onmousedown = null    }    // console.log(`點擊了顏色為${this.color}的色塊22`)  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久精品小视频| 最近2019中文免费高清视频观看www99| 国产最新精品视频| 久久九九国产精品怡红院| 91精品在线一区| 中文国产成人精品| 久久人体大胆视频| 91地址最新发布| 欧美一级片在线播放| 一区二区欧美久久| 色伦专区97中文字幕| 亚洲精品v欧美精品v日韩精品| 久久久久www| 欧美在线亚洲在线| 日本精品久久久| 午夜精品久久久久久久男人的天堂| 亚洲精品ady| 国产女人18毛片水18精品| 成人春色激情网| 亚洲国产精品高清久久久| 日韩中文字幕视频| 黑人极品videos精品欧美裸| 精品国产一区二区三区久久狼5月| www.欧美免费| 91国内揄拍国内精品对白| 成人精品福利视频| 久久91精品国产91久久久| 国产aⅴ夜夜欢一区二区三区| 国产精品高潮在线| 国产精品扒开腿爽爽爽视频| 国产主播在线一区| 欧美猛交免费看| 成人欧美一区二区三区在线湿哒哒| 欧日韩不卡在线视频| 亚洲小视频在线| 国产91精品久| 日韩av色综合| 伊人伊人伊人久久| 亚洲电影成人av99爱色| 久久久免费精品视频| 国产成人精品久久亚洲高清不卡| www.亚洲免费视频| 清纯唯美亚洲激情| 日韩av在线播放资源| 精品福利视频导航| 日韩av综合网站| 性色av一区二区三区免费| 欧美日韩一区二区免费视频| 日韩精品丝袜在线| 亚洲精美色品网站| 久久香蕉国产线看观看av| 成人激情视频小说免费下载| 欧洲日本亚洲国产区| 中文字幕亚洲欧美日韩2019| 日韩在线中文视频| 日韩一区二区三区在线播放| 精品久久久久久亚洲国产300| 日韩美女在线观看| 久久久免费精品视频| 日韩免费高清在线观看| 亚洲高清一区二| 91国自产精品中文字幕亚洲| 97色在线播放视频| 国产成人精品久久| 97在线看福利| 国产精品久久久久久久久久小说| 亚洲精品国偷自产在线99热| 一区二区三区高清国产| 国产精品99久久99久久久二8| 日本久久91av| 国产精品成人aaaaa网站| 97久久精品人人澡人人爽缅北| 亚洲精品一区二区三区不| 韩国精品美女www爽爽爽视频| 国产精品成人v| 欧美成人午夜激情视频| 91高清视频免费观看| 欧美激情视频网| 久久99精品视频一区97| 在线播放国产精品| 91精品久久久久久久久久久久久久| 精品一区精品二区| 91高潮在线观看| 国产成人精品一区二区在线| 日韩在线视频网站| 久久99精品久久久久久琪琪| 97成人精品区在线播放| 伊人伊成久久人综合网站| 亚洲精品国产精品久久清纯直播| 日韩电视剧在线观看免费网站| 中文字幕欧美精品在线| 久久久久中文字幕2018| 日本aⅴ大伊香蕉精品视频| 日韩欧美精品网站| 一本色道久久88综合日韩精品| 亚洲天堂av在线播放| 另类天堂视频在线观看| 国产精品久久久久久av福利| 欧美日韩高清在线观看| 欧美肥老妇视频| 51视频国产精品一区二区| 国产一区视频在线播放| 九九热视频这里只有精品| 91亚洲精华国产精华| 色婷婷久久av| 成人激情在线观看| 91天堂在线观看| 亚洲在线一区二区| 国产97在线视频| 国产午夜精品免费一区二区三区| 亚洲深夜福利在线| 亚洲人精选亚洲人成在线| 精品二区三区线观看| 日韩电影第一页| 韩国视频理论视频久久| 91色p视频在线| 日韩精品电影网| 超碰精品一区二区三区乱码| 国产一区二区视频在线观看| 欧美在线中文字幕| 久久夜色精品国产欧美乱| 欧美午夜精品久久久久久浪潮| 欧美成人免费全部观看天天性色| 国产精品扒开腿爽爽爽视频| 日本午夜精品理论片a级appf发布| 国产精品xxxxx| 国产精品自产拍高潮在线观看| 91夜夜揉人人捏人人添红杏| 91视频九色网站| 欧美精品少妇videofree| 色综合五月天导航| 国产精品高精视频免费| 日韩欧美在线观看| 国产精品久久久久久av福利| 欧美日韩国产色视频| 亚洲激情小视频| 欧美成人激情在线| 亚洲国产精品电影在线观看| 国产一区二区久久精品| 欧美老女人bb| 国内精品国产三级国产在线专| 国产精品成人免费电影| 亚洲欧美日韩中文在线制服| 亚洲天堂男人天堂女人天堂| 欧美精品在线免费观看| 91久久精品在线| 久久6精品影院| 97色伦亚洲国产| 国产精品一区二区三| 夜夜狂射影院欧美极品| 日韩在线观看免费全集电视剧网站| 日韩精品免费在线视频观看| 热门国产精品亚洲第一区在线| 91大神福利视频在线| 亚洲欧美国产日韩天堂区| 欧美激情综合色综合啪啪五月| 精品国产31久久久久久| 国产一区二区黑人欧美xxxx| 日韩麻豆第一页| 亚洲美女av电影| 日韩av在线电影网| 欧美日韩在线一区| 欧美激情视频免费观看| 久久人人爽人人爽爽久久|