How can i clone an image in javascript

后端 未结 2 829
旧巷少年郎
旧巷少年郎 2021-02-13 13:27


I\'m trying to clone an image in javascript, bud without loading a new one.
Normally new browsers will load an image once and there are several ways to use that image

2条回答
  •  忘了有多久
    2021-02-13 13:51

    javascript has provisions for this build in. here is code modified from Danny Goodman's Javascript Bible 2nd Ed p.498,500 for cacheing/preloading an image,

    
    
    
    
    

    you can always use an array if you have a number of images to work with.

    var img = new Array();
    function initallimages(numImages, x, y) {
        var x;
        for (x=1; x <= numImages; x++) {
            img['img' + x] = new Image(x,y);
            img['img' + x].src = '/images/someimage.png';
        }
     }
     function assignallimages(numImages) {
        var x;
        for (x=1; x <= numImages; x++) {
            document.getElementById('img' + x).src = img['img'+x]['img' + x].src;
        }
    }
    
    initallimages(3, 320, 240);
    assignallimages(3);
    

提交回复
热议问题