Resize Canvas Using HTML Slider

The HTML 5 Canvas has to be one of the coolest features that has been introduced in quite some time. Solid and well built, it makes image editing and modification such a breeze compared to the javascript-based tools we had in the past. There are bound to be some useful wrapper libraries out there that make the use of the canvas much easier, but for those looking to write code using the raw framework, there are some things that can be a little more tricky to achieve. One problem I had recently was being able to resize the canvas and the image inside of it. I technically wanted to zoom in and out, but the basic concept but it was essentially a resizing of the canvas.

The canvas library does contain a scale method, but it was not working the way I had wanted. I wanted to give users full control over how to resize the image. This meant, modifying height and width and then sliding the “zoom” button to increase or decrease the size of the image. I was able to achieve this, so I said I would make a post about it for others to hopefully find some use in.

So to start, here are some basic input controls. A box for the height and width and a slider then to zoom.

<p>Slide to scale canvas</p>
<input type="range" id="canvasscale" min="-10" max="20" value="0" onchange="scaleCanvas()"><br>
<p>Rezize canvas using input controls</p>
Height:<input type="text" id="height" onchange="resizeCanvas()"> Width: <input type="text" id="width" onchange="resizeCanvas()">

There are onchange events tied to these inputs. When the height or width is changed, call a function to actually take those values and resize the canvas. The scale function will simply take the value from the slider and perform a calculation to get the new height and width. You can set these however you wish. If the canvas is to be populated with a single image, then you can set these values from the dimensions of this image.

So all you need now is the canvas and the javascript code to resize the HTML canvas when the user modifies the height, width or moves the slider to zoom in or out of the image. The canvas is actually modified with this resizing so if you save it, the resulting image will be the same dimensions as the values in the input boxes.

<hr>
<canvas id="testcanvas" style="background-color:black;display:block"></canvas>

<script>
	originalHeight = 200;
	originalWidth = 400;
	function scaleCanvas()
	{
		var scale = document.getElementById("canvasscale").value;
		
		document.getElementById("height").value = Math.round(((originalHeight * ((scale * 10) / 100)) + originalHeight) * 100) / 100;
		document.getElementById("width").value = Math.round(((originalWidth * ((scale * 10) / 100)) + originalWidth) * 100) / 100;
		resizeCanvas();
	}
	
	function resizeCanvas()
	{
		var canvas = document.getElementById("testcanvas");
		var context = canvas.getContext("2d");	
		
		canvas.height = document.getElementById("height").value;
		canvas.width = document.getElementById("width").value;
	}
</script>

If you are dealing with a canvas that contains one image and you want to ensure that the image remains the same height and width as the canvas as it is modified, then you can use the following line of code to very easily do this. You should call this inside the resizeCanvas function. You will, of course, have to store the selected image as a global variable so as not to lose it when the canvas is modified. You will need something more complex if you are dealing with many different images and shapes on the canvas.

function resizeCanvas()
{
	var canvas = document.getElementById("testcanvas");
	var context = canvas.getContext("2d");	
	
	canvas.height = document.getElementById("height").value;
	canvas.width = document.getElementById("width").value;
	context.drawImage(selectedimage, 0, 0, canvas.width, canvas.height);
}

Related Articles

Related Questions

WordPress Table of Contents Plus Not Working

I have been using this plugin for a while and i really like it. It seems to have completely stopped working recently. I can...

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

List Sorting Tool

Welcome to our innovative list ordering and management tool. This next-level platform enables you to sort a list of items in ascending or descending...

Sudoku Solver

Welcome to our free online Sudoku solving tool, an interactive platform for puzzle enthusiasts seeking a break from a Sudoku conundrum. This advanced platform...

Apply Image Filters To Image

Digital imagery in the modern world is all about reinforcing emotions and stories behind each photo we take. To amplify this storytelling, we are...

Add Watermark To Image

As the world is increasingly consumed by digital media, protecting your original images is paramount. We are thrilled to introduce you to our innovative...

CSV To Xml Converter

Welcome to our CSV to XML converter tool, a convenient and user-friendly solution for all your data conversion needs. This versatile tool on our...

RGB Image Splitter

Welcome to our innovative RGB Splitter - a unique image analyzer tool that offers an in-depth peek into the building blocks of your photos....

Latest Posts

Latest Questions