使用KineticJS交换图像

时间:2014-07-15 14:26:49

标签: kineticjs

Hey Guys我试图使用以下代码交换图像:

     // NEW CODE FOR FLIPPING
    var mainImage2 = new Image();
    mainImage2.src = 'expedition2.gif';

    $("#flip").click(function(){

        var myRadio2 = $('input[name=car_selected]');
        var value2 = myRadio2.filter(':checked').val();



        if(value2 == "expedition"){
        alert("hi");
            expGroup.setImage(mainImage2);
            stage.draw();
        }
    });

    // END OF NEW CODE

基本上我希望按钮能够检查他们想要在这种情况下替换的汽车以及面向一个方向的探险,并且图像面向另一个方向。知道为什么这不起作用吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

我在你的代码中发现了这些故障:

  • 使用图像加载程序确保所有图像都已完全加载。

  • 使用$('input[type=radio][name=group1').change

  • 收听radiobutton更改
  • 更好的编码练习:只重绘图层而不是舞台:layer.draw()

以下是带注释的代码和演示:http://jsfiddle.net/m1erickson/GP6ug/

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.1.min.js"></script>
<style>
body{padding:20px;}
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:350px;
  height:350px;
}
</style>        
<script>
$(function(){

    // create the Kinetic.Stage
    var stage = new Kinetic.Stage({
        container: 'container',
        width: 350,
        height: 350
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);

    var theImage;

    // this loads all necessary images into the imgs[] array
    // and then calls the start() function
    var imagesOK=0;
    var imgs=[];
    // put the paths to your images in this array
    var imageURLs=[];  
    imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/multple/marioRunningLeft.png");
    imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/multple/marioRunningRight.png");
    loadAllImages(start);

    function loadAllImages(callback){
        for (var i=0; i<imageURLs.length; i++) {
            var img = new Image();
            imgs.push(img);
            img.onload = function(){ 
                imagesOK++; 
                if (imagesOK>=imageURLs.length ) {
                    callback();
                }
            };
            img.onerror=function(){alert("image load failed");} 
            img.crossOrigin="anonymous";
            img.src = imageURLs[i];
        }      
    }

    function start(){

        // create the Kinetic.Image
        theImage = new Kinetic.Image({
            x:30,
            y:30,
            image:imgs[0],
            draggable: true
        });
        layer.add(theImage);
        layer.draw();

        // listen from changes to the radio button group
        // change the Kinetic.Image based on which button is selected
        // be sure to redraw the layer so the results are displayed
        $('input[type=radio][name=group1').change(function(){
            var whichButton=parseInt(this.value-1);
            theImage.setImage(imgs[whichButton]);
            layer.draw();
        });

    }

}); // end $(function(){});

</script>       
</head>
<body>
    <input type=radio name=group1 value=1 checked>Image#1
    <input type=radio name=group1 value=2>Image#2
    <div id="container"></div>
</body>
</html>
相关问题