首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在画布元素中重置选定和设置的图像

在画布元素中重置选定和设置的图像
EN

Stack Overflow用户
提问于 2015-06-09 00:41:29
回答 1查看 92关注 0票数 0

如何更改加载到画布元素中的图像,如果我在操作后上传相同的文件名?换句话说,我需要能够清除画布,以便可以加载新的图像,或者找到一种方法,在再次选择相同的文件时重新设置它。

我有以下加载图像文件的文件,但不确定重置已选择的照片的逻辑,以便重新加载原始照片:

代码语言:javascript
复制
var fileInput = document.getElementById("file"),
    renderButton = $("#renderButton"),
    submit = $(".submit"),
    imgly = new ImglyKit({
        container: "#container",
        ratio: 1 / 1
    });
// As soon as the user selects a file...
fileInput.addEventListener("change", function (event) {

    //CHECK FILE SIZE OF INPUT
    if (window.File && window.FileReader && window.FileList && window.Blob) {
        //get the file size and file type from file input field
        var fsize = $('#file')[0].files[0].size;
        var ftype = $('#file')[0].files[0].type;
        var fname = $('#file')[0].files[0].name;
        var isValidSize = true;
        var filesize = (fsize / (1024 * 1024 * 10)).toFixed(2);

        if (fsize > 80000000) //do something if file size more than 1 mb (1048576)
        {
            $(".file-warning").html("<div class='alert alert-danger'><p>The image: <b>" + fname + "</b> is <b>" + filesize + " MB</b> and too big. Please reduce your image size to <b>1MB</b> or less.</p></div>");
            $("#file").css("border-color", "red");
            //alert("Type :"+ ftype +" | "+ fsize +" bites\n(File: "+fname+") Too big!");
            isValidSize = false; // this variable should be created somewhere at the top. It will keep the state of file-picker.
            return; //this will stop any further actions;
        }
        //IF FILE SIZE WAS TOO BIG AND WARNING THROWN, BUT NEW CORRECT FILE SIZE LOADED, CLEAR WARNING
        else {
            if (fsize < 80000000) {
                $("#file").css("border-color", "#ccc");
                $(".file-warning").empty();
            }
        }
    } else {
        alert("Please upgrade your browser, because your current browser lacks some new features we need!");
    }
    //END FILE SIZE CHECK

    var file;

    var fileToBlob = event.target.files[0];
    var blob = new Blob([fileToBlob], {
        "type": fileToBlob.type
    });
    // do stuff with blob
    console.log(blob);
    // Find the selected file
    if (event.target.files) {
        file = event.target.files[0];
    } else {
        file = event.target.value;
    }

    // Use FileReader to turn the selected
    // file into a data url. ImglyKit needs
    // a data url or an image
    var reader = new FileReader();
    reader.onload = (function (file) {
        return function (e) {
            data = e.target.result;

            // Run ImglyKit with the selected file
            try {
                imgly.run(data);
            } catch (e) {
                if (e.name == "NoSupportError") {
                    alert("Your browser does not support canvas.");
                } else if (e.name == "InvalidError") {
                    alert("The given file is not an image");
                }
            }
        };
    })(file);
    reader.readAsDataURL(file);
EN

回答 1

Stack Overflow用户

发布于 2015-06-09 00:50:27

Canvas-API有一个这样的方法:

代码语言:javascript
复制
context.clearRect(0, 0, canvas.width, canvas.height);

这很好而且很简单--但是只有当你的画布没有被转换的时候,当然如果你正在为你的图片使用整个画布的时候。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30714713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档