js realizes the image cutting function

js realizes the image cutting function

This article example shares the specific code of js to achieve image cutting for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .cube{
            height: 0;
            width: 0;
            position: absolute;
            left: 0;
            top:0;
 
            box-sizing: border-box;
            background-color: lightseagreen;
            opacity: .3;
            z-index: 99; pointer-events: none;
        }
        #big{
            border: 1px solid black;
            height: 500px;
            width: 500px;
            position: relative;
            background-image: url("img/pixel.png");
            line-height: 500px;
            overflow: hidden;
            vertical-align: middle;
            text-align: center;
            float: left;
        }
        #big>img{line-height: 500px; vertical-align: middle;
            max-height: 100%;
            max-width: 100%;
            pointer-events: none;
        }
        .cv{
            position: relative;
            float: left; border: 2px solid lightseagreen;
        }
    </style>
    <script>
        let count=0;let ​​c,b,d,p1,p2,cv,cx,img,p;
        window.onload = function () {
            c=document.querySelectorAll(".cube");
            b=document.getElementById("big");
            cv=document.getElementById("c");
            cx = cv.getContext('2d');
            img = new Image();
            d=b.querySelector("img");
            img.src=d.src;
            img.onload=function(){
                cx.drawImage(img, 0,0,500,500,0,0,500,500);
               p=img.width/b.querySelector("img").width;
               console.log(img);
            }
             p1={
                 x:0,
                 y:0
             };
            p2={
                x:0,
                y:0
            };
            b.addEventListener("click",function (e){
                count++;
                if(count===1){
                    p1.x=e.offsetX;
                    p1.y=e.offsetY;
                    p2.x=e.offsetX;
                    p2.y=e.offsetY;
                    f2();
                    f4();
                }
                if(count===2){
                    p2.x=e.offsetX;
                    p2.y=e.offsetY;
                    myDraw();
                 }
            });
            function f1(){
                c[0].style.height=p2.y+"px";
                c[1].style.height=p2.y+"px";
                c[2].style.height=p2.y+"px";
                c[3].style.top=p2.y+"px";
                c[4].style.top=p2.y+"px";
                c[3].style.height=(p1.y-p2.y)+"px";
                c[4].style.height=(p1.y-p2.y)+"px";
                c[5].style.top=p1.y+"px";
                c[6].style.top=p1.y+"px";
                c[5].style.height=(b.offsetHeight-p1.y)+"px";
                c[6].style.height=b.offsetHeight-p1.y+"px";
                c[7].style.top=p1.y+"px";
                c[7].style.height=(b.offsetHeight-p1.y)+"px";
            }
            function f2(){
                c[0].style.height=p1.y+"px";
                c[1].style.height=p1.y+"px";
                c[2].style.height=p1.y+"px";
                c[3].style.top=p1.y+"px";
                c[4].style.top=p1.y+"px";
                c[3].style.height=(p2.y-p1.y)+"px";
                c[4].style.height=(p2.y-p1.y)+"px";
                c[5].style.top=p2.y+"px";
                c[6].style.top=p2.y+"px";
                c[5].style.height=(b.offsetHeight-p2.y)+"px";
                c[6].style.height=b.offsetHeight-p2.y+"px";
                c[7].style.top=p2.y+"px";
                c[7].style.height=(b.offsetHeight-p2.y)+"px";
            }
            function f3(){
                c[0].style.width=p2.x+"px";
                c[1].style.left=p2.x+"px";
                c[1].style.width=(p1.x-p2.x)+"px";
                c[2].style.left=p1.x+"px";
                c[2].style.width=(b.offsetWidth-p1.x)+"px";
                c[3].style.width=p2.x+"px";
                c[4].style.left=p1.x+"px";
                c[4].style.width=(b.offsetWidth-p2.x)+"px";
                c[5].style.width=p2.x+"px";
                c[6].style.left=p2.x+"px";
                c[6].style.width=(p1.x-p2.x)+"px";
                c[7].style.left=p1.x+"px";
                c[7].style.width=(b.offsetWidth-p1.x)+"px";
            }
            function f4(){
                c[0].style.width=p1.x+"px";
                c[1].style.left=p1.x+"px";
                c[1].style.width=(p2.x-p1.x)+"px";
                c[2].style.left=p2.x+"px";
                c[2].style.width=(b.offsetWidth-p2.x)+"px";
                c[3].style.width=p1.x+"px";
                c[4].style.left=p2.x+"px";
                c[4].style.width=(b.offsetWidth-p2.x)+"px";
                c[5].style.width=p1.x+"px";
                c[6].style.left=p1.x+"px";
                c[6].style.width=(p2.x-p1.x)+"px";
                c[7].style.left=p2.x+"px";
                c[7].style.width=(b.offsetWidth-p2.x)+"px";
            }
            b.addEventListener("mousemove",function (e){
                if(count===1){
                    p2.x=e.offsetX;
                    p2.y=e.offsetY;
                     if(p2.y<p1.y){
                        f1();
                     }else{
                         f2();
                     }
 
                    if(p2.x<p1.x){
                        f3();
                    }else{
                        f4();
                     }
 
                 }
            })
 
        }
        function myDraw(){
            cx.clearRect(0,0,500,500);
            let w=p1.x-p2.x;
            if(w<0){
                w=-w;
            }
            w*=p;
            let h=p1.y-p2.y;
            if(h<0){
                h=-h;
            }
            h*=p;
            if(p1.x<p2.x){
                p1.x=(p1.xb.querySelector("img").offsetLeft)*p;
            }else{
                p1.x=(p2.xb.querySelector("img").offsetLeft)*p;
            }
            if(p1.y<p2.y){
                p1.y=(p1.yb.querySelector("img").offsetTop)*p;
            }else{
                p1.y=(p2.yb.querySelector("img").offsetTop)*p;
            }
            cx.drawImage(img,
                p1.x,p1.y,
                w,h,0,0,500,500);
            p1.x=0;
            p1.y=0;
            p2.x=0;
            p2.y=0;
        }
    </script>
 
 
