Convert an image to canvas that is already loaded

前端 未结 2 373
难免孤独
难免孤独 2020-12-16 07:34

I am working on a plugin in which I\'m converting Image into Canvas and storing as data url .This function triggers on \'load\' event but how can I convert an image which is

2条回答
  •  再見小時候
    2020-12-16 08:30

    Now it works perfectly.If you create a temporary image element using javascript then store it in localStorage. This worked for me hope it'll help others too

        image = document.createElement('img');
        document.body.appendChild(image);
        image.setAttribute('style','display:none');
        image.setAttribute('alt','script div');
        image.setAttribute("src", path);
    
        var imgCanvas = document.createElement("canvas"),
        imgContext = imgCanvas.getContext("2d");
    
        // Make sure canvas is as big as the picture
        imgCanvas.width = image.width;
        imgCanvas.height = image.height;
    
        // Draw image into canvas element
        imgContext.drawImage(image, 0, 0, image.width, image.height);
        // Save image as a data URL
        imgInfom = imgCanvas.toDataURL("image/png");
        localStorage.setItem("imgInfo",imgInfom);
        document.body.removeChild(image);
    

提交回复
热议问题