JavaScript canvas realizes the effect of nine-square grid cutting

JavaScript canvas realizes the effect of nine-square grid cutting

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:
  • JavaScript+canvas makes a nine-square grid applet
  • Example of image cutting effect using javascript
  • JavaScript image cutting effect (magnifying glass)

<<:  How to install and deploy ftp image server in linux

>>:  MySQL trigger principle and usage example analysis

Recommend

Detailed process of installing Jenkins-2.249.3-1.1 with Docker

Table of contents 1. Install Docker 2. Pull the J...

Docker's flexible implementation of building a PHP environment

Use Docker to build a flexible online PHP environ...

Understand the use of CSS3's all attribute

1. Compatibility As shown below: The compatibilit...

The implementation principle of Mysql master-slave synchronization

1. What is MySQL master-slave synchronization? Wh...

Docker image import, export, backup and migration operations

Export: docker save -o centos.tar centos:latest #...

Vue implements nested routing method example

1. Nested routing is also called sub-routing. In ...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Detailed explanation of the entry-level use of MySql stored procedure parameters

Use of stored procedure in parameters IN paramete...

Linux Autofs automatic mount service installation and deployment tutorial

Table of contents 1. Introduction to autofs servi...

Copy the contents of one file to the end of another file in linux

Problem description: For example, the content of ...

Cross-origin image resource permissions (CORS enabled image)

The HTML specification document introduces the cr...

Solve the problem of insufficient docker disk space

After the server where Docker is located has been...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

MySQL performance optimization tips

MySQL Performance Optimization MySQL is widely us...

HTML Basics: HTML Content Details

Let's start with the body: When viewing a web ...