</head>
<body>
 
<div
id="big">
    <!-- 8 div covers-->
    <div class="cube"></div>
    <div class="cube"></div>
    <div class="cube"></div>
    <div class="cube"></div>
    <div class="cube"></div>
    <div class="cube"></div>
    <div class="cube"></div>
    <div class="cube"></div>
    <img src="img/katy2.jpg" alt=""/>
</div>
<div class="cv">
    <canvas id="c" height="500" width="500">
 
    </canvas>
</div>
</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:
  • js to achieve avatar image cutting and scaling and non-refresh upload image method
  • Example of image cutting effect using javascript
  • JavaScript image cutting effect (magnifying glass)

<<:  Detailed explanation of the basic functions and usage of MySQL foreign keys

>>:  Steps to use VMWare to build a Linux environment under Windows (picture and text)

Recommend

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

Detailed explanation of Vue's live broadcast function

Recently, the company happened to be doing live b...

How to import, register and use components in batches in Vue

Preface Components are something we use very ofte...

Vue integrates Tencent Map to implement API (with DEMO)

Table of contents Writing Background Project Desc...

Detailed explanation of React setState data update mechanism

Table of contents Why use setState Usage of setSt...

Using zabbix to monitor the ogg process (Windows platform)

This article introduces how to monitor the ogg pr...

Simple usage example of MySQL 8.0 recursive query

Preface This article uses the new features of MyS...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

Solve the problem that the time zone cannot be set in Linux environment

When changing the time zone under Linux, it is al...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

VMware ESXi 5.5 deployment and configuration diagram process

Table of contents 1. Installation requirements 2....

How to view and clean up Docker container logs (tested and effective)

1. Problem The docker container logs caused the h...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...

Detailed explanation of VUE's data proxy and events

Table of contents Review of Object.defineProperty...

Detailed explanation of the basic usage of VUE watch listener

Table of contents 1. The following code is a simp...