我用 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":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章