Home > Article > Web Front-end > Use canvas to achieve a picture triangulation (LOW POLY) effect_javascript skills
I accidentally saw Ovilia using threejs to make a LOW POLY, which is the effect of plane triangulation of pictures. I thought it was amazing, so I spent some time trying it myself.
I haven’t used threejs much, so I just used the 2D drawing API of canvas to do it, because it seems that threejs cannot be used for this effect.
Go directly to the demo first: http://whxaxes.github.io/canvas-test/src/Funny-demo/lowpoly/index.html (You can also watch it on the mobile terminal, but because of the calculation The amount is relatively large, and it will take more time to calculate on a mobile device than on a PC)
To achieve this effect, you mainly need to triangulate the image and perform edge detection on the image. Of these two, the first uses the delaunay triangulation algorithm, and the second uses the sobel edge detection algorithm. It sounds very high-end, but both algorithms have corresponding open source components that can be used directly: ironwallaby's delaunay component and Miguel Mota's sobel component.
Of these two algorithms, sobel is better, but delaunay is a bit complicated. We can study it in the future. However, if you only want to create an effect at the moment, you can still use these components.
Now that the two most important components are in place, the rest is very simple:
First draw the image onto canvas:
canvas.width = (img.width > 800) ? 800 : img.width; canvas.height = img.height * canvas.width/img.width; ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
Then get the imgData of canvas, and then return the new imgData through sobel calculation
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height); var newImgData = Sobel(imgData);
If we put newImgData on canvas, we will find that the color image becomes a grayscale image like this:
Since the Sobel component mentioned above is not very suitable for my usage, and the code is also inappropriate, I made appropriate modifications and optimizations, optimized the loop method, accelerated the calculation speed, and added a callback function. For details, please see the sobel.js file in the project github
When traversing imgData.data in the Sobel method, the callback function will be called, and the coordinate points with a color value greater than 40 (that is, the grayscale is above rgb(40,40,40)) will be recorded in the callback. Come down. Then randomly obtain a part of the edge points, and then add some random coordinates and the coordinate values of the four corners. In this way, we can get the coordinate points we need
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height); // 收集色值大于40的边缘像素点 var collectors = []; Sobel(imgData , function(value , x , y){ if(value > 40){collectors.push([x , y]);} }); // 添加一些随机点 for(var i=0;i<300;i++){particles.push([Math.random()*canvas.width , Math.random()*canvas.height]);} // 添加随机边缘点,数量为边缘点数量除于50 var length = ~~(collectors.length/50), random; for(var l=0;l<length;l++){ random = (Math.random()*collectors.length)<<0; particles.push(collectors[random]); collectors.splice(random , 1); } // 添加四顶点坐标 particles.push([0,0] , [0,canvas.height] , [canvas.width,0] , [canvas.width,canvas.height]);
After obtaining the coordinate points, you can use the delaunay component to calculate and obtain the triangular coordinate array in the correct order. By connecting the points in these arrays, you can have the following effect:
Of course, the effect we want is not to connect lines, but to color-fill all triangles, that is, to obtain the three coordinates of the triangle, then calculate the coordinates of the center point, and then obtain the corresponding coordinates in imgData based on the center point coordinates RGB color value, and then fill it into the triangular area:
// 使用delaunay三角化获取三角坐标 var triangles = Delaunay.triangulate(particles); var x1,x2,x3,y1,y2,y3,cx,cy; for(var i=0;i < triangles.length; i+=3) { x1 = particles[triangles[i]][0]; x2 = particles[triangles[i+1]][0]; x3 = particles[triangles[i+2]][0]; y1 = particles[triangles[i]][1]; y2 = particles[triangles[i+1]][1]; y3 = particles[triangles[i+2]][1]; // 获取三角形中心点坐标 cx = ~~((x1 + x2 + x3) / 3); cy = ~~((y1 + y2 + y3) / 3); // 获取中心点坐标的颜色值 index = (cy*imgData.width + cx)*4; var color_r = imgData.data[index]; var color_g = imgData.data[index+1]; var color_b = imgData.data[index+2]; // 绘制三角形 ctx.save(); ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.lineTo(x3, y3); ctx.closePath(); ctx.fillStyle = "rgba("+color_r+","+color_g+","+color_b+",1)"; ctx.fill(); ctx.restore(); }
One thing to note above is that the obtained center point coordinates must be rounded to get the correct color parameters. If you want not to round, but to round when getting the rgb index, you can get The color value is wrong. Because the pixel obtained in this way is not the center pixel we want.
After the color is obtained, it is a simple connection and then filling operation. The final effect is:
The above content introduces you to using canvas to achieve the effect of triangulating a picture (LOW POLY). I hope it will be helpful to you!