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

MySQL uses the truncate command to quickly clear all tables in a database

1. Execute the select statement first to generate...

HTML simple web form creation example introduction

<input> is used to collect user information ...

View the number of files in each subfolder of a specified folder in Linux

count script #!/bin/sh numOfArgs=$# if [ $numOfAr...

Summary of Commonly Used MySQL Commands in Linux Operating System

Here are some common MySQL commands for you: -- S...

How to add a certificate to docker

1. Upgrade process: sudo apt-get update Problems ...

How to implement MySQL master-slave replication based on Docker

Preface MySQL master-slave replication is the bas...

Solution for FileZilla 425 Unable to connect to FTP (Alibaba Cloud Server)

Alibaba Cloud Server cannot connect to FTP FileZi...

Using js to implement simple switch light code

Body part: <button>Turn on/off light</bu...

50 lines of code to implement Webpack component usage statistics

background Recently, a leader wanted us to build ...

Summary of MySQL ALTER command knowledge points

When we need to change the table name or modify t...

How to configure common software on Linux

When you get a new Linux server, you generally ha...

Mini Programs enable product attribute selection or specification selection

This article shares the specific code for impleme...

Vue two fields joint verification to achieve the password modification function

Table of contents 1. Introduction 2. Solution Imp...