当画布旋转和调整大小操作正在选定文件上时,加载程序图像不显示

时间:2013-08-28 13:58:20

标签: javascript jquery css html5 canvas

我在这里创建了示例代码http://jsfiddle.net/kamlesh_bhure/YpejU/,其中我正在旋转图像并根据最大高度和宽度调整其大小。

在开始此过程之前,我将显示加载图像的蒙版并在完成后隐藏它。但即使我使用大图像,我也无法在移动设备或桌面浏览器上看到此加载程序。

此外,我想显示加载图像,直到浏览器完成对已处理图像的渲染。

提前致谢。

<!-- HTML Code -->
<div id="mask" class="newLoaderMask">
    <img src="http://jimpunk.net/Loading/wp-content/uploads/loading1.gif" width="200" id="loader" />
</div>
<input type="file" accept="image/*" id="takePictureField" />
<div class="captureInsuPanel">
    <img id="yourimage" class="imgWeightHght" width="500" />
</div>
<canvas id="hidden_canvas_old"></canvas>
<canvas id="hidden_canvas_new"></canvas>
<style>
#yourimage {
    width:100%;
}
.imgWeightHght {
    height: 290px !important;
    width: 220px !important;
}
.captureInsuPanel img {
    height: auto;
    width: auto;
}
.newLoaderMask {
    display: none;
    background: #E5E5E5;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    opacity: 0.8;
    z-index: 999;
}
</style>

<script>
//Js Code 
$(document).ready(function () {
    $("#takePictureField").on("change", gotPic);
});

function gotPic(event) {
    if (event.target.files.length == 1 && event.target.files[0].type.indexOf("image/") == 0) {
        var image = event.target.files[0];
        $("#yourimage").attr("src", URL.createObjectURL(image));
        drawRotatedImage(image, 90, 800, 1000);
    }
}

function drawRotatedImage(image, angle, max_width, max_height) {
    //show loading mask
    $('#mask').show();
    var fileLoader = new FileReader(),
        imageObj = new Image();

    if (image.type.indexOf("image/") == 0) {
        fileLoader.readAsDataURL(image);
    } else {
        alert('File is not an image');
    }
    fileLoader.onload = function () {
        var data = this.result;
        imageObj.src = data;
    };

    fileLoader.onabort = function () {
        alert("The upload was aborted.");
    };

    fileLoader.onerror = function () {
        alert("An error occured while reading the file.");
    };


    // set up the images onload function which clears the hidden canvas context, 
    // draws the new image then gets the blob data from it
    imageObj.onload = function () {
        var imgWidth = this.width;
        var imgHeight = this.height;
        rotateAndResize(this, imgWidth, imgHeight);

    };

    function rotateAndResize(image, imgWidth, imgHeight) {
        var canvas = document.getElementById("hidden_canvas_old"),
            ctx = canvas.getContext("2d");

        var widthHalf = imgWidth / 2,
            heightHalf = imgHeight / 2;

        canvas.width = imgWidth;
        canvas.height = imgWidth;

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.save();
        ctx.translate(canvas.width / 2, canvas.height / 2);
        ctx.rotate(angle * Math.PI / 180);
        ctx.drawImage(image, -widthHalf, -widthHalf);

        ctx.restore();

        var tempCanvas = document.getElementById("hidden_canvas_new"),
            tCtx = tempCanvas.getContext("2d");

        tempCanvas.width = imgHeight;
        tempCanvas.height = imgWidth;
        /*
         * Crop rotated image from old canvas to remove white space 
         * So that canvas will have only image content without extra padding
         */
        tCtx.drawImage(canvas, canvas.width - imgHeight, 0, imgHeight, imgWidth, 0, 0, imgHeight, imgWidth);
        tCtx.restore();

        /**
         * Resizing Rotated image
         */
        // calculate the width and height, constraining the proportions
        if (imgWidth > imgHeight) {
            if (imgWidth > max_width) {
                imgHeight = Math.round(imgHeight *= max_width / imgWidth);
                imgWidth = max_width;
            }
        } else {
            if (imgWidth > max_height) {
                imgWidth = Math.round(imgWidth *= max_height / imgHeight);
                imgHeight = max_height;
            }
        }
        var tempCanvasTemp = tempCanvas;
        tempCanvas.remove();
        canvas.remove();

        var tempCanvas1 = document.createElement("canvas"),
            tCtx1 = tempCanvas1.getContext("2d");

        tempCanvas1.id = 'hidden_canvas_new';
        //tempCanvas1.style.display = 'none';
        tempCanvas1.width = imgHeight;
        tempCanvas1.height = imgWidth;

        tCtx1.drawImage(tempCanvasTemp, 0, 0, imgHeight, imgWidth);
        tCtx1.restore();

        document.body.appendChild(tempCanvas1);

        $("#yourimage").attr("src", tempCanvas1.toDataURL('image/jpeg'));
    }
    //hide loading mask
    $('#mask').hide();
}
</script>

