Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Resize Image Using HTML Canvas in JavaScript

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const img = new Image();
img.src = "https://images.pexels.com/photos/3408744/pexels-photo-3408744.jpeg?auto=compress&cs=tinysrgb&dpr=2";

img.onload = function () {
    // set height proportional to destination image
    canvas.height = canvas.width * (img.height / img.width);
    // step 1 - resize to 75%
    const oc = document.createElement('canvas');
    const octx = oc.getContext('2d');
    // Set the width & height to 75% of image
    oc.width = img.width * 0.75;
    oc.height = img.height * 0.75;
    // step 2, resize to temporary size
    octx.drawImage(img, 0, 0, oc.width, oc.height);
    // step 3, resize to final size
    ctx.drawImage(oc, 0, 0, oc.width * 0.75, oc.height * 0.75, 0, 0, canvas.width, canvas.height);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: sort dates javascript 
Javascript :: react catch error json message 
Javascript :: adobe target triggerview 
Javascript :: get last element of array javascript 
Javascript :: js deep copy 
Javascript :: edit json text react 
Javascript :: how to create an object in javascript 
Javascript :: js set css 
Javascript :: javascript removelastchild 
Javascript :: type conversions in javascript 
Javascript :: load all icon from a folder in react 
Javascript :: winston logger levels 
Javascript :: javascript compress base64 image 
Javascript :: forever loop in js 
Javascript :: loadstring json flutter 
Javascript :: discord.js mobile status 
Javascript :: this keyword in javascript 
Javascript :: cheerio library to parse the meta tags in url 
Javascript :: vue global computed property 
Javascript :: empty string in javascript 
Javascript :: apollo graphql 
Javascript :: confetti canvas 
Javascript :: mongoose discriminator 
Javascript :: jquery add event to dynamically created element 
Javascript :: select ng-options set default value 
Javascript :: random word react npm package 
Javascript :: angular property binding 
Javascript :: contextMenus chrome extensions 
Javascript :: linux command to install standard js 
Javascript :: pandas json_normalize column with json array 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =