JavaScript canvas to load pictures

JavaScript canvas to load pictures

This article shares the specific code of JavaScript canvas to load pictures for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Using image</title>
        <style type="text/css">
            * {
                /* margin: 0;
                padding: 0; */
                box-sizing: border-box;
            }
            canvas {
                border-width: 1px;
                border-color: #000000;
                border-style: solid;
            }
        </style>
    </head>
    <body>
        <canvas id="canvas"></canvas>
        
        <div>
            <input type="file" accept="image/*" />
        </div>
        
        <script type="text/javascript">
            window.onload = (event) => {
                main()
            }
            
            function main() {
                const canvas = document.getElementById("canvas");
                const ctx = canvas.getContext("2d");
                
                const inputFile = document.querySelector("input[type=file]");
                inputFile.onchange = (event) => {
                    const files = event.target.files;
                    if (files.length > 0) {
                        const file = files[0]; // First file
                        console.log(file);
                        
                        const image = new Image();
                        image.src = URL.createObjectURL(file);
                        image.onload = function(event) {
                            // console.log(event, this);
                            URL.revokeObjectURL(this.src);
                            
                            canvas.width = image.width;
                            canvas.height = image.height;
                            
                            ctx.drawImage(image, 0, 0);
                        }
                    }
                }
            }
        </script>
    </body>
</html>

Reference: Link

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+HTML5 canvas to implement a simple loading bar (progress bar) function example
  • JS implements preloading of video audio/video to obtain screenshots (return canvas screenshots)
  • JS Canvas timer simulates dynamic loading animation
  • JavaScript to achieve web page loading progress bar code is super simple
  • JavaScript implements a web page loading progress loading
  • Simple implementation of js progress bar loading effect
  • Native JS to achieve home page progress loading animation
  • pace.js page loading progress bar plugin
  • JavaScript to implement page loading progress bar code
  • JavaScript canvas realizes water ball loading animation

<<:  Introduction to ufw firewall in Linux

>>:  Detailed explanation of MySQL event modification events (ALTER EVENT), disabling events (DISABLE), enabling events (ENABLE), event renaming and database event migration operations

Recommend

Text pop-up effects implemented with CSS3

Achieve resultsImplementation Code html <div&g...

Why can't the MP4 format video embedded in HTML be played?

The following code is in my test.html. The video c...

CSS3 realizes the effect of triangle continuous enlargement

1. CSS3 triangle continues to zoom in special eff...

How to use resident nodes for layer management in CocosCreator

CocosCreator version: 2.3.4 Most games have layer...

The image element img has extra blank space in IE6

When doing DIV+CSS layout of the page, it is very...

How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Hexo binds a custom domain name to GitHub under W...

Sample code for implementing PC resolution adaptation in Vue

Table of contents plan Install Dependencies Intro...

How to encapsulate the table component of Vue Element

When encapsulating Vue components, I will still u...

Detailed explanation of adding dotted lines to Vue element tree controls

Table of contents 1. Achieve results 2. Implement...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

A brief discussion on CSS height collapse problem

Performance For example: HTML: <div class=&quo...

CentOS 7.2 builds nginx web server to deploy uniapp project

Panther started as a rookie, and I am still a roo...

Detailed explanation of custom swiper component in JavaScript

Table of contents Effect display Component Settin...