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

Specific use of routing guards in Vue

Table of contents 1. Global Guard 1.1 Global fron...

Three solutions for sub-functions accessing external variables in JavaScript

Preface When we write web pages, we will definite...

Discuss the value of Web standards from four aspects with a mind map

I have roughly listed some values ​​to stimulate ...

Steps for encapsulating element-ui pop-up components

Encapsulate el-dialog as a component When we use ...

Use Vue3 for data binding and display list data

Table of contents 1. Comparison with Vue2 1. New ...

Introduction to CSS style classification (basic knowledge)

Classification of CSS styles 1. Internal style --...

MySQL transaction control flow and ACID characteristics

Table of contents 1. ACID Characteristics Transac...

Avoiding Problems Caused by Closures in JavaScript

About let to avoid problems caused by closure Use...

Vue.js cloud storage realizes image upload function

Preface Tip: The following is the main content of...

How to compile and install xdebug in Ubuntu environment

This article describes how to compile and install...

How to support full Unicode in MySQL/MariaDB

Table of contents Introduction to utf8mb4 UTF8 by...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

Binary installation of mysql 5.7.23 under CentOS7

The installation information on the Internet is u...