1 个答案:

答案 0 :(得分:2)

您在#mask函数中隐藏了加载div drawRotatedImage,但您应该将其隐藏在rotateAndResize函数中,并将图片绘制到画布上。

<强> CODE:

$(document).ready(function () {
    $("#takePictureField").on("change", gotPic);
});

function gotPic(event) {
    if (event.target.files.length == 1 && event.target.files[0].type.indexOf("image/") == 0) {
        var image = event.target.files[0];
        $("#yourimage").attr("src", URL.createObjectURL(image));
        drawRotatedImage(image, 90, 800, 1000);
    }
}

function drawRotatedImage(image, angle, max_width, max_height) {
    $('#mask').show();
    var fileLoader = new FileReader(),
        imageObj = new Image();

    if (image.type.indexOf("image/") == 0) {
        fileLoader.readAsDataURL(image);
    } else {
        alert('File is not an image');
    }
    fileLoader.onload = function () {
        var data = this.result;
        imageObj.src = data;
    };

    fileLoader.onabort = function () {
        alert("The upload was aborted.");
    };

    fileLoader.onerror = function () {
        alert("An error occured while reading the file.");
    };


    // set up the images onload function which clears the hidden canvas context, 
    // draws the new image then gets the blob data from it
    imageObj.onload = function () {
        var imgWidth = this.width;
        var imgHeight = this.height;
        rotateAndResize(this, imgWidth, imgHeight);

    };

    function rotateAndResize(image, imgWidth, imgHeight) {
        var canvas = document.getElementById("hidden_canvas_old"),
            ctx = canvas.getContext("2d");

        var widthHalf = imgWidth / 2,
            heightHalf = imgHeight / 2;

        canvas.width = imgWidth;
        canvas.height = imgWidth;

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.save();
        ctx.translate(canvas.width / 2, canvas.height / 2);
        ctx.rotate(angle * Math.PI / 180);
        ctx.drawImage(image, -widthHalf, -widthHalf);

        ctx.restore();

        var tempCanvas = document.getElementById("hidden_canvas_new"),
            tCtx = tempCanvas.getContext("2d");

        tempCanvas.width = imgHeight;
        tempCanvas.height = imgWidth;
        /*
         * Crop rotated image from old canvas to remove white space 
         * So that canvas will have only image content without extra padding
         */
        tCtx.drawImage(canvas, canvas.width - imgHeight, 0, imgHeight, imgWidth, 0, 0, imgHeight, imgWidth);
        tCtx.restore();

        /**
         * Resizing Rotated image
         */
        // calculate the width and height, constraining the proportions
        if (imgWidth > imgHeight) {
            if (imgWidth > max_width) {
                imgHeight = Math.round(imgHeight *= max_width / imgWidth);
                imgWidth = max_width;
            }
        } else {
            if (imgWidth > max_height) {
                imgWidth = Math.round(imgWidth *= max_height / imgHeight);
                imgHeight = max_height;
            }
        }
        var tempCanvasTemp = tempCanvas;
        tempCanvas.remove();
        canvas.remove();

        var tempCanvas1 = document.createElement("canvas"),
            tCtx1 = tempCanvas1.getContext("2d");

        tempCanvas1.id = 'hidden_canvas_new';
        //tempCanvas1.style.display = 'none';
        tempCanvas1.width = imgHeight;
        tempCanvas1.height = imgWidth;

        tCtx1.drawImage(tempCanvasTemp, 0, 0, imgHeight, imgWidth);
        tCtx1.restore();

        document.body.appendChild(tempCanvas1);

        $("#yourimage").attr("src", tempCanvas1.toDataURL('image/jpeg'));
         $('#mask').hide();
    }
}

DEMO FIDDLE

注意:在测试小提琴时发现了另外一个问题,如果我们尝试上传图像第二次第二个画布而不是用新图像更新。希望你需要解决这个问题。