時(shí)間:2023-07-25 01:51:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-07-25 01:51:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
教你用canvas打造一個(gè)炫酷的碎片切圖效果:this.imgW = 800; // 圖片原始寬this.imgH = 530; // 圖片原始高this.conW = 800; // 畫布寬this.conH = 530; // 畫布高this.dw = 16; // 單元格寬this.dh = 15; // 單元格高this.I = this.conH / this.dh; //單元行數(shù)this.J = this.conW / this.dw; // 單元列數(shù)this.DW = this.imgW / this.J; // 原圖單元寬this.DH = this.imgH / this.I; // 原圖單元高
「行數(shù) = 畫布高度 / 單元格高度;列數(shù) = 畫面寬度 / 單元格寬度」? drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)這個(gè)方法一共有9個(gè)參數(shù),作用是在畫布上繪制圖像。看到這么多參數(shù)是不是已經(jīng)被勸退了,哈哈
?
HTMLImageElement
、SVGImageElement
、HTMLVideoElement
、HTMLCanvasElement
、ImageBitmap
、OffscreenCanvas
或 VideoFrame
。image
的矩形(裁剪)選擇框的左上角 X 軸坐標(biāo)??梢允褂?3 參數(shù)或 5 參數(shù)語(yǔ)法來(lái)省略這個(gè)參數(shù)。image
的矩形(裁剪)選擇框的左上角 Y 軸坐標(biāo)。可以使用 3 參數(shù)或 5 參數(shù)語(yǔ)法來(lái)省略這個(gè)參數(shù)。image
的矩形(裁剪)選擇框的寬度。如果不說(shuō)明,整個(gè)矩形(裁剪)從坐標(biāo)的 sx
和 sy
開始,到 image
的右下角結(jié)束??梢允褂?3 參數(shù)或 5 參數(shù)語(yǔ)法來(lái)省略這個(gè)參數(shù)。使用負(fù)值將翻轉(zhuǎn)這個(gè)圖像。image
的矩形(裁剪)選擇框的高度。使用負(fù)值將翻轉(zhuǎn)這個(gè)圖像。image
的左上角在目標(biāo)畫布上 X 軸坐標(biāo)。image
的左上角在目標(biāo)畫布上 Y 軸坐標(biāo)。image
在目標(biāo)畫布上繪制的寬度。允許對(duì)繪制的 image
進(jìn)行縮放。如果不說(shuō)明,在繪制時(shí) image
寬度不會(huì)縮放。注意,這個(gè)參數(shù)不包含在 3 參數(shù)語(yǔ)法中。image
在目標(biāo)畫布上繪制的高度。允許對(duì)繪制的 image
進(jìn)行縮放。如果不說(shuō)明,在繪制時(shí) image
高度不會(huì)縮放。注意,這個(gè)參數(shù)不包含在 3 參數(shù)語(yǔ)法中。class ChipBanner { constructor() { this.cvs = document.querySelector("#chip"); this.ctx = this.cvs.getContext("2d"); this.imgList = document.querySelectorAll(".bg"); this.imgIndex = 0; this.isAnimating = false; this.imgW = 800; //圖片原始寬/高 this.imgH = 530; this.conW = 800; //畫布寬/高 this.conH = 530; this.dw = 16; //畫布單元寬/高 this.dh = 15; this.I = this.conH / this.dh; //單元行/列數(shù) this.J = this.conW / this.dw; this.DW = this.imgW / this.J; //原圖單元寬/高 this.DH = this.imgH / this.I; } init() { this.ctx.beginPath(); for (let i = 0; i < this.I; i++) { for (let j = 0; j < this.J; j++) { this.chipDraw(this.imgList[this.imgIndex], i, j); } } this.ctx.closePath(); this.ctx.stroke(); } drawText() { this.ctx.font = "150px serif"; this.ctx.strokeStyle = "white"; this.ctx.strokeText("1024", 500, 500); } chipDraw(img, i, j) { this.drawText(); //負(fù)責(zé)繪制,i: 單元行號(hào);j: 單元列號(hào) this.ctx.drawImage( img, this.DW * j, this.DH * i, this.DW, this.DH, this.dw * j, this.dh * i, this.dw, this.dh ); }}
這里正確拼出來(lái)看到的和正常圖片沒有任何區(qū)別countAround(i, j, dst) { let arr = []; for (let m = i - dst; m <= i + dst; m++) { for (let n = j - dst; n <= j + dst; n++) { if ( Math.abs(m - i) + Math.abs(n - j) == dst && m >= 0 && n >= 0 && m <= this.I - 1 && n <= this.J - 1 ) { arr.push({ x: m, y: n }); } } } return arr; }
chipClear(i, j) { this.ctx.clearRect(this.dw * j, this.dh * i, this.dw, this.dh);}
start(i, j) { if (this.isAnimating) return; this.isAnimating = true; this.imgIndex++; if (this.imgIndex > this.imgList.length - 1) this.imgIndex = 0; let _this = this, dst = 0, timer = setInterval(() => { let resArr = _this.countAround(i, j, dst); resArr.forEach((item) => { _this.chipClear(item.x, item.y); // 清除單元格 _this.chipDraw(_this.imgList[_this.imgIndex], item.x, item.y); // 繪制下一張圖片 }); if (!resArr.length) { clearInterval(timer); _this.isAnimating = false; } dst++; }, 30); }
大功告成,這樣就實(shí)現(xiàn)了一個(gè)炫酷的碎片式切圖效果了~關(guān)鍵詞:效果,打造
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。