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. Install Docker 2. Pull the J...
Use Docker to build a flexible online PHP environ...
1. Compatibility As shown below: The compatibilit...
1. What is MySQL master-slave synchronization? Wh...
Export: docker save -o centos.tar centos:latest #...
1. Nested routing is also called sub-routing. In ...
Preface This article will focus on the use of Typ...
Use of stored procedure in parameters IN paramete...
Table of contents 1. Introduction to autofs servi...
Problem description: For example, the content of ...
The HTML specification document introduces the cr...
After the server where Docker is located has been...
After Vmvare sets the disk size of the virtual ma...
MySQL Performance Optimization MySQL is widely us...
Let's start with the body: When viewing a web ...