micwallace / WebPrint

Print directly to printers using Javascript.
104 stars 46 forks source link

Problem with overriding images #5

Closed milicev closed 7 years ago

milicev commented 7 years ago

Is there a way to print more than one image without override by last one?

milicev commented 7 years ago

Problem solved!

    function getESCPImageString(url, callback) {                        
        img = new Image();               
        img.onload = function () {         
            // Create an empty canvas element
            var canvas = document.createElement('canvas');
            canvas.width = this.width;
            canvas.height = this.height;
            // Copy the image contents to the canvas
            var ctx = canvas.getContext("2d");
            ctx.drawImage(this, 0, 0);
            // get image slices and append commands
            var bytedata = esc_init + esc_a_c + getESCPImageSlices(ctx, canvas) + font_reset;
            //alert(bytedata);
            callback(bytedata);
        };      
        img.src = url;                                   
    }

Instead img use this and no more overriding. For example: change previously canvas.width = img.width into canvas.width = this.width