【问题标题】:Kinetic JS - Loading multiple images on canvasKinetic JS - 在画布上加载多个图像
【发布时间】:2015-05-27 23:54:25
【问题描述】:

我想在画布上加载和显示多个图像。图像源保存在数组中:

var sources = {"../src/img/IMG_1284.JPG", "../src/img/IMG_1285.JPG", "../src/img/IMG_1286.JPG", "../src/img/IMG_1287.JPG", "../src/img/IMG_1288.JPG"}

我找到了这个教程:http://www.html5canvastutorials.com/tutorials/html5-canvas-image-loader

但我不知道如何使这段代码适应我的源数组。 此外,我想将每个图像都设置为可拖动,但我认为这对于 context.drawImage(..) 是不可能的。

希望我的问题可以理解。感谢您的帮助...

【问题讨论】:

    标签: javascript jquery html kineticjs


    【解决方案1】:

    我看到你用 KineticJS 标记了你的问题。

    KineticJS 通过跟踪您的图像并允许您的用户拖动它们,让您的任务变得轻松。

    简单如下:

    • 使用图像加载器确保所有图像在尝试绘制之前都已完全加载
    • 加载后,使用每个图像创建一个 Kinetic.Image。
    • 将可拖动属性放在 Kinetic.Image 上,它将自动可拖动

    这是代码和演示:http://jsfiddle.net/m1erickson/3es4Z/

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Prototype</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
        <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.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 and layer
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 350,
            height: 350
        });
        var layer = new Kinetic.Layer();
        stage.add(layer);
    
        // put the paths to your images in imageURLs
        var imageURLs=[];
        imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-1.jpg");
        imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-2.jpg");
        imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-3.jpg");
        var imagesOK=0;
        var imgs=[];
    
        // fully load every image, then call the start function
        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(){
            // the imgs[] array holds fully loaded images
            // the imgs[] are in the same order as imageURLs[]
    
            // make each image into a draggable Kinetic.Image
            for(var i=0;i<imgs.length;i++){
                var img=new Kinetic.Image({
                    x:i*75+15,
                    y:i*75+15,
                    width:60,
                    height:60,
                    image:imgs[i],
                    draggable:true
                });
                layer.add(img);
            }
            layer.draw();
        }
    
    }); // end $(function(){});
    </script>       
    </head>
    <body>
        <button id="myButton">Button</button>
        <div id="container"></div>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 2020-08-03
      相关资源
      最近更新 更多