我用 10000 張圖片合成我們美好的瞬間

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"月亮照回湖心 野鶴奔向閒雲","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"昨天是情人節,大家相比都非常愉快的度過了節日~我也是😚","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"好了,廢話不多說,今天給大家帶來是一個非常有意思的項目,通過切割目標圖片,獲得10000個方塊,用我們所選擇到的圖片,對應的填充方塊實現一個千圖成像的效果.你可以用它來拼任何你想拼的有意義的大圖.(比如我,就想用它把我和對象戀愛到結婚拍的所有照片用來做一個超級超級超級超級大的婚紗照,在老家","attrs":{}},{"type":"link","attrs":{"href":"https://baike.baidu.com/item/%E9%84%B1%E9%98%B3%E6%B9%96%E6%99%AF%E5%8C%BA/6125357?fr=aladdin&fromtitle=%E9%84%B1%E9%98%B3%E6%B9%96&fromid=154797","title":"","type":null},"content":[{"type":"text","text":"鄱陽湖","attrs":{}}]},{"type":"text","text":"的草地上鋪着,用無人機高空俯瞰,嘖,挺有意思~在這裏先埋個點,希望幾年後能夠實現😊)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,這篇文章是基於我的上一篇fabric入門篇所出的一篇實用案例,也是我自己用來練手總結所用,在此分享給大家,一起成長!","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"進入正題","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"首先我們初始一個 800*800 的畫布","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"(界面的樣式,在這裏我就不過多表述了,我們主要講邏輯功能的實現)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//初始化畫布\ninitCanvas() {\n this.canvas = new fabric.Canvas(\"canvas\", {\n selectable: false,\n selection: false,\n hoverCursor: \"pointer\",\n });\n this.ctx = canvas.getContext(\"2d\");\n this.addCanvasEvent();//給畫布添加事件\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據自己電腦的配置來自定義畫布的大小, 目前還沒找到直接在 web 端做類似千圖成像的,在 web 端實現這個功能確實是很消耗性能的,因爲需要處理的數據量好大,計算量也大","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"需要注意的是: 800*800 的畫布有 640000 個像素,通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ctx.getImageData","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"獲取到的每個像素是 4 個值,就是 2560000 個值,我們後面需要處理這 2560000 個值,所以這裏我就不做大了","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f5/f5ed4a0c75585b3b088638df7e870bd8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"用 fabric 繪製目標圖片","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"需要注意的是,我們通過本地圖片繪製到畫布,需要將拿到的 file 文件通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"window.URL.createObjectURL(file)","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"將文件轉爲 blob 類型的 url","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"像你喜歡用 elementUI 的 upload 組件,你就這麼寫","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//目標圖片選擇回調\nslectFile(file, fileList) {\n let tempUrl = window.URL.createObjectURL(file.raw);\n this.drawImage(tempUrl);\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我不喜歡它的組件,因爲後面選擇資源圖片的時候,選擇數千張圖片會有文件列表,我又不想隱藏它(主要還是想分享一下自定義的文件選擇)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以我是這麼寫的","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"export function inputFile() {\n return new Promise(function (resolve, reject) {\n if (document.getElementById(\"myInput\")) {\n let inputFile = document.getElementById(\"myInput\");\n inputFile.onchange = (e) => {\n let urlArr = [];\n for (let i = 0; i < e.target.files.length; i++) {\n urlArr.push(URL.createObjectURL(e.target.files[i]));\n }\n resolve(urlArr);\n };\n inputFile.click();\n } else {\n let inputFile = document.createElement(\"input\");\n inputFile.setAttribute(\"id\", \"myInput\");\n inputFile.setAttribute(\"type\", \"file\");\n inputFile.setAttribute(\"accept\", \"image/*\");\n inputFile.setAttribute(\"name\", \"file\");\n inputFile.setAttribute(\"multiple\", \"multiple\");\n inputFile.setAttribute(\"style\", \"display: none\");\n inputFile.onchange = (e) => {\n // console.log(e.target.files[0]);\n // console.log(e.target.files);\n // let tempUrl = URL.createObjectURL(e.target.files[0]);\n // console.log(tempUrl);\n let urlArr = [];\n for (let i = 0; i < e.target.files.length; i++) {\n urlArr.push(URL.createObjectURL(e.target.files[i]));\n }\n resolve(urlArr);\n };\n document.body.appendChild(inputFile);\n inputFile.click();\n }\n });\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過以上方法拿到文件後,我在裏面已經將圖片文件轉爲了 blob 的 URL 供我們使用 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"(需要注意的是文件的選擇是異步的,所以這裏需要用 promise 來寫)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//繪製目標圖片\ndrawImage(url) {\n fabric.Image.fromURL(url, (img) => {\n //設置縮放比例,長圖的縮放比爲this.canvas.width / img.width,寬圖的縮放比爲this.canvas.height / img.height\n let scale =\n img.height > img.width\n ? this.canvas.width / img.width\n : this.canvas.height / img.height;\n img.set({\n left: this.canvas.height / 2, //距離左邊的距離\n originX: \"center\", //圖片在原點的對齊方式\n top: 0,\n scaleX: scale, //橫向縮放\n scaleY: scale, //縱向縮放\n selectable: false, //可交互\n });\n //圖片添加到畫布的回調函數\n img.on(\"added\", (e) => {\n //這裏有個問題,added後獲取的是之前的畫布像素數據,其他手動觸發的事件,不會有這種問題\n //故用一個異步解決\n setTimeout(() => {\n this.getCanvasData();\n }, 500);\n });\n this.canvas.add(img); //將圖片添加到畫布\n this.drawLine(); //繪製網格線條\n });\n},\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"繪製完圖片後順便在畫布上繪製 100*100 的柵格","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//柵格線\ndrawLine() {\n const blockPixel = 8;\n for (let i = 0; i <= this.canvas.width / blockPixel; i++) {\n this.canvas.add(\n new fabric.Line([i * blockPixel, 0, i * blockPixel, this.canvas.height], {\n left: i * blockPixel,\n stroke: \"gray\",\n selectable: false, //是否可被選中\n })\n );\n this.canvas.add(\n new fabric.Line([0, i * blockPixel, this.canvas.height, i * blockPixel], {\n top: i * blockPixel,\n stroke: \"gray\",\n selectable: false, //是否可被選中\n })\n );\n }\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"繪製完畢後可以看到圖片加網格線的效果,還是挺好看的~😘","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/21/2182a708cb0cd9383232d460dd34d154.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"將圖片顏色分塊保存在數組中","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"一開始這麼寫把瀏覽器跑崩了","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/98/98c7f1fa5bd0d7c982dc9ce011f9df36.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c11d85ecf5d6b6141de261258a829188.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"我哭 😥,這麼寫循環嵌套太多(而且基數是 800*800*4==2560000-->得好好寫,要不然對不起 pixelList 被我瘋狂操作了 2560000 次)得優化一下寫法,既然瀏覽器炸了,笨方法行不通,那隻能換了~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先說明,這裏我們每個小塊的長寬給的是 8 個像素 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"(越小後面合成圖片的精度越精細,越大越模糊)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//獲取畫布像素數據\ngetCanvasData() {\n for (let Y = 0; Y < this.canvas.height / 8; Y++) {\n for (let X = 0; X < this.canvas.width / 8; X++) {\n //每8*8像素的一塊區域一組\n let tempColorData = this.ctx.getImageData(X * 8, Y * 8, 8, 8).data;\n //將獲取到數據每4個一組,每組都是一個像素\n this.blockList[Y * 100 + X] = { position: [X, Y], color: [] };\n for (let i = 0; i < tempColorData.length; i += 4) {\n this.blockList[Y * 100 + X].color.push([\n tempColorData[i],\n tempColorData[i + 1],\n tempColorData[i + 2],\n tempColorData[i + 3],\n ]);\n }\n }\n }\n console.log(mostBlockColor(this.blockList));\n this.mostBlockColor(this.blockList);//獲取每個小塊的主色調\n this.loading = false;\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/39/399a6a8d415515c469b5ee7d8ce019e3.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"😅 換了一種寫法後,這裏我們將每個 8*8 的像素塊劃爲一組,得到 10000 個元素,每個元素裏都有 4 個值,分別代表着 RGBA 的值,後面我們會用對應的 10000 張圖片填充對應的像素塊","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8f/8f39c4d95edf502226d52a6646d79e7b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"拿到畫布上的所有像素值後,我們需要求出每個小方塊的主色調","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後面我們需要通過這些小方塊的主色調通過求它與資源圖片的色差,來決定該方塊具體是填充哪一張圖片 😊","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到這裏很興奮,感覺是快完成了一半了,其實不然,後面更抓頭皮 😭","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" //獲取每個格子的主色調\nmostBlockColor(blockList) {\n for (let i = 0; i < blockList.length; i++) {\n let colorList = [];\n let rgbaStr = \"\";\n for (let k = 0; k < blockList[k].color.length; k++) {\n rgbaStr = blockList[i].color[k];\n if (rgbaStr in colorList) {\n ++colorList[rgbaStr];\n } else {\n colorList[rgbaStr] = 1;\n }\n }\n let arr = [];\n for (let prop in colorList) {\n arr.push({\n // 如果只獲取rgb,則爲`rgb(${prop})`\n color: prop.split(\",\"),\n // color: `rgba(${prop})`,\n count: colorList[prop],\n });\n }\n // 數組排序\n arr.sort((a, b) => {\n return b.count - a.count;\n });\n arr[0].position = blockList[i].position;\n this.blockMainColors.push(arr[0]);\n }\n console.log(this.blockMainColors);\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"腦瓜子不好使,草稿紙都用上了","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"獲取每張資源圖的主色調","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"export function getMostColor(imgUrl) {\n return new Promise((resolve, reject) => {\n try {\n const canvas = document.createElement(\"canvas\");\n //設置canvas的寬高都爲20,越小越快,但是越小越不精確\n canvas.width = 20;\n canvas.height = 20;\n const img = new Image(); // 創建img元素\n img.src = imgUrl; // 設置圖片源地址\n img.onload = () => {\n const ctx = canvas.getContext(\"2d\");\n const scaleH = canvas.height / img.height;\n img.height = canvas.height;\n img.width = img.width * scaleH;\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height);\n console.log(img.width, img.height);\n // 獲取像素數據\n let pixelData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;\n let colorList = [];\n let color = [];\n let colorKey = \"\";\n let colorArr = [];\n // 分組循環\n for (let i = 0; i < pixelData.length; i += 4) {\n color[0] = pixelData[i];\n color[1] = pixelData[i + 1];\n color[2] = pixelData[i + 2];\n color[3] = pixelData[i + 3];\n colorKey = color.join(\",\");\n if (colorKey in colorList) {\n ++colorList[colorKey];\n } else {\n colorList[colorKey] = 1;\n }\n }\n for (let prop in colorList) {\n colorArr.push({\n color: prop.split(\",\"),\n count: colorList[prop],\n });\n }\n // 對所有顏色數組排序,取第一個爲主色調\n colorArr.sort((a, b) => {\n return b.count - a.count;\n });\n colorArr[0].url = imgUrl;\n console.log(\n `%c rgba(${colorArr[0].color.join(\",\")})`,\n `background: rgba(${colorArr[0].color.join(\",\")})`\n );\n resolve(colorArr[0]);\n };\n } catch (e) {\n reject(e);\n }\n });\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"我們隨機選擇一些文件後,將他們的主色調打印出來看看效果","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ff/ff4d245f5ffa225fe92c22efe065fdea.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"顏色空間","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要求顏色的色差,我們首先需要一起來了解一下顏色的定義,顏色有很多種表示方式,它們的標準都不相同,有 CMYK,RGB,HSB,LAB 等等...","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"這裏我們是 RGBA 的,它就是 RGB 的顏色模型附加了額外的 Alpha 信息","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RGBA 是代表 Red(紅色)Green(綠色)Blue(藍色)和 Alpha 的色彩空間。雖然它有的時候被描述爲一個顏色空間,但是它其實僅僅是 RGB 模型的附加了額外的信息。採用的顏色是 RGB,可以屬於任何一種 RGB 顏色空間,但是 Catmull 和 Smith 在 1971 至 1972 年間提出了這個不可或缺的 alpha 數值,使得 alpha 渲染和 alpha 合成變得可能。提出者以 alpha 來命名是源於經典的線性插值方程 αA + (1-α)B 所用的就是這個希臘字母。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7a/7afde29f239337ab5455ce75f833e0de.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e2/e2c8b93a0db6b3c65c0a0f681d45e96d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其他顏色的相關介紹可以看","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://zhuanlan.zhihu.com/p/24281841","title":"","type":null},"content":[{"type":"text","text":"這裏:","attrs":{}}]},{"type":"text","text":"https://zhuanlan.zhihu.com/p/24281841","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或","attrs":{}},{"type":"link","attrs":{"href":"https://baike.baidu.com/item/%E9%A2%9C%E8%89%B2%E7%A9%BA%E9%97%B4/10834848?fr=aladdin","title":"","type":null},"content":[{"type":"text","text":"這裏","attrs":{}}]},{"type":"text","text":"https://baike.baidu.com/item/%E9%A2%9C%E8%89%B2%E7%A9%BA%E9%97%B4/10834848?fr=aladdin","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"求顏色差異的方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"由於顏色在空間中的分佈如上面的介紹所示,這裏我們採用中學學過的歐氏距離法,來求兩個顏色的絕對距離,通過它們的遠近就知道兩個顏色的相似程度的大小","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先我們瞭解一下歐氏距離的基本概念","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"歐幾里得度量(euclidean metric)(也稱歐氏距離)是一個通常採用的距離定義,指在 m 維空間中兩個點之間的真實距離,或者向量的自然長度(即該 點到原點的距離)。在二維和三維空間中的歐氏距離就是兩點之間的實際距離。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ee/ee026618ca44861869458171a9ec0d47.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"將公式轉化爲代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//計算顏色差異\ncolorDiff(color1, color2) {\n let distance = 0;//初始化距離\n for (let i = 0; i < color1.length; i++) {\n distance += (color1[i] - color2[i]) ** 2;//對兩組顏色r,g,b[a]的差的平方求和\n }\n return Math.sqrt(distance);//開平方後得到兩個顏色在色彩空間的絕對距離\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算顏色差異的方法有多種,可以看","attrs":{}},{"type":"link","attrs":{"href":"https://www.wikiwand.com/en/Color_difference#/sRGB","title":"","type":null},"content":[{"type":"text","text":"wikiwand:","attrs":{}}]},{"type":"text","text":"https://www.wikiwand.com/en/Color_difference#/sRGB","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或者你也可以使用類似 ColorRNA.js 等顏色處理庫進行對比,這裏我們不做過多描述","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"計算差值後渲染圖片","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在這裏我們需要將每個像素塊的主色調與所有資源圖片的主色調作比較,取差異最小的那張渲染到對應的方塊上","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7f/7f2e94b359a7ed14351d22686314f1df.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//生成圖片\ngenerateImg() {\n this.loading = true;\n let diffColorList = [];\n //遍歷所有方塊\n for (let i = 0; i < this.blockMainColors.length; i++) {\n diffColorList[i] = { diffs: [] };\n //遍歷所有圖片\n for (let j = 0; j < this.imgList.length; j++) {\n diffColorList[i].diffs.push({\n url: this.imgList[j].url,\n diff: this.colorDiff(this.blockMainColors[i].color, this.imgList[j].color),\n color: this.imgList[j].color,\n });\n }\n //對比較過的圖片進行排序,差異最小的放最前面\n diffColorList[i].diffs.sort((a, b) => {\n return a.diff - b.diff;\n });\n //取第0個圖片信息\n diffColorList[i].url = diffColorList[i].diffs[0].url;\n diffColorList[i].position = this.blockMainColors[i].position;\n diffColorList[i].Acolor = this.blockMainColors[i].color;\n diffColorList[i].Bcolor = diffColorList[i].diffs[0].color;\n }\n this.loading = false;\n console.log(diffColorList);\n //便利每一個方塊,對其渲染\n diffColorList.forEach((item) => {\n fabric.Image.fromURL(item.url, (img) => {\n let scale = img.height > img.width ? 8 / img.width : 8 / img.height;\n // img.scale(8 / img.height);\n img.set({\n left: item.position[0] * 8,\n top: item.position[1] * 8,\n originX: \"center\",\n scaleX: scale,\n scaleY: scale,\n });\n this.canvas.add(img);\n });\n });\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"好傢伙!!! 這是什麼玩意???這搞了一晚上,出個這?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"我哭了,現在都五點多了,我還沒睡呢~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a9/a957d2a848d3e13b7ca862cf777208e8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"不拋棄不放棄,堅持到底就是勝利","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"仔細分析了下每一個步驟,逐步查找問題所在從最開始的目標圖片像素數據開始看像素數據的正確性,但是沒找到問題所在,數據都沒啥問題,初步判斷是計算像素塊的主色調上出了問題,於是想到,會不會主色調並不是取一張圖片或者一塊像素塊中出現最多次數的顏色爲主色調,而是取它們的所有顏色的平均值作爲主色調呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想到這裏,我很興奮!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"差點吵醒已經熟睡的瓜娃子,我開始重新梳理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"這裏,我對每個 8*8 的小方塊都改成了通過平均值求主色調","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//獲取每個格子的主色調\nmostBlockColor(blockList) {\n for (let i = 0; i < blockList.length; i++) {\n let r = 0,\n g = 0,\n b = 0,\n a = 0;\n for (let j = 0; j < blockList[i].color[j].length; j++) {\n r += blockList[i].color[j][0];\n g += blockList[i].color[j][1];\n b += blockList[i].color[j][2];\n a += blockList[i].color[j][3];\n }\n // 求取平均值\n r /= blockList[i].color[0].length;\n g /= blockList[i].color[0].length;\n b /= blockList[i].color[0].length;\n a /= blockList[i].color[0].length;\n // 將最終的值取整\n r = Math.round(r);\n g = Math.round(g);\n b = Math.round(b);\n a = Math.round(a);\n this.blockMainColors.push({\n position: blockList[i].position,\n color: [r, g, b, a],\n });\n }\n console.log(this.blockMainColors);\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"然後,對每張圖片也改成了通過平均值求主色調","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"export function getAverageColor(imgUrl) {\n return new Promise((resolve, reject) => {\n try {\n const canvas = document.createElement(\"canvas\");\n //設置canvas的寬高都爲20,越小越快,但是越小越不精確\n canvas.width = 20;\n canvas.height = 20;\n const img = new Image(); // 創建img元素\n img.src = imgUrl; // 設置圖片源地址\n img.onload = () => {\n console.log(img.width, img.height);\n let ctx = canvas.getContext(\"2d\");\n const scaleH = canvas.height / img.height;\n img.height = canvas.height;\n img.width = img.width * scaleH;\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height);\n // 獲取像素數據\n let data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;\n let r = 0,\n g = 0,\n b = 0,\n a = 0;\n // 取所有像素的平均值\n for (let row = 0; row < canvas.height; row++) {\n for (let col = 0; col < canvas.width; col++) {\n r += data[(canvas.width * row + col) * 4];\n g += data[(canvas.width * row + col) * 4 + 1];\n b += data[(canvas.width * row + col) * 4 + 2];\n a += data[(canvas.width * row + col) * 4 + 3];\n }\n }\n // 求取平均值\n r /= canvas.width * canvas.height;\n g /= canvas.width * canvas.height;\n b /= canvas.width * canvas.height;\n a /= canvas.width * canvas.height;\n\n // 將最終的值取整\n r = Math.round(r);\n g = Math.round(g);\n b = Math.round(b);\n a = Math.round(a);\n console.log(\n `%c ${\"rgba(\" + r + \",\" + g + \",\" + b + \",\" + a + \")\"}\n `,\n `background: ${\"rgba(\" + r + \",\" + g + \",\" + b + \",\" + a + \")\"};`\n );\n resolve({ color: [r, g, b, a], url: imgUrl });\n };\n } catch (e) {\n reject(e);\n }\n });\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"激動人心的時候到了!!!!!!!!!!!!!啊啊啊啊啊!!我很激動,勝利就在眼前,臨門一 jor 了!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一頓操作,選擇目標圖片,選擇資源圖片,點擊生成圖片按鈕後,我開始了等待勝利的召喚!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/34/348c509cd8f1f562a9af529943b71c9f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"我去,更醜了,這咋回事","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緊接着我直接熱血了起來,遇到這種有挑戰的事情我就很有勁頭,我要搞不過它,那不符合我的氣質,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"於是我開始分析處理過的小塊主色調,我發現它們好像都有規律","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fc/fc614735de530c850cfc986d9d08610c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我想是什麼影響到了呢,圖片繪製上去不可能會一樣的顏色啊,一樣的顏色是什麼呢???","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"wo kao~不會是我畫的 100*100 的線條吧","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"於是我回到,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"drawLine","attrs":{}}],"attrs":{}},{"type":"text","text":"函數,我把它給註釋掉了~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c1b91bb8e725979ae5e47c4fafdc992b.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"nice!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/58/58c563baf1fbb7f481905015c027216b.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"每一個方塊都可以交互的拉伸旋轉,移動,到這裏畫布的基本功能就已經完結啦~撒花~🌹🏵🌸💐🌺🌻🌼🌷","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們還可以把生成好的圖片導出來,機器好的小夥伴們可以定義一個很大的畫布,或者給圖片做上編號,打印出來,是可以用來做巨大的合成圖的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"(比如我前面提到的婚紗照等等,還是很有意思的)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" //導出圖片\nexportCanvas() {\n const dataURL = this.canvas.toDataURL({\n width: this.canvas.width,\n height: this.canvas.height,\n left: 0,\n top: 0,\n format: \"png\",\n });\n const link = document.createElement(\"a\");\n link.download = \"canvas.png\";\n link.href = dataURL;\n document.body.appendChild(link);\n link.click();\n document.body.removeChild(link);\n},\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個情人節過的,屬實是有點充實,現在是早上六點半~我又肝了一波,睡覺睡覺,保命要緊,白天還要出去玩 😅😅","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"最後","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"昇華一下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"浪漫的七夕,連空氣中都飄蕩着一股愛情的味道。對對有情人歡喜相邀,黃昏後,柳梢頭,竊竊私語,良辰美景,月圓花好!祝福天下有情人,幸福快樂!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個項目我放在我的","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/wangrongding","title":"","type":null},"content":[{"type":"text","text":"github","attrs":{}}]},{"type":"text","text":"上( https://github.com/wangrongding),喜歡的小夥伴,記得要點個贊哦~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我是榮頂,很高興能在這裏和你一起變強!一起","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"面向快樂編程!","attrs":{}},{"type":"text","text":" 😉","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你也非常熱愛前端相關技術!","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"掃描二維碼~","attrs":{}},{"type":"text","text":" 進入我的小密圈 🦄","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d1/d1861c77db4169468d0a1925f435e094.gif","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}}],"attrs":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章