js canvas realizes rounded corners picture

js canvas realizes rounded corners picture

This article shares the specific code of js canvas to achieve rounded corners for your reference. The specific content is as follows

Code implementation of rounded corner images:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="background: rgba(199,237,204,1)">
 
<div style="display:flex; flex-direction: row">
 
    <!-- Setting the width and height of the canvas through style causes the drawn content to stretch vertically on Firefox. . . -->
    <canvas id="drawing" width="400px" height="400px">canvas to draw</canvas>
 
    <pre id="container" style="margin: 10px"/>
 
    <img src=//img.jbzj.com/file_images/article/202109/202191115608734.jpg>
</div>
</body>
<script type="text/javascript">
 
    window.onlοad = function () {
        var drawing = document.getElementById('drawing');
 
        if (drawing.getContext) {
            print('support')
            addRoundRectFunc();
            var context = drawing.getContext('2d');
            draw(context);
 
        } else {
            print('not ')
        }
    }
 
 
    function draw(context) {
        context.fillStyle = 'red';
 
        var image = document.images[0];
 
        context.roundRect(0,0,image.width/2,image.height/2,30,true)
 
        context.globalCompositeOperation='source-in';
        context.drawImage(image, 0, 0, image.width / 2, image.height / 2)
        // toImage();
 
    }
 
    function addRoundRectFunc() {
        CanvasRenderingContext2D.prototype.roundRect =
            function (x, y, width, height, radius, fill, stroke) {
                if (typeof stroke == "undefined") {
                    stroke = true;
                }
                if (typeof radius === "undefined") {
                    radius = 5;
                }
                this.beginPath();
                this.moveTo(x + radius, y);
                this.lineTo(x + width - radius, y);
                this.quadraticCurveTo(x + width, y, x + width, y + radius);
                this.lineTo(x + width, y + height - radius);
                this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
                this.lineTo(x + radius, y + height);
                this.quadraticCurveTo(x, y + height, x, y + height - radius);
                this.lineTo(x, y + radius);
                this.quadraticCurveTo(x, y, x + radius, y);
                this.closePath();
                if (stroke) {
                    this.stroke();
                }
                if (fill) {
                    this.fill();
                }
            };
    }
 
    function toImage() {
        var imageUri = drawing.toDataURL('image/png');
        var imageTag = document.createElement('img');
        imageTag.style = 'margin:10px;width:200px;height:200px'
        imageTag.src = imageUri;
        document.body.appendChild(imageTag)
    }
 
    function print(txt) {
        document.getElementById("container").innerHTML += ('\n') + txt;
    }
 
    document.body.onclick = function () {
        window.location.reload()
    }
    console.log = print;
 
 
</script>
 
 
</html>

Effect picture:

Add a small code: canvas generates rounded corner images (avatars, etc.)

circleImg(ctx, img, x, y, r) {
    ctx.save();
    var d =2 * r;
    var cx = x + r;
    var cy = y + r;
    ctx.arc(cx, cy, r, 0, 2 * Math.PI);
    ctx.clip();
    ctx.drawImage(img, x, y, d, d);
    ctx.restore();
  }

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+CSS to achieve a complete example of a draggable beautiful rounded corner special effect pop-up layer
  • Abstract CSS rounded corner effect implemented with js!!
  • Using js to generate and update CSS rounded corners

<<:  Centos7 configuration fastdfs and nginx distributed file storage system implementation process analysis

>>:  How to find identical files in Linux

Recommend

Websocket+Vuex implements a real-time chat software

Table of contents Preface 1. The effect is as sho...

Tutorial on how to deploy LNMP and enable HTTPS service

What is LNMP: Linux+Nginx+Mysql+(php-fpm,php-mysq...

Detailed explanation of Javascript string methods

Table of contents String length: length charAt() ...

Nginx configuration 80 port access 8080 and project name address method analysis

Tomcat accesses the project, usually ip + port + ...

CentOS7.x uninstall and install MySQL5.7 operation process and encoding format modification method

1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

No-nonsense quick start React routing development

Install Enter the following command to install it...

Tutorial on installing MySQL with Docker and implementing remote connection

Pull the image docker pull mysql View the complet...

Example of implementing load balancing with Nginx+SpringBoot

Introduction to Load Balancing Before introducing...

Example of Html shielding right-click menu and left-click typing function

Disable right-click menu <body oncontextmenu=s...

Learn one minute a day to use Git server to view debug branches and fix them

Debug branch During the normal development of a p...

Understanding the MySQL query optimization process

Table of contents Parsers and preprocessors Query...