1. 程式人生 > >刮刮卡功能實現封裝

刮刮卡功能實現封裝

個人 scratch set offset img com str rect 功能實現

  lz項目裏會經常用到刮刮卡這個小功能,其實就是利用canvas清除圖層去實現;之前在網上看過一些封裝,感覺有些封裝的還是相對比較復雜,所以就把他封裝成了一個相對簡單點可復用的組件;

  思路很簡單,就不再一一贅述;lz直接粘貼個人的代碼;

/**
 * @param {object} config
 * @param {string} canvasId 要進行操作的圖層
 * @param { nunber } wd,ht  繪圖的寬高
 * @param { string } imgurl 刮卡圖層
 * @param { number } clearRange 刮多少下停止
 * @param { number } radius 刮開圓的半徑
 * @param { function } Start touchStart 事件回調
 * @param { function } Move touchMove 事件回調
 * @param { function } End touchEnd 事件回調
 * @param { boolean } ifend 判斷是否要結束監聽事件
 * 
 
*/ export class Scratch { constructor(config) { this.config = config; let { clearRange, canvasId, radius } = config; this.moveNum = 0; this.clearRange = clearRange || 2; this.canvasId = canvasId; this.radius = radius || 25; this.canvas = $(this
.canvasId); this.ifend = false; this.init(); } init() { this.renderAct(); this.bindEvent(); } bindEvent() { this.touchStart(); this.touchMove(); this.touchEnd(); } //初始化繪畫圖層 renderAct() { let image
= new Image(); let { wd, ht, imgurl } = this.config; image.onload = () => { [this.width, this.height] = [wd / 2, ht / 2]; let canvas = $(this.canvasId)[0]; canvas.width = this.width; canvas.height = this.height; if (canvas.getContext) { this.ctx = canvas.getContext(‘2d‘); this.ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, this.width, this.height); } }; image.src = imgurl; } /** * 取消事件,以及回調 */ offFn() { this.ifend = true; $(this.canvasId).off(‘touchstart‘); $(this.canvasId).off(‘touchmove‘); $(this.canvasId).off(‘touchend‘); } /** * touchStart事件 */ touchStart() { $(document).on(‘touchstart‘, this.canvasId, () => { if (this.ifend) return; this.offset = this.canvas.offset(); if (typeof this.config.Start === ‘function‘) { this.config.Start(); } }); } /** * touchMove事件 */ touchMove() { $(document).on(‘touchmove‘, this.canvasId, event => { if (this.ifend) return; this.rx = event.touches[0].pageX - this.offset.left; this.ry = event.touches[0].pageY - this.offset.top; this.ctx.beginPath(); this.ctx.globalCompositeOperation = ‘destination-out‘; this.ctx.arc(this.rx, this.ry, this.radius, 0, 2 * Math.PI); this.ctx.fill(); this.moveNum++; if (typeof this.config.Move === ‘function‘) { this.config.Move(); } }); } /** * touchEnd事件 */ touchEnd() { $(document).on(‘touchend‘, this.canvasId, () => { if (this.ifend) return; if (this.moveNum > this.clearRange) { // this.ctx.clearRect(0, 0, this.width, this.height); if (typeof this.config.End === ‘function‘) { this.config.End(); this.moveNum = 0; } } $(this.canvasId).off(‘touchstart‘); $(this.canvasId).off(‘touchmove‘); $(this.canvasId).off(‘touchend‘); }); } }

  使用如下:

<canvas id="ActCover"></canvas>
<script>
    new Scratch({
        canvasId: #ActCover,
        wd: 640,
        ht: 360,
        imgurl: `${Gdata.assetPath}/private/T/T042/img/T042_area_gray.png`,
        Move(){

        },        
        Start() {
                
        },
        End() {
               
        }
    });
</script>                        

  在日常項目中,我們一定有一個態度就是能提煉出來的東西一定思考怎樣提煉出來,只有多思考才能把代碼越寫越好;lz也在努力中。。。

  如有不妥,請大佬指正;

  

刮刮卡功能實現封裝