This article shares the specific code of canvas to achieve the nine-grid cutting effect for your reference. The specific content is as follows First page display Directly on the code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> 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; } </style> </head> <body> <h3>Use canvas to achieve the effect of nine-grid cutting</h3> <div class="mycanvas"> <canvas width="300" height="300" id="mycnavas"></canvas> </div> <div class="newpohoto"> Start cutting pictures</div> <div class="newcanvas"> <canvas width="100" height="100" id="img1"></canvas> <canvas width="100" height="100" id="img2"></canvas> <canvas width="100" height="100" id="img3"></canvas> <canvas width="100" height="100" id="img4"></canvas> <canvas width="100" height="100" id="img5"></canvas> <canvas width="100" height="100" id="img6"></canvas> <canvas width="100" height="100" id="img7"></canvas> <canvas width="100" height="100" id="img8"></canvas> <canvas width="100" height="100" id="img9"></canvas> </div> <div class="download"> Download image</div> <script> var canvas = document.getElementById("mycnavas"); //Now put the picture on it var cxt = mycnavas.getContext("2d"); var img = new Image(); img.src = "../img/img10.jpg"; window.onload = function() { cxt.drawImage(img, 0, 0, 400, 300); //Draw the image position} var arr = []; //Save the cut image into the array document.getElementsByClassName("newpohoto")[0].onclick = function() { var q = 1; for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { var data = cxt.getImageData(j * 100, i * 100, 400, 100); //Similar to the "copy" function var img = document.getElementById("img" + q) var newcxt = img.getContext("2d"); newcxt.putImageData(data, 0, 0); //Similar to the "paste" function arr.push(console.log(img.toDataURL(q + ".png"))) //Two parameters of the toDataURL() method: DataURL(type, encoderOptions) type specifies the format of the image after conversion to base64 encoding, such as: image/png, image/jpeg, image/webp, etc. The default is image/png format; q++; console.log(arr) } } } //Download the cut picture var arr = []; document.getElementsByClassName('download')[0].onclick = function() { for (var i = 0; i < 9; i++) { var a = document.createElement('a'); a.download = 'img' + (i + 1); a.href = arr[i]; document.body.appendChild(a); a.click(); } } </script> </body> </html> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to install and deploy ftp image server in linux
>>: MySQL trigger principle and usage example analysis
Table of contents 1. Global Guard 1.1 Global fron...
Preface When we write web pages, we will definite...
Table of contents 1. One-way value transfer betwe...
I have roughly listed some values to stimulate ...
Encapsulate el-dialog as a component When we use ...
Table of contents 1. Comparison with Vue2 1. New ...
Classification of CSS styles 1. Internal style --...
Table of contents 1. ACID Characteristics Transac...
About let to avoid problems caused by closure Use...
Preface Tip: The following is the main content of...
es installation docker pull elasticsearch:7.4.0 #...
This article describes how to compile and install...
Table of contents Introduction to utf8mb4 UTF8 by...
Table of contents How to deploy MySQL service usi...
The installation information on the Internet is u...