How to realize the effect of Nine Palace Grid cut by canvas in JavaScript
Editor to share with you how to achieve the nine palace grid cut effect of canvas in JavaScript. I hope you will get something after reading this article. Let's discuss it together.
First of all, the page is displayed
Go directly to the code
Document body {text-align: center;} canvas {border: 1px solid;} .newcanvas {width: 316px; height: 316px; margin: auto;} .newpohoto, .download {width: 300px Height: 40px; line-height: 40px; margin: auto; background-color: cornflowerblue; border-radius: 5px; cursor: pointer; margin: 10px auto; color: white } use canvas to achieve the effect of cutting images in the ninth palace. Start cutting and downloading pictures var canvas = document.getElementById ("mycnavas") / / now put the picture on var cxt = mycnavas.getContext ("2d"); var img = new Image (); img.src = ".. / img/img10.jpg"; _ window.onload = function () {cxt.drawImage (img, 0,0400,300); / / the position where the picture is drawn} var arr = [] / save the cut images in the array document.getElementsByClassName ("newpohoto") [0] .onclick = function () {var Q = 1; for (var I = 0; I < 3; iSense +) {for (var j = 0; j < 3; jacks +) {var data = cxt.getImageData (j * 100,100,400,100) / / similar to the "copy" function var img = document.getElementById ("img" + Q) var newcxt = img.getContext ("2d"); newcxt.putImageData (data, 0,0) / / two parameters of the similar "paste" function arr.push (console.log (img.toDataURL (Q + ".png")) / / toDataURL () method: DataURL (type, encoderOptions) type specifies the format of the image converted to base64 encoding, such as image/png, image/jpeg, image/webp, etc., which defaults to image/png format Qupload images; console.log (arr)} / / download cut images var arr = []; document.getElementsByClassName ('download') [0] .onclick = function () {for (var I = 0; I < 9) Var +) {var a = document.createElement ('a'); a.download = 'img' + (I + 1); a.href = arr [I]; document.body.appendChild (a); a.click () }} after reading this article, I believe you have a certain understanding of "how to achieve the nine-grid cut effect of canvas in JavaScript". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!