How To Resize An Image In An HTML 5 Canvas ? - GeeksforGeeks
Có thể bạn quan tâm
Skip to content
- Courses
- DSA to Development
- Machine Learning & Data Science
- Generative AI & ChatGPT
- Become AWS Certified
- DSA Courses
- Data Structure & Algorithm(C++/JAVA)
- Data Structure & Algorithm(Python)
- Data Structure & Algorithm(JavaScript)
- Programming Languages
- CPP
- Java
- Python
- JavaScript
- C
- Tutorials
- Python Tutorial
- Taking Input in Python
- Python Operators
- Python Data Types
- Python Numbers
- Python String
- Python Lists
- Python Tuples
- Sets in Python
- Python Dictionary
- Python Loops and Control Flow
- Python Conditional Statements
- Python Loops
- Python Functions
- Python OOPS Concept
- Python Data Structures
- Python DSA
- Linked List
- Stack
- Queue
- Tree
- Heap
- Hashing
- Graph
- Sets
- Map
- Advance Data Structure
- Sorting Algorithms
- Searching Algorithms
- Python Exception Handling
- Python File Handling
- Python Exercises
- Python List Exercise
- Python String Exercise
- Python Tuple Exercise
- Python Dictionary Exercise
- Python Set Exercise
- Python Design Patterns
- Python Programming Examples
- Python Practice Questions
- Java
- Java Programming Language
- Java Tutorial
- Data Types
- Variables
- Operators
- Flow Control in Java
- Loops in Java
- Methods
- Strings
- Arrays
- OOPs Concepts
- OOPs Concepts
- Classes and Objects
- Access Modifiers
- Inheritance
- Abstraction
- Encapsulation
- Polymorphism
- Interface
- Packages
- Multithreading
- File Handling
- Regular Expression
- Java Collections
- Java Collections
- Collection Class
- List Interface
- ArrayList
- LinkedList Class
- Queue Interface
- Set Interface
- HashSet Class
- Map Interface
- HashMap Class
- HashTable Class
- Iterator
- Comparator
- Collection Interview Questions
- Java 8 Tutorial
- Java Programs
- Java Programming Examples
- Java Array Programs
- Java String Programs
- Java Date-Time Programs
- Java File Handling Programs
- Java Collection Programs
- Java JDBC Programs
- Java Apache POI Programs
- Java OpenCV Programs
- Java Interview Questions
- Java Interview Questions
- Core Java Interview Questions-Freshers
- Java Multithreading Interview Questions
- OOPs Interview Questions and Answers
- Java Exercises
- Java Quiz
- Java Quiz
- Core Java MCQ
- Java Projects
- Advance Java
- Spring Tutorial
- Spring Boot Tutorial
- Spring Boot Interview Questions
- Spring MVC Tutorial
- Spring MVC Interview Questions
- Hibernate Tutorial
- Hibernate Interview Questions
- Java Programming Language
- Programming Languages
- C
- C++
- JavaScript
- PHP
- R Tutorial
- C#
- SQL
- Scala
- Perl
- Go Language
- Kotlin
- System Design
- System Design Tutorial
- What is System Design
- Key Terminologies in System Design
- Analysis and Architecture of Systems
- Scalability in System Design
- Databases in System Design
- High Level Design or HLD
- Low Level Design or LLD
- Case Studies in Designing Systems
- Complete System Design Tutorial
- Software Design Patterns
- System Design Roadmap
- Top 10 System Design Interview Questions and Answers
- System Design Tutorial
- Interview Corner
- Company Preparation
- Top Topics
- Practice Company Questions
- Interview Experiences
- Experienced Interviews
- Internship Interviews
- Competitive Programming
- Multiple Choice Quizzes
- Aptitude for Placements
- Computer Science Subjects
- Operating System
- DBMS
- Computer Networks
- Engineering Mathematics
- Computer Organization and Architecture
- Theory of Computation
- Compiler Design
- Digital Logic
- Software Engineering
- DevOps
- GIT
- AWS
- Docker
- Kubernetes
- Microsoft Azure Tutorial
- Google Cloud Platform
- Linux
- Linux Tutorial
- Linux Commands A-Z
- Linux Commands Cheatsheet
- File Permission Commands
- Linux System Administration
- Linux File System
- Linux Shell Scripting
- Linux Networking
- Linux Interview Questions
- Software Testing
- Software Testing Tutorial
- Software Engineering Tutorial
- Testing Interview Questions
- Jira
- Databases
- DBMS Tutorial
- SQL Tutorial
- PostgreSQL Tutorial
- MongoDB Tutorial
- SQL Interview Questions
- MySQL Interview Questions
- PL/SQL Interview Questions
- Android
- Android Tutorial
- Android Studio Tutorial
- Kotlin For Android
- Android Projects
- Android Interview Questions
- 6 Weeks of Android App Development
- Excel
- MS Excel Tutorial
- Introduction to MS Excel
- Data Analysis in Excel
- Basic Excel Formulas & Functions
- Data Analysis in Advanced Excel
- Workbooks
- Statistical Functions
- Data Visualization in Excel
- Pivot Tables in Excel
- Excel Spreadsheets in Python
- Basic Excel Shortcuts
- Mathematics
- Number System
- Algebra
- Linear Algebra
- Trigonometry
- Set Theory
- Statistics
- Probability
- Geometry
- Mensuration
- Logarithms
- Calculus
- Python Tutorial
- DSA
- Data Structures
- Arrays
- Matrix
- Strings
- Linked List
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Doubly Circular Linked List
- Linked List Tutorial
- Stack
- Queue
- Tree
- Generic Tree
- Binary Tree
- Binary Search Tree
- AVL Tree
- B Tree
- B+ Tree
- Red Black Tree
- Tree Data Structure Tutorial
- Heap
- Hashing
- Graph
- Set Data Structure
- Map Data Structure
- Advanced Data Structure
- Data Structures Tutorial
- Algorithms
- Analysis of Algorithms
- Design and Analysis of Algorithms
- Asymptotic Analysis
- Asymptotic Notations
- Worst, Average and Best Cases
- Searching Algorithms
- Linear Search
- Binary Search
- Searching Algorithms Tutorial
- Sorting Algorithms
- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
- Radix Sort
- Bucket Sort
- Sorting Algorithms Tutorial
- Greedy Algorithms
- Dynamic Programming
- Graph Algorithms
- Pattern Searching
- Recursion
- Backtracking
- Divide and Conquer
- Mathematical Algorithms
- Geometric Algorithms
- Bitwise Algorithms
- Randomized Algorithms
- Branch and Bound
- Algorithms Tutorial
- Analysis of Algorithms
- DSA Tutorial
- Practice
- All DSA Problems
- Problem of the Day
- Company Wise Coding Practice
- Amazon
- Microsoft
- Flipkart
- Explore All
- GfG SDE Sheet
- Practice Problems Difficulty Wise
- School
- Basic
- Easy
- Medium
- Hard
- Language Wise Coding Practice
- CPP
- Java
- Python
- Curated DSA Lists
- Beginner's DSA Sheet
- Top 50 Array Problems
- Top 50 String Problems
- Top 50 DP Problems
- Top 50 Graph Problems
- Top 50 Tree Problems
- Competitive Programming
- Company Wise SDE Sheets
- Facebook SDE Sheet
- Amazon SDE Sheet
- Apple SDE Sheet
- Netflix SDE Sheet
- Google SDE Sheet
- DSA Cheat Sheets
- SDE Sheet
- DSA Sheet for Beginners
- FAANG Coding Sheet
- Product-Based Coding Sheet
- Company-Wise Preparation Sheet
- Top Interview Questions
- Puzzles
- All Puzzles
- Top 100 Puzzles Asked In Interviews
- Top 20 Puzzles Commonly Asked During SDE Interviews
- Data Structures
- Data Science
- Python Tutorial
- R Tutorial
- Machine Learning
- Data Science using Python
- Data Science using R
- Data Science Packages
- Pandas Tutorial
- NumPy Tutorial
- Data Visualization
- Python Data Visualization Tutorial
- Data Visualization with R
- Data Analysis
- Data Analysis with Python
- Data Analysis with R
- Deep Learning
- NLP Tutorial
- Web Tech
- HTML Tutorial
- CSS Tutorial
- JavaScript Tutorial
- PHP Tutorial
- ReactJS Tutorial
- NodeJS Tutorial
- AngularJS Tutorial
- Bootstrap Tutorial
- Typescript
- Web Development Using Python
- Django
- Django Tutorial
- Django Projects
- Django Interview Questions
- Flask
- Flask Tutorial
- Flask Projects
- Flask Interview Questions
- Postman
- Github
- Django
- Wordpress Tutorial
- Web Design
- Cheat Sheets
- HTML Cheat Sheet
- CSS Cheat Sheet
- JavaScript Cheat Sheet
- React Cheat Sheet
- Angular Cheat Sheet
- jQuery Cheat Sheet
- Bootstrap Cheat Sheet
- Learn Complete Web Development
- HTML Tutorial
- HTML Exercises
- HTML Tags
- HTML Attributes
- Global Attributes
- Event Attributes
- HTML Interview Questions
- HTML DOM
- DOM Audio/Video
- HTML 5
- HTML Examples
- Color Picker
- A to Z Guide
- HTML Formatter
The canvas element is part of HTML 5 and it allows the rendering of 2D shapes and bitmap(also called “raster”) images. A canvas actually has two sizes:
- the size of the element.
- the size of the element’s drawing surface.
The element’s width and height attributes set both the size of the element and the size of the element’s drawing surface. CSS attributes affect only the element’s size and not the drawing surface.
Example:
<!DOCTYPE html> <html> <body> <p>Image:</p> <img id="forest" width="220" height="277" src= "https://media.geeksforgeeks.org/wp-content/uploads/20190809013546/gfg_350X350.png" alt="Forest"> <p>Canvas:</p> <canvas id="Canvas" width="300" height="200" style="border:15px solid #000066;"> Your browser not support the HTML5 canvas . </canvas> <script> window.onload = function() { var canvas = document.getElementById("Canvas"); var context = canvas.getContext("2d"); var img = document.getElementById("forest"); context.drawImage(img, 12, 8); }; </script> </body> </html> |
Output:
H
himanshushinde0410 Follow Improve Next Article How to Resize an Image in HTML?Similar Reads
Chart.js Canvas Resize The Chart.js library provides responsive, interactive charts that automatically resize based on the size of their parent container. By default, charts created with Chart.js are responsive and will adjust their dimensions to fit the available space on the page. However, if you need to manually resize a chart, you can set the responsive property to t 3 min read How to Force Image Resize and Keep Aspect Ratio in HTML ? To resize an image in HTML while keeping its aspect ratio, you can use simple HTML or CSS techniques. The problem is to ensure that the image scales without distortion or cropping. This can be fixed by applying simple styles that resize the image while keeping its original proportions. Below are the various methods to force image resize and keep th 2 min read How to Resize an Image in HTML? Images are a vital part of any webpage, enhancing the visual appeal and improving user engagement. However, displaying images at the correct size is essential for a professional look and optimal page performance. Resizing an image in HTML ensures that it fits within your webpage's design while maintaining fast loading times. This article will explo 2 min read How to Converte Chart.js Canvas Chart to Image using .toDataUrl() Results in Blank Image? In Chart.js, we can convert the canvas chart into an image using the .toDataURL() method, which generates a base64-encoded data URL representing the chart. This allows us to save or display the chart as an image. These are the following approaches: Table of Content Basic Conversion to ImageAdvanced Conversion with Custom Image StylingChartJS CDN Li 4 min read How to resize list style image in CSS ? In this article, we will learn to set an image or icon as the <li> image. We need to set the size of the custom list image to make it attractive. So, we will also learn the different ways to resize the custom list image. Syntax: In this article, we will use below CSS properties. background-image: We will add a custom image as a background ima 3 min read p5.js Image resize() Method The resize() method of p5.Image in p5.js is used to resize the image to the given width and height. The image can be scaled proportionally by using 0 as one of the values of the width and height. Syntax: resize( width, height ) Parameters: This function accepts two parameters as mentioned above and described below. width: It is a number that specif 2 min read How to Resize a SVG image ? Resizing an SVG image cannot be done by simply using CSS height and width property to the img tag. To resize the SVG image, follow these steps: Method1: The SVG image will be taking 100% of all the width and height available to it. To make the image of the desired size set the CSS height and width property of the image Example 1: C/C++ Code <!DO 1 min read Create Image Resize App using Tailwind CSS Image Resizer is an application that is used to resize an uploaded image by providing new dimensions in the form of width and height. This application contains two numbered input fields to get the number input for width and height from the user. It also contains a file input field to upload an image file. It allows you to download the resized image 3 min read Create an Image Resize and Editor using React-Native Image manipulation in mobile apps is an important functionality, from cropping and resizing to adding filters or overlays. In this tutorial, you will learn the process of building a simple Image Resize and Editor application using React-Native. This project aims to provide a foundation to integrate image editing capabilities into their React-Native 3 min read How to Auto Resize Image in CSS FlexBox Layout and Keeping Aspect Ratio? In modern web design, responsive images are crucial to ensure that your site looks good on all devices. CSS Flexbox provides a powerful layout mode that allows you to create flexible and responsive layouts. We'll explore how to auto-resize images within a CSS Flexbox layout while maintaining their aspect ratio. ApproachDefine a div container with a 2 min read How To Resize Image in GitHub Using Markdown? Markdown is a popular markup language that simplifies formatting text, commonly used in README files, wikis, and documentation across various platforms like GitHub. It provides a simple way to embed images but doesn't include built-in options to resize them directly. In this article, we will see the different methods to resize Images in GitHub usin 3 min read How to Auto-Resize an Image to Fit a Div Container using CSS? To resize an image or video to fit inside a div container, you can use the object-fit property. This property is used to specify how the content should fit inside the container. With object-fit, you can set the content to maintain its aspect ratio or stretch to fill the entire container. This allows you to control how the content is displayed withi 3 min read Resize image proportionally with CSS To resize an image proportionally with CSS, you can use the max-width property to ensure the image adjusts automatically to fit its container without stretching. The height: auto rule maintains the image’s aspect ratio, preventing distortion. Following are how we can resize the image proportionally using CSS: Table of Content Resizing with max-widt 2 min read How to save an HTML 5 Canvas as an image on the server ? Saving HTML canvas as an image is pretty easy, it can be done by just right-clicking on the canvas and save it as an image. But saving canvas as an image on the server is quite different. This article will show you how to achieve that. Sometimes it is required to save canvas image after doing some server processing and this article will help in sen 3 min read HTML | DOM Style resize Property The Style resize property in HTML DOM is used to specify whether an element is resizable in height and width by the user. Syntax: It returns the resize property:object.style.resizeIt is used to set the resize property:object.style.resize = "both|horizontal|vertical|none|initial| inherit" Property Values: 1. both: This value enables the user to chan 5 min read How to disable the resize grabber of <textarea> in HTML ? You can use the resize property to specify whether a text area should be resizable or not. The <textarea> is an HTML tag used to create a multi-line input field. By default, <textarea> includes a resize grabber in the bottom right corner which allows users to resize the input field by dragging the grabber with the mouse. Syntax:textarea 2 min read Create a Resize and Compress Images in HTML CSS & JavaScript While using the GeeksforGeeks Write Portal to write articles, we need to upload the images. As we need to resize the image as per GeeksforGeeks's requirement, we search for different tools and websites on the internet to resize and compress the image. But, as a web developer, we can create our own image resize and compressor using HTML, CSS, and Ja 7 min read How to convert canvas graphics to image ? In this article, we will learn how to convert the contents of the canvas to an image and download it to the download manager of the existing browser. The Canvas graphics is used to draw the graphics with the help of the <canvas> element on the web page. It can act as a graphics container. The different methods that are facilitated by the canv 3 min read How to create a canvas image using Fabric.js ? Creating a canvas image using Fabric.js is a powerful method for generating dynamic and interactive graphical content on the web. Fabric.js, a robust JavaScript library, simplifies the process of working with the HTML5 canvas element by providing a rich API for manipulating shapes, text, and images. With Fabric.js, developers can easily create, tra 2 min read How to Crop an Image Using Canvas? Cropping an image using an HTML5 canvas involves selecting a specific portion of an image and displaying only that part on the canvas. This technique is useful for focusing on a particular area of an image, removing unwanted sections, or fitting images into specific layouts without altering the original file. ApproachIn this approach, we will creat 2 min read How to Download Canvas as Image on Button Click in ChartJS? In Chart.js, the charts that are generated can be converted and downloaded as images for easy sharing and reporting. By using built-in methods like toBase64Image() and the HTML canvas toBlob() method, we can enable users to download charts in formats such as PNG. These techniques provide a simple way to capture the visual data from charts and offer 4 min read jQuery resize() method The jQuery resize() method is an inbuilt method that is used when the browser window changes its size, resize() method triggers an event when the size of an element is changed, allowing you to perform actions or apply styles based on the new dimensions. Syntax: $(selector).resize(function)Parameter: This method accepts a single parameter function w 2 min read CSS resize Property The resize property in CSS is used to resize the element according to user requirement. It does not apply to inline elements or to block elements where overflow is visible. Syntax: resize: none|both|horizontal|vertical|initial; Property value: none both horizontal vertical initial none: The user is not able to resize the element. It is a default va 4 min read How to Resize Rotation Radius using CSS ? In dynamic CSS, you can resize rotation radius using CSS Custom Properties (Variables). As custom properties are now in the latest browsers from Mozilla, Google, Opera, Apple, and Microsoft – it’s definitely a good time to explore and experiment. The pre-requisites to learn CSS Variables and CSS var() Function. Approach: First, defines a global cus 2 min read Node Jimp | resize The resize() function is an inbuilt function in Nodejs | Jimp which resizes the image to a set width and height using a 2-pass bilinear algorithm. Syntax: resize(w, h, mode, cb) Parameter: w: This parameter stores the width of the image.h: This parameter stores the height of the image.mode: This is an optional parameter that stores the scaling meth 2 min read Cross-browser window resize event using JavaScript/jQuery The resize() method is an inbuilt method in jQuery that is used when the browser window changes its size. The resize() method triggers the resize event or attaches a function to run when a resize event occurs. jQuery has a built-in method for window resizing events. Syntax: $(selector).resize(function) This syntax is used for cross-browser resize e 2 min read Node.js GM resize() Function The resize() function is an inbuilt function in the GraphicsMagick library which is used to resize an image. The function returns the true value of success. Syntax: raise( width, height ) Parameters: This function accepts two parameters as mentioned above and described below: width: This parameter is used to specify the width of the image.height: T 1 min read jQuery UI Dialog resize Event jQuery UI resize event is triggered when the dialog box is resized. Learn more about jQuery selectors and events here. Syntax: $(".selector").dialog ( resize: function( event, ui ) { console.log('resized') },Approach: First, add jQuery Mobile scripts needed for your project.<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jque 1 min read How to resize SVG icon using Tailwind CSS ? SVG stands for Scalable Vector Graphics and is an XML-based ( can be edited ) vector image format. SVG is commonly used for icons, animations, interactive charts, graphs, and other dynamic graphics in the browser. As it is XML-based, you can easily resize the SVG icon using Tailwind. Approach: You can simply customize the class of SVG by changing t 3 min read jQWidgets jqxWindow resize() Method Introduction: jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxWindow is used for entering data or viewing information in an application. The resize() method is used to resize the current window. The 'resiza 2 min read Article Tags :- HTML
- Web Technologies
- HTML-Misc
What kind of Experience do you want to share?
Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam ExperiencesTừ khóa » Html Canvas Stretch Image
-
Html5 Canvas DrawImage Stretched - Javascript - Stack Overflow
-
HTML Canvas DrawImage() Method - W3Schools
-
Html5-canvas Tutorial => Scaling Image To Fit Or Fill.
-
Resize Image Using HTML Canvas In JavaScript | Delft Stack
-
Resize Image To Fixed Size - Canvas, Images And Pixels
-
HTML5 Resize Image In Canvas
-
Javascript – HTML Canvas: Scaling Image To Fit Without Stretching
-
CanvasRenderingContext2D.drawImage() - Web APIs | MDN
-
ageSmoothingEnabled - Web APIs
-
How To Resize Image With JavaScript Canvas? - The Web Dev
-
How To Scale An Image To Fit On Canvas With JavaScript?
-
Resize Images In JavaScript The Right Way
-
Stretch Image Inside Canvas - ADocLib
-
HTML Canvas - DrawImage - Stretched Results - Ars Technica