【问题标题】:Displaying different images on an HTML canvas according to selected radio button根据选定的单选按钮在 HTML 画布上显示不同的图像
【发布时间】:2017-07-18 13:01:48
【问题描述】:

使用此项目,您应该能够选择特定颜色的单选按钮,单击蓝色大按钮,相应的 150x150 像素色样将在 HTML 画布上绘制。我已经把所有东西都整理好了,直到要显示图像的部分。我卡在 switch 语句上。

我还为此制作了一个 JSFiddle:

https://jsfiddle.net/sheradon/yuqqono6/12/

$(document).ready(function() 
{       

var tabClicked = true;
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = 'http://i.imgur.com/utPXYZM.jpg';

imgArray[1] = new Image();
imgArray[1].src = 'http://i.imgur.com/j7CfXVF.jpg';

imgArray[2] = new Image();
imgArray[2].src = 'http://i.imgur.com/muNhjqq.jpg';

imgArray[3] = new Image();
imgArray[3].src = 'http://i.imgur.com/SSSSvkD.jpg';

imgArray[4] = new Image();
imgArray[4].src = 'http://i.imgur.com/KMCsKTX.jpg';

imgArray[5] = new Image();
imgArray[5].src = 'http://i.imgur.com/QNe0jAr.jpg';

imgArray[6] = new Image();
imgArray[6].src = 'http://i.imgur.com/QbzmJlE.jpg';

    $('#colorstab').click(function() 
    {        
        if(tabClicked == true)     
        {                
        $('#colorstab').animate(    
        {                
            marginTop:'-8px'      
        }, 0 );                                       
    $('#colorsbox').toggle();       
        tabClicked = false;        
        } else {                                     
        $('.radiobutton').prop('checked', false);    
        $('#colorstab').animate(              
        {                                
            marginTop:'83px'       
        }, 0);           
    $('#colorsbox').toggle();                    
    tabClicked = true;                            
    }
    });
});

function doButtonStuff() 
{
        console.log('Button is working');
    var radioButton = document.getElementsByName('selection');
    for (var i = 0; i < document.filterform.selection.length; i++) 
    {    
        if (document.filterform.selection[i].checked)              
        {
        var radioValue = document.filterform.selection[i].value;    
            alert(radioValue);
        return radioValue;
        }
    switch (document.getElementById('canvas'))
    {
        case (radioValue == 'cyan'):
            canvas.getContext('2d').drawImage(imgArray[6],0,0);
            break;
    }
    }
}

【问题讨论】:

    标签: html canvas switch-statement case radio


    【解决方案1】:

    switch statements 不是这样工作的。

    Switch 语句将一个值与多个其他值进行比较。以此为例:

    var inputValue = document.getElementById('favorite-color-input').value;
    switch (inputValue) { // compare the input value
      case 'green':       // if 'inputValue' === 'green', execute the following block
        console.log('your favorite color is green');
        break;            // don't execute the next case block
      case 'blue':        // if 'inputValue' === 'blue', execute the following block
        console.log('...');
        break;
      default:           // neither green nor blue
        console.log('I don\'t know that color');
    }
    

    所以你的 switch 语句应该是这样的:

    switch (radioValue) {
      case 'cyan': // draw the cyan image
        ctx.drawImage(imgArray[6],0,0);
        break;
      case 'pink': // draw the pink image
        ctx.drawImage(imgArray[...],0,0);
        break;
    }
    

    注意你return radioValue在执行switch语句之前。我认为这不是你想要的。此外,不要总是获取画布的绘图上下文 (canvas.getContext('2d')),而是使用 ctx

    然而,在 JavaScript 中你很少看到 switch 语句是有原因的:在大多数情况下,你不需要它们。考虑创建一个包含所有图像的对象:

    // map every color to the source of the corresponding image
    var imageSources = {
      'cyan': 'http://i.imgur.com/QbzmJlE.jpg',
      'pink': '...'
    };
    var images = {};
    // map every color to the corresponding image
    for (var color in imageSources) {
      images[color] = new Image();
      images[color].src = imageSources[color];
    }
    

    images 现在看起来像:

    { 
      'cyan': <img src="http://i.imgur.com/QbzmJlE.jpg">,
      'pink': <img src="..."> 
    }
    

    然后,当您绘制图像时:

    if (images.hasOwnProperty(radioValue)) {
      var image = images[radioValue];
      canvas.getContext('2d').drawImage(image, 0, 0);
    } else {
      // the image doesn't exist
    }
    

    【讨论】:

    • 哦,天哪……它现在可以工作了!我非常感谢您的详尽解释。 :) 非常感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多