几个月前我有一个关于通过合成(HTML5 Canvas)为画布着色的相关问题。当我再次遇到它时,我确实以某种方式理解它现在是如何工作的。但我今天的问题是,是否可以通过单选按钮更改绘制像素的分层而不影响已选择颜色的其他图层?
为了掌握我在说什么,我创建了一个有效的JSFIDDLE。(对不起凌乱的CSS)
解释小提琴:我目前拥有的是我可以更改示例图像的背景颜色和中心的花朵。现在,如果您单击灰色按钮“更改中心图像”,将出现一个模式,显示 2 个示例中心图像,花朵和爪子。我想要实现的是根据模态框上的选中项来改变之前绘制的画布(也就是花)。假设我单击爪子图像,我应该能够看到爪子而不是花。背景的选定颜色不得受到影响。
这可能吗?如果没有,还有其他方法吗?
所以这是我的 JavaScript:
var cvs = document.getElementById("canvas");
var ctx = cvs.getContext("2d");
var centerImgDefaultColor = "#f6de16";
var baseColor = "#d85700";
var imageURLs = [];
var imagesOK = 0;
var imgs = [];
var images = ["http://s23.postimg.org/y8hbni44b/base.png","http://s10.postimg.org/592v9dsd5/flower.png","http://s10.postimg.org/592v9dsd5/flower.png"];
for (var i = 0; i < images.length; i++) {
imageURLs.push(images[i]);
}
loadAllImages();
function loadAllImages() {
for (var i = 0; i < imageURLs.length; i++) {
var img = new Image();
imgs.push(img);
img.onload = function () {
imagesOK++;
imagesAllLoaded();
};
img.src = imageURLs[i];
}
}
var imagesAllLoaded = function () {
if (imagesOK >= imageURLs.length) {
base = imgs[0];
paw = imgs[1];
overlay = imgs[2];
draw();
}
};
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.drawImage(overlay, 0, 0);
ctx.fillStyle = centerImgDefaultColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = "destination-atop";
ctx.drawImage(paw, 0, 0);
ctx.drawImage(base, 0, 0);
ctx.fillStyle = baseColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = "destination-atop";
ctx.restore();
}
$(".paw").click(function () {
centerImgDefaultColor = '#' + $( this ).attr( "value" );
draw();
});
$(".base").click(function () {
baseColor = '#' + $( this ).attr( "value" );
draw();
});
还有我的 HTML:
<div style="float:left;">
<p>Background Color</p>
<div class="base" style="width:25px;height:25px;background-color:#000000;" value="000000"></div>
<div class="base" style="width:25px;height:25px;background-color:#7da7de;" value="7da7de"></div>
<div class="base" style="width:25px;height:25px;background-color:#d85700;" value="d85700"></div>
</div>
<div style="float:right;">
<p>Center Image Color</p>
<div class="paw" style="width:25px;height:25px;background-color:#040054;" value="040054"></div>
<div class="paw" style="width:25px;height:25px;background-color:#267d00;" value="267d00"></div>
<div class="paw" style="width:25px;height:25px;background-color:#f6de16;" value="f6de16"></div>
</div>
<a class="button" href="">Change center image</a>
<div id="modal">
<a href="" class="close">×</a>
<input type="radio" class="btn-img" name="imageLayout" value="flower" checked="checked"/><img src="http://s10.postimg.org/6j3y3l979/prev_flower.png"/>
<input type="radio" class="btn-img" name="imageLayout" value="paw"/><img src="http://s1.postimg.org/gtmv5giwr/prev_paw.png"/>
</div>
<div class="modal-bg"></div>
<canvas id="canvas" width=310 height=450></canvas>
我实际上最初是在考虑获取单选按钮的值并通过单击功能将其加载到我的图像数组中,但我认为这是不可能的。
$(".btn-img").click(function() { var val =
$('input[name="imageLayout"]:checked').val();
});
所以我想在我的 JS 中使用这样的东西(假设图像来自我的服务器,所以我使用图像的直接名称):
var selectedimageLayout = $('input[name="imageLayout"]:checked').val();
var imageLayoutSample = selectedimageLayout + ".png";
并将其传递给我的图像数组:
var images = ["http://s23.postimg.org/y8hbni44b/base.png",imageLayoutSample ,imageLayoutSample];
但是当我单击单选按钮时,我无法动态地让它改变。
我将非常感谢任何建议、建议或答案。谢谢你。