åˆæˆï¼šå¦‚何根æ®å•é€‰æŒ‰é’®ä¸­çš„选定选项动æ€æ›´æ”¹ç”»å¸ƒæ–°å›¾çº¸ï¼Ÿ

时间:2015-09-01 07:09:40

标签: javascript html5 canvas html5-canvas compositing

我有related question个月之å‰å…³äºŽé€šè¿‡Compositing(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">&#215;</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];

但是当我点击一个å•é€‰æŒ‰é’®æ—¶ï¼Œæˆ‘无法动æ€åœ°æ”¹å˜å®ƒã€‚

我éžå¸¸æ„Ÿè°¢ä»»ä½•å»ºè®®ï¼Œå»ºè®®æˆ–答案。谢谢。

1 个答案:

答案 0 :(得分:2)

您å¯ä»¥ä½¿ç”¨æ­¤jQuery选择器收å¬æ¨¡æ€å•é€‰æŒ‰é’®çš„点击次数:

#modal input[type=radio]

然åŽæ ¹æ®å•å‡»çš„å•é€‰æŒ‰é’®é‡ç»˜åœºæ™¯ï¼š

$('#modal input[type=radio]').click(function(){
    var imageName=$(this).val();
    if(imageName=='paw'){
        // draw the paw in the specified colors
    }else if(imageName=='flower'){
        // draw the flower in the specified colors
    }
});

[添加:使用åˆæˆæ¥åˆ¶ä½œå›¾åƒ+背景]

这是一个é‡ç»˜åŠŸèƒ½ï¼Œå¯ä»¥æŽ¥å—imageObject,背景颜色和å‰æ™¯è‰²å¹¶ä½¿ç”¨åˆæˆæ¥é‡æ–°ç€è‰²å…·æœ‰æŒ‡å®šèƒŒæ™¯çš„imageObject:

function redraw(img,background,foreground){
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.drawImage(img,0,0);
    ctx.globalCompositeOperation='source-in';
    ctx.fillStyle=foreground;
    ctx.fillRect(0,0,canvas.width,canvas.height);
    ctx.globalCompositeOperation='destination-over';
    ctx.fillStyle=background;
    ctx.fillRect(0,0,canvas.width,canvas.height);
    ctx.globalCompositeOperation='source-over';
}