[javascript] Resizing an image in an HTML5 canvas

I know this is an old thread but it might be useful for some people such as myself that months after are hitting this issue for the first time.

Here is some code that resizes the image every time you reload the image. I am aware this is not optimal at all, but I provide it as a proof of concept.

Also, sorry for using jQuery for simple selectors but I just feel too comfortable with the syntax.

_x000D_
_x000D_
$(document).on('ready', createImage);_x000D_
$(window).on('resize', createImage);_x000D_
_x000D_
var createImage = function(){_x000D_
  var canvas = document.getElementById('myCanvas');_x000D_
  canvas.width = window.innerWidth || $(window).width();_x000D_
  canvas.height = window.innerHeight || $(window).height();_x000D_
  var ctx = canvas.getContext('2d');_x000D_
  img = new Image();_x000D_
  img.addEventListener('load', function () {_x000D_
    ctx.drawImage(this, 0, 0, w, h);_x000D_
  });_x000D_
  img.src = 'http://www.ruinvalor.com/Telanor/images/original.jpg';_x000D_
};
_x000D_
html, body{_x000D_
  height: 100%;_x000D_
  width: 100%;_x000D_
  margin: 0;_x000D_
  padding: 0;_x000D_
  background: #000;_x000D_
}_x000D_
canvas{_x000D_
  position: absolute;_x000D_
  left: 0;_x000D_
  top: 0;_x000D_
  z-index: 0;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<html>_x000D_
  <head>_x000D_
    <meta charset="utf-8" />_x000D_
    <title>Canvas Resize</title>_x000D_
  </head>_x000D_
  <body>_x000D_
    <canvas id="myCanvas"></canvas>_x000D_
  </body>_x000D_
</html>
_x000D_
_x000D_
_x000D_

My createImage function is called once when the document is loaded and after that it is called every time the window receives a resize event.

I tested it in Chrome 6 and Firefox 3.6, both on the Mac. This "technique" eats processor as it if was ice cream in the summer, but it does the trick.

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to canvas

How to make canvas responsive How to fill the whole canvas with specific color? Use HTML5 to resize an image before upload Convert canvas to PDF Scaling an image to fit on canvas Split string in JavaScript and detect line break Get distance between two points in canvas canvas.toDataURL() SecurityError Converting Chart.js canvas chart to image using .toDataUrl() results in blank image Chart.js canvas resize

Examples related to image-resizing

Resize Google Maps marker icon image Resize image in PHP Responsive css background images How to resize an image to a specific size in OpenCV? How to set max width of an image in CSS Resizing an image in an HTML5 canvas Resize image proportionally with CSS?