91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

JavaScript canvas怎么實現鏡像圖片效果

發布時間:2021-08-16 15:42:04 來源:億速云 閱讀:206 作者:chen 欄目:開發技術

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

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

JavaScript canvas怎么實現鏡像圖片效果

<!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>

到此,關于“JavaScript canvas怎么實現鏡像圖片效果”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

湖口县| 桑日县| 宁武县| 仲巴县| 义乌市| 东丰县| 蚌埠市| 交口县| 英吉沙县| 绿春县| 连平县| 荣成市| 香港| 德格县| 灵璧县| 雷波县| 铁力市| 普定县| 安平县| 广丰县| 于田县| 武乡县| 茌平县| 罗平县| 林周县| 酒泉市| 闻喜县| 鄂托克旗| 淳安县| 启东市| 满城县| 浦东新区| 蚌埠市| 饶阳县| 榆中县| 滦南县| 静海县| 拉萨市| 儋州市| 盐城市| 错那县|