globalCompositeOperation在背景上绘制白色

时间:2015-05-26 02:38:57

标签: javascript html5 canvas alpha masking

我在javascript中为html5画布使用globalCompositeOperation时出现问题。

https://jsfiddle.net/6j51kxeh /

我正在使用“目的地”在心灵的精灵中画一个黑色方块,所以当玩家失去那颗心时,它会显示出来。它吸引了黑色的心脏,但它在其他一切的顶部都是白色的。如果你注释掉globalCompositeOperation行,你会看到它是在其他东西之上绘制的(只是来自同一个精灵表的一组随机精灵)。谢谢你的帮助。

我也很难链接到jsfiddle而且我已经玩完了。

var canvas = document.createElement("canvas");    

canvas.height = window.innerHeight;
canvas.width  = window.innerWidth;

var scale = 4;

document.body.appendChild(canvas);

var draw = canvas.getContext("2d");

draw.imageSmoothingEnabled = false;
draw.mozImageSmoothingEnabled = false;
draw.oImageSmoothingEnabled = false;
draw.webkitImageSmoothingEnabled = false;

var assetsToLoad = [];
var assetsLoaded = 0;

var sprites = new Image();
sprites.onload = loadHandler;
sprites.src = "https://dl.dropboxusercontent.com/u/38825680/Site/images/sprites.png";
assetsToLoad.push(sprites);


function loadHandler() {
    assetsLoaded++;
    if(assetsLoaded === assetsToLoad.length) {
        console.log(assetsLoaded + " asset[s] loaded");

        Draw();
    }
};

function Draw() {
    draw.save();
    draw.scale(scale,scale);

    draw.drawImage(sprites,0,64,64,64,0,0,64,64);
    // draw.drawImage(sprites,112,80,16,16,0,0,16,16);
    draw.fillStyle = "black";
    draw.fillRect(0,0,16,16);

    //draw.globalCompositeOperation = "destination-in";

    draw.drawImage(sprites,96,48,16,16,0,0,16,16);


    draw.restore();
}

1 个答案:

答案 0 :(得分:0)

destination-in合成具有破坏性,因为它会擦除画布上的所有内容,除非新(心)与任何现有的非透明像素重叠。所以这种形式的合成不太可能是你需要的。

澄清一下:你想在另一个精灵图像上绘制心形精灵,然后从心脏被添加到的另一个精灵图像中清除该精灵?

如果是,那么:

  • 要添加心脏,请在另一个精灵上绘制心形精灵。
  • 要清除心脏,只需重绘另一个精灵。

示例代码和演示:

var canvas = document.createElement("canvas");    

canvas.height = 640;//window.innerHeight;
canvas.width  = 960;//window.innerWidth;

var scale = 4;

document.body.appendChild(canvas);

var draw = canvas.getContext("2d");

draw.imageSmoothingEnabled = false;
draw.mozImageSmoothingEnabled = false;
draw.oImageSmoothingEnabled = false;
draw.webkitImageSmoothingEnabled = false;

var assetsToLoad = [];
var assetsLoaded = 0;

var sprites = new Image();
sprites.onload = loadHandler;

sprites.src = "https://dl.dropboxusercontent.com/u/38825680/Site/images/sprites.png";
assetsToLoad.push(sprites);


function loadHandler() {
  assetsLoaded++;
  if(assetsLoaded === assetsToLoad.length) {
    console.log(assetsLoaded + " asset[s] loaded");

    draw.scale(scale,scale);
    Draw();
  }
};

function Draw() {
  draw.drawImage(sprites,0,64,64,64,0,0,64,64);
}



function addHeart(x,y){
  draw.fillStyle = "black";
  draw.fillRect(x,y,16,16);
  draw.drawImage(sprites,96,48,16,16,x,y,16,16);
}

function removeHeart(x,y,spriteX,spriteY){
  draw.clearRect(x,y,16,16);
  draw.drawImage(sprites,spriteX,spriteY+64,16,16,x,y,16,16)
}

$('#add').click(function(){
  addHeart(0,0);
});

$('#remove').click(function(){
  removeHeart(0,0,0,0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button id=add>Add heart</button>
<button id=remove>Remove heart</button>