2017-10-14 43 views
2

我不想檢查徑向元素的碰撞。檢測徑向像素

問題是,目前我只檢查像素爲矩形,因爲其他圖像本身就是HTML元素。

我只使用畫布繪製邊界背景來檢查alpha透明度。

this.checkCollision = function checkCollision() { 
    var width = 34; 
    var height = 32; 
    var image = _context.getImageData(_position.x - (height/2), _position.y - (width/2), width, height); 
    var pixels = image.data; 
    var size = image.data.length; 

    // HERE I WANT TO CHECK A RADIAL AREA 
    for(var index = 0; index < size; index += 4) { 
     var RED  = image.data[index]; 
     var GREEN = image.data[index + 1]; 
     var BLUE = image.data[index + 2]; 
     var ALPHA = image.data[index + 3]; 

     if(_debug) { 
      document.querySelector('#debug').innerHTML = JSON.stringify({ 
       POSITION: _position, 
       INDEX:  index, 
       COLOR: { 
        R: RED, 
        G: GREEN, 
        B: BLUE, 
        A: ALPHA 
       } 
      }, 0, 1); 
     } 

     if(ALPHA !== 0) { 
      return true; 
     } 
    } 
    _context.putImageData(image, 0, 0); 
    return false; 
}; 

預覽

enter image description here

這裏是一個工作小提琴:

https://jsfiddle.net/2bLfd6xp/

我怎麼能在getImageData選擇徑向像素範圍檢查與碰撞來自的alpha透明度?

我的想法是從這裏修改的像素數據陣列:

var image = _context.getImageData(_position.x - (height/2), _position.y - (width/2), width, height); 

,並刪除無形edges。但是,從基於矩形的像素陣列計算徑向區域以去除這些不需要的像素的最佳做法是什麼?

對於樣本:

var image = _context.getImageData(_position.x - (height/2), _position.y - (width/2), width, height); 

var radial_area = selectRadialArea(image, radius); 

function selectRadialArea(pixelArray, radius) { 
    /* 
     Modify `pixelArray` with given `radius`... 
     All pixels outside the `radius` filled with `null`... 
    */ 
    return theNewArray; 
} 

回答

2

我發現與邏輯思維的答案:

首先,我們創建一個臨時繪製背景和在這個新的領域得出兩個elemengts:

  • an 紅色矩形
  • 透明圓弧/圓形目的地 - 複合

所得Uint8ClampedArray相比Uint8ClampedArray。如果面積RED,我們隱藏的像素與 -entries:

this.rectangleToRadial = function rectangleToRadial(source, width, height) { 
    var test  = document.createElement('canvas'); 
    var context  = test.getContext('2d'); 

    // Create an transparent circle and a red removeable area 
    context.beginPath(); 
    context.fillStyle = 'rgba(255, 0, 0, 1)'; 
    context.fillRect(0, 0, width, height); 
    context.globalCompositeOperation = 'destination-out'; 
    context.arc(width/2, height/2, width/2, 0, 2 * Math.PI); 
    context.fillStyle = 'rgba(0, 0, 0, 1)'; 
    context.fill(); 

    // get the data 
    var destination = context.getImageData(0, 0, width, height); 
    var size  = destination.data.length; 

    // check the pixels 
    for(var index = 0; index < size; index += 4) { 
     var RED  = destination.data[index]; 
     var GREEN = destination.data[index + 1]; 
     var BLUE = destination.data[index + 2]; 
     var ALPHA = destination.data[index + 3]; 

     /* 
      if the >>red removeable area<< is given, null the pixel from the source 
     */ 
     if(RED == 255 && GREEN == 0 && BLUE == 0) { 
      // Remove this from source 
      source.data[index]  = null; 
      source.data[index + 1] = null; 
      source.data[index + 2] = null; 
      source.data[index + 3] = null; 
     } 
    } 

    // Return the source `Uint8ClampedArray` 
    return source; 
}; 

這是很容易,當我們試圖想:)

var image = _context.getImageData(_position.x - (height/2), _position.y - (width/2), width, height); 
    var pixels = this.rectangleToRadial(image, width, height);