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

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...

Whats the difference between the Tapo P100 and the P105?

There are a few different Tapo smart plugs. The P100 and P110 differ based on the smart power monitoring feature but where does the...

Time remaining keeps going up on Whirlpool dryer??

I have clothes in the dryer regularly and the time remaining shows on the display. There could be 10 mins remaining and if I...

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

Memory Converter

Converting values between various metric measurements is usually quite simple as there will be 1000 of the smaller unit in the next larger unit....

Bitrate Converter

Below you will find a bitrate converter. This tool will allow you to enter a bitrate value, in one of many different formats and...

Aesthetic Text Generator

There are various ways to make your social media profile seem more unique, some of which are not as easy to implement as others....

Aspect Ratio Calculator For Images

Aspect ratio is the ratio between the height and width of an image. If you want to resize an image by 100 pixels, you...

Add Text To Image

Use this free tool to add text to an image. Simply select the image file that you want to overlay text onto and you...

JavaScript Multi-line String Builder

Javascript did not always support multi-line strings. If you attempted to create a string variable using quotes, putting a line break into the source...

Latest Posts

Latest Questions