溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript canvas怎么實現(xiàn)鏡像圖片效果

發(fā)布時間:2021-08-16 15:42:04 來源:億速云 閱讀:204 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“JavaScript canvas怎么實現(xiàn)鏡像圖片效果”,在日常操作中,相信很多人在JavaScript canvas怎么實現(xiàn)鏡像圖片效果問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JavaScript canvas怎么實現(xiàn)鏡像圖片效果”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

本文實例為大家分享了JavaScript canvas實現(xiàn)鏡像圖片效果的具體代碼,供大家參考,具體內(nèi)容如下

JavaScript canvas怎么實現(xiàn)鏡像圖片效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Using image</title>
        <style type="text/css">
            * {
                /* margin: 0;
                padding: 0; */
                box-sizing: border-box;
            }
            canvas {
                /* border-width: 1px;
                border-color: #000000;
                border-style: solid; */
            }
        </style>
    </head>
    <body>
        <canvas id="canvas"></canvas>
        <canvas id="mirror"></canvas>
        
        <div>
            <input type="file" accept="image/*" />
        </div>
        
        <script type="text/javascript">
            window.onload = (event) => {
                main()
            }
            
            function main() {
                const canvas = document.getElementById("canvas");
                const mirror = document.getElementById("mirror");
                const ctx = canvas.getContext("2d");
                const mirrorCtx = mirror.getContext("2d");
                
                const inputFile = document.querySelector("input[type=file]");
                inputFile.onchange = (event) => {
                    const files = event.target.files;
                    if (files.length > 0) {
                        const file = files[0]; // First file
                        console.log(file);
                        
                        const image = new Image();
                        image.src = URL.createObjectURL(file);
                        image.onload = function(event) {
                            // console.log(event, this);
                            URL.revokeObjectURL(this.src);
                            
                            canvas.width = image.width;
                            canvas.height = image.height;
                            mirror.width = image.width;
                            mirror.height = image.height;
                            
                            ctx.drawImage(image, 0, 0);
                            
                            const emptyImageData = ctx.createImageData(canvas.width, canvas.height);
                            const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                            
                            // console.log(getPixel(imageData, 0, 0));
                            // console.log(getPixel(imageData, 0, 5));
                            // console.log(getPixel(imageData, 0, 9));
                            // console.log(getColor(imageData, 0, 0, "R"));
                            // console.log(getColor(imageData, 0, 5, "G"));
                            // console.log(getColor(imageData, 0, 9, "B"));
                            
                            // console.log(imageData);
                            
                            // const uint8ClampedArray = imageData.data;
                            // uint8ClampedArray.length = imageData.width * imageData.height * 4;
                            console.log(imageData.data[0]);
                            for(let h = 0; h < imageData.height; h++) {
                                for(let w = 0; w < imageData.width; w++) {
                                    const pixel = getPixel(imageData, h, imageData.width - w - 1);
                                    setPixel(emptyImageData, h, w, pixel);
                                }
                            }
                            mirrorCtx.putImageData(emptyImageData, 0, 0);
                            
                            console.log(imageData, emptyImageData);
                            
                            function getPixel(imageData, row, column) {
                                const uint8ClampedArray = imageData.data;
                                const width = imageData.width;
                                const height = imageData.height;
                                const pixel = [];
                                for(let i = 0; i < 4; i++) {
                                    pixel.push(uint8ClampedArray[row * width * 4 + column * 4 + i]);
                                }
                                return pixel;
                            }
                            
                            function setPixel(imageData, row, column, pixel) {
                                const uint8ClampedArray = imageData.data;
                                const width = imageData.width;
                                const height = imageData.height;
                                for(let i = 0; i < 4; i++) {
                                    uint8ClampedArray[row * width * 4 + column * 4 + i] = pixel[i];
                                }
                            }
                            
                            // function getColor(imageData, row, column, color) {
                            //     const pixel = getPixel(imageData, row, column);
                            //     switch(color) {
                            //         case "R":
                            //             return pixel[0];
                            //         case "G":
                            //             return pixel[1];
                            //         case "B":
                            //             return pixel[2];
                            //         case "A":
                            //             return pixel[3];
                            //     }
                            //     return null;
                            // }
                        }
                    }
                }
            }
        </script>
    </body>
</html>

到此,關(guān)于“JavaScript canvas怎么實現(xiàn)鏡像圖片效果”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI